Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,43 +1,79 @@
|
|
1 |
-
# app.py
|
2 |
-
import os
|
3 |
import gradio as gr
|
4 |
-
import
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
#
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
#
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import requests
|
3 |
+
import json
|
4 |
+
import os
|
5 |
+
from PIL import Image
|
6 |
+
|
7 |
+
def generate(description, image):
|
8 |
+
if image is None:
|
9 |
+
# No image uploaded, generate text only
|
10 |
+
return generate_text(description)
|
11 |
+
|
12 |
+
# Image uploaded, generate text with image caption
|
13 |
+
return generate_text_with_image(description, image)
|
14 |
+
|
15 |
+
def generate_text(description):
|
16 |
+
headers = {
|
17 |
+
'Content-Type': 'application/json',
|
18 |
+
'Authorization': f'Bearer {os.getenv("API_KEY")}'
|
19 |
+
}
|
20 |
+
|
21 |
+
payload = {
|
22 |
+
'messages': [{'role': 'system', 'content': f'{description}'}],
|
23 |
+
'max_tokens': 10000,
|
24 |
+
'model': os.getenv("MODEL")
|
25 |
+
}
|
26 |
+
|
27 |
+
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
|
28 |
+
data = json.loads(response.text)
|
29 |
+
|
30 |
+
if 'choices' in data and len(data['choices']) > 0:
|
31 |
+
command = data['choices'][0]['message']['content'].strip()
|
32 |
+
return command
|
33 |
+
elif 'error' in data:
|
34 |
+
error_message = data['error']['message']
|
35 |
+
return f'Ошибка: {error_message}'
|
36 |
+
else:
|
37 |
+
return f'Не удалось сгенерировать текст. {data}'
|
38 |
+
|
39 |
+
def generate_text_with_image(description, image):
|
40 |
+
# Preprocess image
|
41 |
+
image = Image.open(image).resize((224, 224))
|
42 |
+
image = image.convert('RGB')
|
43 |
+
image = image.tobytes()
|
44 |
+
|
45 |
+
# Convert image to base64 encoding
|
46 |
+
image_base64 = base64.b64encode(image).decode('utf-8')
|
47 |
+
|
48 |
+
headers = {
|
49 |
+
'Content-Type': 'application/json',
|
50 |
+
'Authorization': f'Bearer {os.getenv("API_KEY")}'
|
51 |
+
}
|
52 |
+
|
53 |
+
payload = {
|
54 |
+
'messages': [{
|
55 |
+
'role': 'system',
|
56 |
+
'content': f'{description}',
|
57 |
+
'image': image_base64
|
58 |
+
}],
|
59 |
+
'max_tokens': 10000,
|
60 |
+
'model': os.getenv("MODEL")
|
61 |
+
}
|
62 |
+
|
63 |
+
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
|
64 |
+
data = json.loads(response.text)
|
65 |
+
|
66 |
+
if 'choices' in data and len(data['choices']) > 0:
|
67 |
+
command = data['choices'][0]['message']['content'].strip()
|
68 |
+
return command
|
69 |
+
elif 'error' in data:
|
70 |
+
error_message = data['error']['message']
|
71 |
+
return f'Ошибка: {error_message}'
|
72 |
+
else:
|
73 |
+
return f'Не удалось сгенерировать текст. {data}'
|
74 |
+
|
75 |
+
iface = gr.Interface(fn=generate, inputs=[
|
76 |
+
gr.Textbox(label="Запрос"),
|
77 |
+
gr.File(label="Изображение")
|
78 |
+
], outputs=gr.Textbox(label="Ответ"), title="GPT")
|
79 |
+
iface.launch()
|