Skip to content

Commit

Permalink
Add variant example
Browse files Browse the repository at this point in the history
  • Loading branch information
hlohaus committed Feb 12, 2024
1 parent aba4b96 commit 151f8b8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,9 @@ docker-compose down
## 💡 Usage

### New Client with Image Generation

#### not jet released

```python
from g4f.client import Client

Expand Down
Binary file added docs/cat.webp
Binary file not shown.
6 changes: 6 additions & 0 deletions docs/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,10 @@ response = client.images.create_variation(
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)
16 changes: 11 additions & 5 deletions g4f/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ def generate(self, prompt, model: str = None, **kwargs):
"",
[{"role": "user", "content": prompt}],
True,
proxy=self.client.get_proxy()
proxy=self.client.get_proxy(),
**kwargs
)
else:
response = provider.create(prompt)
Expand All @@ -253,15 +254,20 @@ def generate(self, prompt, model: str = None, **kwargs):

def create_variation(self, image: ImageType, model: str = None, **kwargs):
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(
"",
[{"role": "user", "content": "create a image like this"}],
True,
image=image,
proxy=self.client.get_proxy()
proxy=self.client.get_proxy(),
**kwargs
)
for chunk in response:
if isinstance(chunk, ImageProviderResponse):
return ImagesResponse([Image(image)for image in list(chunk.images)])
raise NoImageResponseError()
result = ([chunk.images] if isinstance(chunk.images, str) else chunk.images)
result = ImagesResponse([Image(image)for image in result])
if result is None:
raise NoImageResponseError()
return result

0 comments on commit 151f8b8

Please sign in to comment.