the RunwareAI Plugin is working now
This commit is contained in:
@@ -11,6 +11,5 @@ interface ApiPluginInterface
|
||||
public function disable(): bool;
|
||||
public function getStatus(string $imageUUID): array;
|
||||
public function getProgress(string $imageUUID): array;
|
||||
public function upload(string $imagePath): array;
|
||||
public function styleChangeRequest(string $prompt, string $seedImageUUID): array;
|
||||
public function processImageStyleChange(string $imagePath, string $prompt, string $modelId, ?string $parameters = null): array;
|
||||
}
|
||||
@@ -1,106 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace App\Api\Plugins;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\RequestException;
|
||||
|
||||
class RunwareAIPlugin implements ApiPluginInterface
|
||||
{
|
||||
protected $client;
|
||||
protected $apiUrl;
|
||||
protected $apiKey;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
// Hier müssten die API-URL und der API-Schlüssel aus der Datenbank oder Konfiguration geladen werden.
|
||||
// Fürs Erste hardcodiere ich sie als Platzhalter.
|
||||
$this->apiUrl = env('RUNWARE_AI_API_URL', 'https://api.runware.ai/v1');
|
||||
$this->apiKey = env('RUNWARE_AI_API_KEY', 'YOUR_RUNWARE_AI_API_KEY');
|
||||
|
||||
$this->client = new Client([
|
||||
'base_uri' => $this->apiUrl,
|
||||
'headers' => [
|
||||
'Authorization' => 'Bearer ' . $this->apiKey,
|
||||
'Accept' => 'application/json',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function getIdentifier(): string
|
||||
{
|
||||
return 'runware-ai';
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'Runware AI';
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
// Implementieren Sie hier die Logik, um zu prüfen, ob das Plugin aktiviert ist
|
||||
return true;
|
||||
}
|
||||
|
||||
public function enable(): bool
|
||||
{
|
||||
// Implementieren Sie hier die Logik zum Aktivieren des Plugins
|
||||
return true;
|
||||
}
|
||||
|
||||
public function disable(): bool
|
||||
{
|
||||
// Implementieren Sie hier die Logik zum Deaktivieren des Plugins
|
||||
return true;
|
||||
}
|
||||
|
||||
public function getStatus(string $imageUUID): array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->get("image-inference/status/{$imageUUID}");
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
} catch (RequestException $e) {
|
||||
return ['error' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
public function getProgress(string $imageUUID): array
|
||||
{
|
||||
// Runware AI hat keine separate Progress-API, Status enthält den Fortschritt
|
||||
return $this->getStatus($imageUUID);
|
||||
}
|
||||
|
||||
public function upload(string $imagePath): array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->post('image-inference/upload', [
|
||||
'multipart' => [
|
||||
[
|
||||
'name' => 'image',
|
||||
'contents' => fopen($imagePath, 'r'),
|
||||
'filename' => basename($imagePath),
|
||||
],
|
||||
],
|
||||
]);
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
} catch (RequestException $e) {
|
||||
return ['error' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
|
||||
public function styleChangeRequest(string $prompt, string $seedImageUUID): array
|
||||
{
|
||||
try {
|
||||
$response = $this->client->post('image-inference/image-to-image', [
|
||||
'json' => [
|
||||
'prompt' => $prompt,
|
||||
'seed_image_uuid' => $seedImageUUID,
|
||||
],
|
||||
]);
|
||||
return json_decode($response->getBody()->getContents(), true);
|
||||
} catch (RequestException $e) {
|
||||
return ['error' => $e->getMessage()];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -71,7 +71,27 @@ class RunwareAi implements ApiPluginInterface
|
||||
return ['progress' => 0];
|
||||
}
|
||||
|
||||
public function upload(string $imagePath): array
|
||||
public function processImageStyleChange(string $imagePath, string $prompt, string $modelId, ?string $parameters = null): array
|
||||
{
|
||||
// Step 1: Upload the original image
|
||||
$uploadResult = $this->upload($imagePath);
|
||||
|
||||
if (!isset($uploadResult['data'][0]['imageUUID'])) {
|
||||
throw new \Exception('Image upload to AI service failed or returned no UUID.');
|
||||
}
|
||||
$seedImageUUID = $uploadResult['data'][0]['imageUUID'];
|
||||
|
||||
// Step 2: Request style change using the uploaded image's UUID
|
||||
$result = $this->styleChangeRequest($prompt, $seedImageUUID, $modelId, $parameters);
|
||||
|
||||
if (!isset($result['base64Data'])) {
|
||||
throw new \Exception('AI service did not return base64 image data.');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private function upload(string $imagePath): array
|
||||
{
|
||||
$this->logInfo('Attempting to upload image to RunwareAI.', ['image_path' => $imagePath]);
|
||||
if (!$this->apiProvider->api_url || !$this->apiProvider->token) {
|
||||
@@ -83,15 +103,19 @@ class RunwareAi implements ApiPluginInterface
|
||||
$token = $this->apiProvider->token;
|
||||
$taskUUID = (string) Str::uuid();
|
||||
|
||||
$imageData = 'data:image/png;base64,' . base64_encode(file_get_contents($imagePath));
|
||||
|
||||
try {
|
||||
$response = Http::withHeaders([
|
||||
'Authorization' => 'Bearer ' . $token,
|
||||
'Accept' => 'application/json',
|
||||
])->attach(
|
||||
'image', file_get_contents($imagePath), basename($imagePath)
|
||||
)->post($apiUrl, [
|
||||
'taskType' => 'imageUpload',
|
||||
'taskUUID' => $taskUUID,
|
||||
'Content-Type' => 'application/json',
|
||||
])->post($apiUrl, [
|
||||
[
|
||||
'taskType' => 'imageUpload',
|
||||
'taskUUID' => $taskUUID,
|
||||
'image' => $imageData,
|
||||
]
|
||||
]);
|
||||
|
||||
$response->throw();
|
||||
@@ -103,7 +127,7 @@ class RunwareAi implements ApiPluginInterface
|
||||
}
|
||||
}
|
||||
|
||||
public function styleChangeRequest(string $prompt, string $seedImageUUID, ?string $parameters = null): array
|
||||
private function styleChangeRequest(string $prompt, string $seedImageUUID, string $modelId, ?string $parameters = null): array
|
||||
{
|
||||
$this->logInfo('Attempting style change request to RunwareAI.', ['prompt' => $prompt, 'seed_image_uuid' => $seedImageUUID]);
|
||||
if (!$this->apiProvider->api_url || !$this->apiProvider->token) {
|
||||
@@ -121,6 +145,7 @@ class RunwareAi implements ApiPluginInterface
|
||||
'positivePrompt' => $prompt,
|
||||
'seedImage' => $seedImageUUID,
|
||||
'outputType' => 'base64Data',
|
||||
'model' => $modelId,
|
||||
];
|
||||
|
||||
$decodedParameters = json_decode($parameters, true) ?? [];
|
||||
@@ -132,13 +157,28 @@ class RunwareAi implements ApiPluginInterface
|
||||
$response = Http::withHeaders([
|
||||
'Authorization' => 'Bearer ' . $token,
|
||||
'Accept' => 'application/json',
|
||||
])->post($apiUrl, $data);
|
||||
])->post($apiUrl, [
|
||||
$data
|
||||
]);
|
||||
|
||||
$response->throw();
|
||||
$this->logInfo('Style change request successful to RunwareAI.', ['task_uuid' => $taskUUID, 'response' => $response->json()]);
|
||||
return $response->json();
|
||||
$responseData = $response->json();
|
||||
|
||||
if (!isset($responseData['data'][0]['imageBase64Data'])) {
|
||||
throw new \Exception('AI service did not return base64 image data.');
|
||||
}
|
||||
|
||||
$base64Image = $responseData['data'][0]['imageBase64Data'];
|
||||
|
||||
$this->logInfo('Style change request successful to RunwareAI.', ['task_uuid' => $taskUUID, 'response' => $responseData]);
|
||||
return ['base64Data' => $base64Image];
|
||||
} catch (\Exception $e) {
|
||||
$this->logError('Style change request to RunwareAI failed.', ['error' => $e->getMessage(), 'task_uuid' => $taskUUID]);
|
||||
$errorData = [];
|
||||
/*if ($e instanceof \Illuminate\Http\Client\RequestException && $e->response) {
|
||||
$errorData['response_body'] = $e->response->body();
|
||||
$errorData['response_status'] = $e->response->status();
|
||||
}*/
|
||||
$this->logError('Style change request to RunwareAI failed.', ['error' => $e->getMessage(), 'task_uuid' => $taskUUID] + $errorData);
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user