Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 committed Aug 1, 2023
1 parent dfdeef8 commit 8dfeb5f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 14 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"require": {
"php": "^8.2",
"guzzlehttp/guzzle": "^7.7",
"nutgram/hydrator": "^5.0",
"textalk/websocket": "^1.6"
},
"require-dev": {
Expand Down
44 changes: 30 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use GuzzleHttp\Client as Guzzle;
use InvalidArgumentException;
use SergiX44\Gradio\DTO\Config;
use SergiX44\Hydrator\Hydrator;
use SergiX44\Hydrator\HydratorInterface;
use WebSocket\Client as WebSocket;

class Client
Expand All @@ -12,55 +15,68 @@ class Client

private const WS_PREDICT = 'queue/join';

private string $src;

private string $sessionHash;

private array $config = [];
private Config $config;

private Guzzle $httpClient;

private WebSocket $wsClient;

private HydratorInterface $hydrator;

public function __construct(string $src)
{
if (
! str_starts_with($src, 'http://') &&
! str_starts_with($src, 'https://') &&
! str_starts_with($src, 'ws://') &&
! str_starts_with($src, 'wss://')
!str_starts_with($src, 'http://') &&
!str_starts_with($src, 'https://') &&
!str_starts_with($src, 'ws://') &&
!str_starts_with($src, 'wss://')
) {
throw new InvalidArgumentException('The src must not contain the protocol');
}

$this->src = str_ends_with($src, '/') ? $src : "{$src}/";
$src = str_ends_with($src, '/') ? $src : "{$src}/";
$this->sessionHash = substr(md5(microtime()), 0, 11);
$this->hydrator = new Hydrator();

$this->httpClient = new Guzzle([
'base_uri' => str_replace('ws', 'http', $this->src),
'base_uri' => str_replace('ws', 'http', $src),
'headers' => [
'User-Agent' => 'gradio_client_php/1.0',
],
]);
$this->wsClient = new WebSocket(str_replace('http', 'ws', $this->src));
$this->wsClient = new WebSocket(str_replace('http', 'ws', $src));

$this->config = $this->getConfig();
$this->config = $this->loadConfig();
}

public function predict(string $apiName = null, int $fnIndex = null, mixed ...$arguments)
{
if ($apiName === null && $fnIndex === null) {
throw new InvalidArgumentException('You must provide an apiName or fnIndex');
}

$fn = $fnIndex ?? $this->config->fnIndexFromApiName($apiName);


}

private function submit()
private function submit(int $dnIndex, mixed ...$arguments)
{


}

private function getConfig()
private function loadConfig()
{
$response = $this->httpClient->get('config');

return json_decode($response->getBody()->getContents(), flags: JSON_THROW_ON_ERROR);
return $this->hydrator->hydrateWithJson(Config::class, $response->getBody()->getContents());
}

public function getConfig(): Config
{
return $this->config;
}
}
35 changes: 35 additions & 0 deletions src/DTO/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace SergiX44\Gradio\DTO;

class Config
{
public ?string $version = null;
public ?string $mode = null;
public ?bool $dev_mode = false;
public ?bool $analytics_enabled = false;

public array $components = [];

public ?string $css = null;
public ?string $title = null;
public bool $is_space = false;
public bool $enable_queue = false;
public bool $show_error = false;
public bool $show_api = false;
public bool $is_colab = false;
public array $stylesheets = [];
public array $dependencies = [];
public ?string $root = null;

public function fnIndexFromApiName(string $apiName): ?int
{
foreach ($this->dependencies as $index => $dep) {
if ($dep->api_name === $apiName) {
return $index;
}
}

return null;
}
}
3 changes: 3 additions & 0 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@

it('can test', function () {
$c = new \SergiX44\Gradio\Client('https://sanchit-gandhi-whisper-jax.hf.space/');

expect($c)->toBeInstanceOf(\SergiX44\Gradio\Client::class);
expect($c->getConfig())->toBeInstanceOf(\SergiX44\Gradio\DTO\Config::class);
});

0 comments on commit 8dfeb5f

Please sign in to comment.