Skip to content

Commit 9a1b826

Browse files
committed
Add image generation functionality to Grokit
- Implement `image()` method to generate and return JPEG binary data - Add `image_url()` method to get URL of generated image - Update README with examples for new image generation features - Modify _stream_response to handle image attachment data - Enhance error handling for image generation process
1 parent 326e87e commit 9a1b826

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,36 @@ grok = Grokit(
2828
)
2929
```
3030

31-
## Generate
31+
## Generate Text
3232

3333
```python
3434
response = grok.generate('Who are you?', model_id='grok-2-mini')
3535
print(response)
3636
```
3737

38-
## Stream
38+
## Stream Text
3939

4040
```python
4141
for chunk in grok.stream('Who are you?', model_id='grok-2'):
4242
print(chunk, end='', flush=True)
4343
```
4444

45+
## Generate Image
46+
47+
### JPEG binary
48+
```python
49+
image_data = grok.image('An astronaut riding a horse.')
50+
51+
with open('sunset.jpg', 'wb') as f:
52+
f.write(image_data)
53+
```
54+
55+
### image URL
56+
```python
57+
image_url = grok.image_url('An astronaut riding a horse.')
58+
print(image_url)
59+
```
60+
4561
## Credentials
4662

4763
To obtain the necessary credentials for using Grokit, follow these steps:

grokit/grokit.py

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,38 @@ def stream(
9494
model_id,
9595
)
9696

97+
def image(self, prompt: str) -> bytes:
98+
image_url = self._get_image_url(prompt)
99+
image_response = requests.get(image_url)
100+
if image_response.status_code == 200:
101+
return image_response.content
102+
raise ValueError('Failed to download the image')
103+
104+
def image_url(self, prompt: str) -> str:
105+
return self._get_image_url(prompt)
106+
107+
def _get_image_url(self, prompt: str) -> str:
108+
conversation_id = self.create_conversation()
109+
if not conversation_id:
110+
raise ValueError('Failed to create conversation')
111+
image_prompt = 'Generate an image of "{}"'.format(prompt)
112+
response = self._stream_response(
113+
conversation_id,
114+
image_prompt,
115+
'',
116+
GrokModels.GROK_2_MINI,
117+
)
118+
119+
for chunk in response:
120+
try:
121+
data = json.loads(chunk)
122+
if 'result' in data and 'imageAttachment' in data['result']:
123+
return data['result']['imageAttachment']['imageUrl']
124+
except json.JSONDecodeError:
125+
continue
126+
127+
raise ValueError('Failed to generate the image')
128+
97129
def _ensure_conversation_id(self, conversation_id: Optional[str]) -> str:
98130
if not conversation_id:
99131
conversation_id = self.create_conversation()
@@ -169,5 +201,8 @@ def _process_response_stream(
169201
for line in response.iter_lines():
170202
if line:
171203
chunk = json.loads(line)
172-
if 'result' in chunk and 'message' in chunk['result']:
173-
yield chunk['result']['message']
204+
if 'result' in chunk:
205+
if 'message' in chunk['result']:
206+
yield chunk['result']['message']
207+
elif 'imageAttachment' in chunk['result']:
208+
yield json.dumps(chunk)

grokit/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
#!/usr/bin/env python
22
# -*- coding: utf-8 -*-
33

4-
__version__ = '1.2410.0'
4+
__version__ = '1.2410.1'

0 commit comments

Comments
 (0)