Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,77 +1,35 @@
|
|
1 |
import gradio as gr
|
2 |
-
from PIL import Image
|
3 |
import requests
|
4 |
import json
|
5 |
import os
|
6 |
-
import io
|
7 |
-
import base64
|
8 |
|
9 |
-
def
|
10 |
try:
|
11 |
-
image = Image.open(io.BytesIO(image_data))
|
12 |
-
# Дополнительная обработка изображения, если требуется
|
13 |
-
processed_image = image
|
14 |
-
return processed_image
|
15 |
-
except Exception as e:
|
16 |
-
return None
|
17 |
-
|
18 |
-
def generate_minecraft_command(input_data, image_data=None):
|
19 |
-
try:
|
20 |
-
if isinstance(input_data, str):
|
21 |
-
description = input_data
|
22 |
-
else:
|
23 |
-
description = input_data.read()
|
24 |
-
|
25 |
headers = {
|
26 |
-
'Content-Type': 'application/json',
|
27 |
'Authorization': f'Bearer {os.getenv("API_KEY")}'
|
28 |
}
|
29 |
|
30 |
-
|
31 |
-
|
|
|
|
|
32 |
'max_tokens': 10000,
|
33 |
'model': os.getenv("MODEL")
|
34 |
}
|
35 |
|
36 |
-
|
37 |
-
|
38 |
-
data_text = json.loads(response_text.text)
|
39 |
-
|
40 |
-
processed_image = process_image(image_data)
|
41 |
-
|
42 |
-
if processed_image is not None:
|
43 |
-
# Конвертируем обработанное изображение в base64 и добавляем к запросу изображения
|
44 |
-
buffered = io.BytesIO()
|
45 |
-
processed_image.save(buffered, format="PNG")
|
46 |
-
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
|
47 |
-
|
48 |
-
payload_image = {
|
49 |
-
'messages': [{'role': 'user', 'content': f'data:image/png;base64,{img_str}'}],
|
50 |
-
'max_tokens': 10000,
|
51 |
-
'model': os.getenv("MODEL")
|
52 |
-
}
|
53 |
-
|
54 |
-
response_image = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload_image)
|
55 |
-
response_image.raise_for_status()
|
56 |
-
data_image = json.loads(response_image.text)
|
57 |
|
58 |
-
|
59 |
-
command = data_image['choices'][0]['message']['content'].strip()
|
60 |
-
return command
|
61 |
-
elif 'error' in data_image:
|
62 |
-
error_message = data_image['error']['message']
|
63 |
-
return f'Ошибка: {error_message}'
|
64 |
-
else:
|
65 |
-
return f'Не удалось сгенерировать команду. {data_image}'
|
66 |
|
67 |
-
|
68 |
-
command =
|
69 |
return command
|
70 |
-
elif 'error' in
|
71 |
-
error_message =
|
72 |
return f'Ошибка: {error_message}'
|
73 |
else:
|
74 |
-
return f'Не удалось сгенерировать команду. {
|
75 |
|
76 |
except Exception as e:
|
77 |
return f'Произошла ошибка: {str(e)}'
|
@@ -80,7 +38,7 @@ iface = gr.Interface(
|
|
80 |
fn=generate_minecraft_command,
|
81 |
inputs=[
|
82 |
gr.Textbox(label="Запрос"),
|
83 |
-
gr.
|
84 |
],
|
85 |
outputs=gr.Textbox(label="Ответ"),
|
86 |
title="GPT"
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import requests
|
3 |
import json
|
4 |
import os
|
|
|
|
|
5 |
|
6 |
+
def generate_minecraft_command(description, image):
|
7 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
headers = {
|
|
|
9 |
'Authorization': f'Bearer {os.getenv("API_KEY")}'
|
10 |
}
|
11 |
|
12 |
+
files = {'image': ('image.png', image.read(), 'image/png')}
|
13 |
+
|
14 |
+
data = {
|
15 |
+
'messages': [{'role': 'system', 'content': description}],
|
16 |
'max_tokens': 10000,
|
17 |
'model': os.getenv("MODEL")
|
18 |
}
|
19 |
|
20 |
+
response = requests.post(os.getenv("BASE_URL"), headers=headers, data=data, files=files)
|
21 |
+
response.raise_for_status()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
+
data = json.loads(response.text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
if 'choices' in data and len(data['choices']) > 0:
|
26 |
+
command = data['choices'][0]['message']['content'].strip()
|
27 |
return command
|
28 |
+
elif 'error' in data:
|
29 |
+
error_message = data['error']['message']
|
30 |
return f'Ошибка: {error_message}'
|
31 |
else:
|
32 |
+
return f'Не удалось сгенерировать команду. {data}'
|
33 |
|
34 |
except Exception as e:
|
35 |
return f'Произошла ошибка: {str(e)}'
|
|
|
38 |
fn=generate_minecraft_command,
|
39 |
inputs=[
|
40 |
gr.Textbox(label="Запрос"),
|
41 |
+
gr.Image(label="Изображение")
|
42 |
],
|
43 |
outputs=gr.Textbox(label="Ответ"),
|
44 |
title="GPT"
|