Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
iMactool committed Jun 25, 2023
1 parent 61bab46 commit c2c0371
Showing 1 changed file with 52 additions and 3 deletions.
55 changes: 52 additions & 3 deletions src/Replicate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class Replicate

private array $inputParams = [];

private string $baseUrl = '';
private string $baseUrl = 'https://api.replicate.com/v1/predictions';

private string $token = '';

Expand Down Expand Up @@ -140,6 +140,55 @@ public static function get(string $replicateId)
return $result;
}

public function list()
{
$response = self::make()
->client()
->get($this->getBaseUrl());

if ($response->getStatusCode() !== 200) {
throw new Exception('Failed to retrieve data.');
}

return json_decode((string) $response->getBody(), true);
}

public function getModel($model_owner, $model_name)
{
if (empty($model_name) || empty($model_owner)) {
throw new Exception("model_owner or model_name can't be empty.");
}
$uri = 'https://api.replicate.com/v1/models/' . trim($model_owner) . '/' . trim($model_name);

$response = self::make()
->client()
->get($uri);

if ($response->getStatusCode() !== 200) {
throw new Exception('Failed to retrieve data.');
}

return json_decode((string) $response->getBody(), true);
}

public function getModelVersion($model_owner, $model_name, $version_id)
{
if (empty($model_name) || empty($model_owner) || empty($version_id)) {
throw new Exception("model_owner、version_id or model_name can't be empty.");
}
$uri = 'https://api.replicate.com/v1/models/' . trim($model_owner) . '/' . trim($model_name) . '/versions/' . $version_id;

$response = self::make()
->client()
->get($uri);

if ($response->getStatusCode() !== 200) {
throw new Exception('Failed to retrieve data.');
}

return json_decode((string) $response->getBody(), true);
}

public function withPrompt(Prompt $prompt)
{
$this->prompt = $prompt;
Expand Down Expand Up @@ -241,7 +290,7 @@ public function generate()
StableDiffusionResult::create($data);
} catch (Exception $exception) {
$msg = $exception->getMessage();
var_dump(['data insert error' => $msg]);
// var_dump(['data insert error' => $msg]);
if ($exception instanceof PDOException) {
$errorInfo = $exception->errorInfo;
$code = $errorInfo[1];
Expand All @@ -260,7 +309,7 @@ private function client(): ClientInterface
{
return ApplicationContext::getContainer()->get(ClientFactory::class)->create([
// 'base_uri' => $this->getBaseUrl(),
// 'timeout' => 10,
'timeout' => 600,
'headers' => [
'Authorization' => 'Token ' . $this->getToken(),
'Accept' => 'application/json',
Expand Down

0 comments on commit c2c0371

Please sign in to comment.