Spaces:
Runtime error
Runtime error
File size: 3,253 Bytes
e92393d 65c5950 e92393d df67f71 73bef9c e92393d 65c5950 df67f71 65c5950 c0e5dbf 66118cd c0e5dbf 00ccf46 ccb8280 c0e5dbf e92393d ccb8280 65c5950 ccb8280 df67f71 65c5950 ccb8280 00ccf46 ccb8280 c0e5dbf ccb8280 c0e5dbf ccb8280 c0e5dbf ccb8280 c0e5dbf 00ccf46 e92393d 00ccf46 757f1cf 00ccf46 771e484 757f1cf 00ccf46 e92393d |
1 2 3 4 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 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
import gradio as gr
from PIL import Image
import requests
import json
import os
import io
import base64
def process_image(image_data):
try:
image = Image.open(io.BytesIO(image_data))
# Дополнительная обработка изображения, если требуется
processed_image = image
return processed_image
except Exception as e:
return None
def generate_minecraft_command(input_data, image_data=None):
try:
if isinstance(input_data, str):
description = input_data
else:
description = input_data.read()
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("API_KEY")}'
}
payload_text = {
'messages': [{'role': 'system', 'content': f'{description}'}],
'max_tokens': 10000,
'model': os.getenv("MODEL")
}
response_text = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload_text)
response_text.raise_for_status()
data_text = json.loads(response_text.text)
processed_image = process_image(image_data)
if processed_image is not None:
# Конвертируем обработанное изображение в base64 и добавляем к запросу изображения
buffered = io.BytesIO()
processed_image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
payload_image = {
'messages': [{'role': 'user', 'content': f'data:image/png;base64,{img_str}'}],
'max_tokens': 10000,
'model': os.getenv("MODEL")
}
response_image = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload_image)
response_image.raise_for_status()
data_image = json.loads(response_image.text)
if 'choices' in data_image and len(data_image['choices']) > 0:
command = data_image['choices'][0]['message']['content'].strip()
return command
elif 'error' in data_image:
error_message = data_image['error']['message']
return f'Ошибка: {error_message}'
else:
return f'Не удалось сгенерировать команду. {data_image}'
elif 'choices' in data_text and len(data_text['choices']) > 0:
command = data_text['choices'][0]['message']['content'].strip()
return command
elif 'error' in data_text:
error_message = data_text['error']['message']
return f'Ошибка: {error_message}'
else:
return f'Не удалось сгенерировать команду. {data_text}'
except Exception as e:
return f'Произошла ошибка: {str(e)}'
iface = gr.Interface(
fn=generate_minecraft_command,
inputs=[
gr.Textbox(label="Запрос"),
gr.File(label="Загрузить изображение", type="binary")
],
outputs=gr.Textbox(label="Ответ"),
title="GPT"
)
iface.launch()
|