Files
ai-stylegallery/app/Api/Plugins/RunwareAIPlugin.php

106 lines
3.1 KiB
PHP

<?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()];
}
}
}