Commit 0b0b41b0 by wangfa

增加api 发送

parent 5e2a1b48
......@@ -56,3 +56,6 @@ VITE_PUSHER_HOST="${PUSHER_HOST}"
VITE_PUSHER_PORT="${PUSHER_PORT}"
VITE_PUSHER_SCHEME="${PUSHER_SCHEME}"
VITE_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
PYTHON_API=
......@@ -16,6 +16,7 @@ protected function init()
{
parent::init();
$color = Admin::color();
$colors = [$color->primary(), $color->alpha('blue2', 0.5)];
......
......@@ -28,19 +28,20 @@ public function show(Request $request)
/**
* 上传策略
*
* @param \Illuminate\Http\Request $request
* @return mixed
*/
public function policy(Request $request)
public function policy()
{
$user = $request->user();
$module = $request->input('module', 'user');
# 区分图片上传路径和视频上传路径
# 10M
$size = 1024 * 1024 * 5;
$module = 'ai_avatar';
$ossConfig = app(AdminConfigService::class)->getAdminUserOssConfig();
$result = (new OssService($ossConfig))->policy('files/' . $module . '/' . 'admin' . '/', $size);
$result = [];
if (filled($ossConfig)) {
$result = (new OssService($ossConfig))->policy('files/' . $module . '/' . 'admin' . '/', $size);
}
return $this->success('success', $result);
}
}
......@@ -9,6 +9,12 @@
class TaskController extends Controller
{
/**
* 提交任务
*
* @param Request $request
* @return mixed
*/
public function SubmitTask(Request $request)
{
$data = $request->input();
......@@ -18,6 +24,20 @@ public function SubmitTask(Request $request)
}
/**
* api 提交任务
*
* @param Request $request
* @return mixed
*/
public function apiSubmit(Request $request)
{
$data = $request->input();
$user = $request->user();
$result = app(TaskService::class)->apiSubmit($user->id, $data);
return $this->success('success', $result);
}
/**
* python-gpt任务回调
*/
public function gpt_callback(Request $request)
......
<?php
namespace App\Jobs;
use App\Service\ExternalApiService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class SendImagineJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public array $data = [];
public function __construct(array $data = [])
{
$this->queue = "{send-imagine}";
$this->data = $data;
}
public function handle()
{
app(ExternalApiService::class)->imagine($this->data);
}
}
......@@ -4,7 +4,6 @@
use App\Models\AdminConfig;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
class AdminConfigService
{
......@@ -24,7 +23,9 @@ class AdminConfigService
*/
public function getAdminUserOssConfig()
{
$config = AdminConfig::where('type', self::TYPE_DEFAULT)->where('status', self::STATUS_OPEN)
$oldConfig = [];
$config = AdminConfig::where('type', self::TYPE_DEFAULT)
->where('status', self::STATUS_OPEN)
->first();
if (filled($config)) {
$oldConfig = $config->config;
......
<?php
namespace App\Service;
use GuzzleHttp\Client;
class ExternalApiService
{
public string $domain;
public function __construct(string $domain = '')
{
$this->setDomain($domain);
}
/**
* 执行生成图片命令
*
* @param array $data
* @return array|mixed
*/
public function imagine(array $data = [])
{
$url = $this->getPath('/api/imagine');
return $this->httpPost($url, $data);
}
/**
* @param string $path
* @return string
*/
public function getPath(string $path = '')
{
return $this->getDomain() . $path;
}
/**
*
* @return string
*/
public function getDomain()
{
return $this->domain;
}
/**
* @param string $domain
*/
public function setDomain(string $domain = '')
{
$this->domain = $domain;
if (blank($domain)) {
$this->domain = config('common.python_api');
}
}
/**
* 发送post请求
*
* @param string $url
* @param array $data
* @return array|mixed
*/
public function httpPost(string $url = '', array $data = [])
{
$requestData = [
'headers' => [
'Content-Type' => 'application/json'
],
'json' => [
'data' => $data
],
];
$client = new Client();
try {
$response = $client->post($url, $requestData);
$data = json_decode($response->getBody()->getContents(), true);
return $data ?? [];
} catch (\Throwable $throwable) {
return [];
}
}
/**
* 下载图片
*
* @param array $data
* @return array|mixed
*/
public function downloadImage(array $data = [])
{
$url = $this->getPath('/api/download');
return $this->httpPost($url, $data);
}
}
......@@ -3,10 +3,10 @@
namespace App\Service;
use App\Exceptions\UserException;
use App\Jobs\SendImagineJob;
use App\Models\PromptTask;
use App\Models\Task;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class TaskService
......@@ -16,25 +16,28 @@ class TaskService
public const GPT_prompt_num = 1; // 指令数量
public function submit($user_id, array $data = [])
/**
* @param array $data
*/
public function validateSubmitData(array $data = [])
{
# task表新增一条记录
$type = $data['type'] ?? '';
$prompt = $data['prompt'] ?? '';
$prompt_img = $data['prompt_img'] ?? [];
$prompt_num = $data['prompt_num'] ?? '';
$promptImg = $data['prompt_img'] ?? [];
$promptNum = $data['prompt_num'] ?? '';
if (empty($type) || empty($prompt)) {
throw new UserException('任务类型不能为空');
}
# 生成指令数量只能在1-10之间
if ($prompt_num < 1 || $prompt_num > 10) {
if ($promptNum < 1 || $promptNum > 10) {
throw new UserException('生成指令数量只能在1-10之间');
}
# 判断类型
if ($type == self::TYPE_IMG_TO_IMG) {
# 图片转图片
if (!count($prompt_img)) {
if (!count($promptImg)) {
throw new UserException('图片不能为空');
}
} elseif ($type == self::TYPE_TEXT_TO_IMG) {
......@@ -42,39 +45,101 @@ public function submit($user_id, array $data = [])
} else {
throw new UserException('任务类型错误');
}
$list = [
'type' => $type,
'prompt' => $prompt,
'prompt_img' => json_encode($prompt_img),
'user_id' => $user_id,
'prompt_num' => self::GPT_prompt_num,
];
# 保存任务
$task_id = Task::query()->create($list)->id;
# old
# self::setRedis($task_id, $list);
# 改成直接提交到自动化任务队列
$list = [
'prompt' => $prompt,
'user_id' => $user_id,
'task_id' => $task_id,
'prompt_img' => json_encode($prompt_img),
'type' => $type,
'callback' => config('common.prompt_callback'),
'policy' => config('common.policy_callback'),
];
$prompt_id = promptTask::query()->create($list)->id;
$list['prompt_id'] = $prompt_id;
}
/**
* 提交任务
*
* @param int $user_id
* @param array $data
* @return array
*/
public function submit(int $user_id = 0, array $data = [])
{
$this->validateSubmitData($data);
$task = $this->createTask($user_id, $data);
$promptTask = $this->createPromptTask($user_id, $task->id, $data);
$list['prompt_id'] = $promptTask->id;
# 插入redis,这是自动化的任务
Redis::rpush('midjourney_prompt', json_encode($list));
# 返回任务id
return [
'task_id' => $task_id,
'task_id' => $task->id,
'prompt_num' => self::GPT_prompt_num,
];
}
/**
* 创建任务
*
* @param int $userId
* @param array $data
* @return Task
*/
public function createTask(int $userId = 0, array $data = [])
{
$taskData = [
'type' => (string)($data['type'] ?? ''),
'prompt' => (string)($data['prompt'] ?? ''),
'prompt_img' => json_encode($data['prompt_img'] ?? []),
'user_id' => $userId,
'prompt_num' => self::GPT_prompt_num,
];
return Task::create($taskData);
}
/**
* 创建提示任务
*
* @param int $userId
* @param int $taskId
* @param array $data
* @return PromptTask
*/
public function createPromptTask(int $userId = 0, int $taskId = 0, array $data = [])
{
$promptTask = [
'prompt' => (string)($data['prompt'] ?? ''),
'user_id' => $userId,
'task_id' => $taskId,
'prompt_img' => json_encode($data['prompt_img'] ?? []),
'type' => (string)($data['type']),
'callback' => config('common.prompt_callback'),
'policy' => config('common.policy_callback'),
];
return PromptTask::create($promptTask);
}
/**
* api 提交任务
*
* @param int $userId
* @param array $data
* @return array
*/
public function apiSubmit(int $userId = 0, array $data = [])
{
$this->validateSubmitData($data);
$task = $this->createTask($userId, $data);
$promptTask = $this->createPromptTask($userId, $task->id, $data);
$sendData = [
'token' => config('common.imagine_token'),
'guild_id' => config('common.imagine_guild_id'),
'channel_id' => config('common.imagine_channel_id'),
'prompt' => $task->prompt,
'prompt_img' => $task->prompt_img,
'prompt_id' => $promptTask->id,
'task_id' => $task->id,
];
dispatch(new SendImagineJob($sendData));
return [
'task_id' => $task->id,
'prompt_num' => $task->prompt_num
];
}
/**
* redis队列插入--这是gpt生成指令的任务
*/
......@@ -176,8 +241,7 @@ public function prompt_callback($data = [])
/**
* python - 下载图片完成-回调
*/
public
function img_download_callback($data = [])
public function img_download_callback($data = [])
{
$credentials = Arr::only($data, ['prompt_id', 'result_img', 'message']);
# 必须字段
......@@ -214,8 +278,7 @@ function img_download_callback($data = [])
/**
* 轮询接口
*/
public
function task_callback($user_id, $data = [])
public function task_callback($user_id, $data = [])
{
$credentials = Arr::only($data, ['task_id']);
# 必须字段
......@@ -242,8 +305,7 @@ function task_callback($user_id, $data = [])
/**
* 创建切割图片任务
*/
public
function create_split_img_task($user_id, $data = [])
public function create_split_img_task($user_id, $data = [])
{
$credentials = Arr::only($data, ['prompt_id', 'cut_id']);
# 必须字段
......
......@@ -12,4 +12,13 @@
'split_img_callback' => env('SPLIT_IMG_CALLBACK', 'http://test.phpgpt.com/api/users/split/callback'),
# 下载图片的回调地址
'img_download_callback' => env('IMG_DOWNLOAD_CALLBACK', 'http://test.phpgpt.com/api/users/img/callback'),
// python API
'python_api' => env('PYTHON_API', 'http://127.0.0.1:9001'),
// 生成图片参数
'imagine_token' => env('IMAGINE_TOKEN', 'MTA3NTYwNTUxMjAwMDkwNTI5Nw.GbEb6_.9-H-5kf501rRc0SJQa5mB-2eV1Fh2u4-rWWG_0'),
'imagine_guild_id' => env('IMAGINE_GUILD_ID', '1095628793860857957'),
'imagine_channel_id' => env('IMAGINE_CHANNEL_ID', '1095628793860857960'),
];
<?php
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
/*
......@@ -48,6 +47,8 @@
Route::get('config/policy', 'ConfigController@policy');
// 提交任务
Route::post('submit', 'TaskController@SubmitTask');
// Api 提交任务
Route::post('api-submit', 'TaskController@apiSubmit');
# 轮询请求任务回调
Route::get('task/callback', 'TaskController@task_callback');
# 创建切割图片任务
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment