Add variant example

This commit is contained in:
Heiner Lohaus 2024-02-12 12:08:08 +01:00
parent aba4b96f23
commit 151f8b8b0e
4 changed files with 20 additions and 5 deletions

View File

@ -227,6 +227,9 @@ docker-compose down
## 💡 Usage ## 💡 Usage
### New Client with Image Generation ### New Client with Image Generation
#### not jet released
```python ```python
from g4f.client import Client from g4f.client import Client

BIN
docs/cat.webp Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.7 KiB

View File

@ -68,4 +68,10 @@ response = client.images.create_variation(
image_url = response.data[0].url image_url = response.data[0].url
``` ```
Orginal:
[![Image with cat](/docs/cat.jpeg)](/docs/client.md)
Variant:
[![Image with cat](/docs/cat.webp)](/docs/client.md)
[to Home](/docs/client.md) [to Home](/docs/client.md)

View File

@ -241,7 +241,8 @@ class Images():
"", "",
[{"role": "user", "content": prompt}], [{"role": "user", "content": prompt}],
True, True,
proxy=self.client.get_proxy() proxy=self.client.get_proxy(),
**kwargs
) )
else: else:
response = provider.create(prompt) response = provider.create(prompt)
@ -253,15 +254,20 @@ class Images():
def create_variation(self, image: ImageType, model: str = None, **kwargs): def create_variation(self, image: ImageType, model: str = None, **kwargs):
provider = self.models.get(model) if model else self.provider provider = self.models.get(model) if model else self.provider
if isinstance(provider, BaseProvider): result = None
if isinstance(provider, type) and issubclass(provider, BaseProvider):
response = provider.create_completion( response = provider.create_completion(
"", "",
[{"role": "user", "content": "create a image like this"}], [{"role": "user", "content": "create a image like this"}],
True, True,
image=image, image=image,
proxy=self.client.get_proxy() proxy=self.client.get_proxy(),
**kwargs
) )
for chunk in response: for chunk in response:
if isinstance(chunk, ImageProviderResponse): if isinstance(chunk, ImageProviderResponse):
return ImagesResponse([Image(image)for image in list(chunk.images)]) result = ([chunk.images] if isinstance(chunk.images, str) else chunk.images)
raise NoImageResponseError() result = ImagesResponse([Image(image)for image in result])
if result is None:
raise NoImageResponseError()
return result