Skip to content

Commit

Permalink
Fix styling
Browse files Browse the repository at this point in the history
  • Loading branch information
sergix44 authored and github-actions[bot] committed Feb 13, 2024
1 parent 0f7b0fc commit fc64add
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 17 deletions.
12 changes: 7 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Client extends RemoteClient

private ?string $hfToken;

public function __construct(string $src, string $hfToken = null, Config $config = null)
public function __construct(string $src, ?string $hfToken = null, ?Config $config = null)
{
parent::__construct($src);
$this->config = $config ?? $this->http('get', self::HTTP_CONFIG, dto: Config::class);
Expand All @@ -62,7 +62,7 @@ public function getConfig(): Config
return $this->config;
}

public function predict(array $arguments, string $apiName = null, int $fnIndex = null): ?Output
public function predict(array $arguments, ?string $apiName = null, ?int $fnIndex = null): ?Output
{
if ($apiName === null && $fnIndex === null) {
throw new InvalidArgumentException('You must provide an apiName or fnIndex');
Expand Down Expand Up @@ -132,6 +132,7 @@ protected function makeUri(Endpoint $endpoint): string
$name = $endpoint->apiName();
if ($name !== null) {
$name = str_replace('/', '', $name);

return "run/$name";
}

Expand Down Expand Up @@ -213,8 +214,8 @@ private function sseV1V2Loop(Endpoint $endpoint, array $payload): ?Output
throw new GradioException('Error joining the queue');
}

// $data = $this->decodeResponse($response);
// $eventId = $data['event_id'];
// $data = $this->decodeResponse($response);
// $eventId = $data['event_id'];

$response = $this->httpRaw('get', self::SSE_GET_DATA, ['session_hash' => $this->sessionHash], [
'headers' => [
Expand All @@ -225,10 +226,11 @@ private function sseV1V2Loop(Endpoint $endpoint, array $payload): ?Output

$buffer = '';
$message = null;
while (!$response->getBody()->eof()) {
while (! $response->getBody()->eof()) {
$data = $response->getBody()->read(1);
if ($data !== "\n") {
$buffer .= $data;

continue;
}

Expand Down
5 changes: 2 additions & 3 deletions src/Client/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

readonly class Endpoint
{

public function __construct(
private Config $config,
public int $index,
Expand All @@ -26,11 +25,11 @@ public function __isset(string $name): bool

public function skipsQueue(): bool
{
return !($this->data['queue'] ?? $this->config->enable_queue);
return ! ($this->data['queue'] ?? $this->config->enable_queue);
}

public function apiName(): ?string
{
return !empty($this->data['api_name']) ? $this->data['api_name'] : null;
return ! empty($this->data['api_name']) ? $this->data['api_name'] : null;
}
}
12 changes: 7 additions & 5 deletions src/Client/RemoteClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ abstract class RemoteClient extends RegisterEvents
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');
}
Expand All @@ -44,12 +44,14 @@ public function __construct(string $src)
protected function http(string $method, string $uri, array $params = [], array $opt = [], ?string $dto = null)
{
$response = $this->httpRaw($method, $uri, $params, $opt);

return $this->decodeResponse($response, $dto);
}

protected function httpRaw(string $method, string $uri, array $params = [], array $opt = [])
{
$keyContent = $method === 'get' ? 'query' : 'json';

return $this->httpClient->request($method, $uri, array_merge([
$keyContent => $params,
], $opt));
Expand All @@ -60,7 +62,7 @@ protected function ws(string $uri, array $options = []): EnhancedClient
return new EnhancedClient(str_replace('http', 'ws', $this->src).$uri, $options);
}

protected function decodeResponse(ResponseInterface|string $response, string $mapTo = null): mixed
protected function decodeResponse(ResponseInterface|string $response, ?string $mapTo = null): mixed
{
$body = $response instanceof ResponseInterface ? $response->getBody()->getContents() : $response;

Expand Down
4 changes: 3 additions & 1 deletion src/DTO/Resolvers/MessageResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ public function concreteFor(array $data): ?string
MessageType::PROCESS_GENERATING->value => ProcessGenerating::class,
MessageType::PROCESS_COMPLETED->value => ProcessCompleted::class,
MessageType::LOG->value => Log::class,
default => (new class extends Message {})::class,
default => (new class extends Message
{
})::class,
};
}
}
2 changes: 1 addition & 1 deletion src/Exception/QueueFullException.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class QueueFullException extends GradioException
{
public function __construct(string $message = 'Queue full.', int $code = 0, Throwable $previous = null)
public function __construct(string $message = 'Queue full.', int $code = 0, ?Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ExampleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
$client = new Client('https://multimodalart-stable-cascade.hf.space');

$response = $client->predict([
"house", // string in 'Prompt' Textbox component
"!", // string in 'Negative prompt' Textbox component
'house', // string in 'Prompt' Textbox component
'!', // string in 'Negative prompt' Textbox component
0, // number (numeric value between 0 and 2147483647) in 'Seed' Slider component
1024, // number (numeric value between 1024 and 1536) in 'Width' Slider component
1024, // number (numeric value between 1024 and 1536) in 'Height' Slider component
Expand Down

0 comments on commit fc64add

Please sign in to comment.