Spaces:
Runtime error
Runtime error
import gradio as gr | |
import requests | |
import json | |
import os | |
import numpy as np | |
from PIL import Image | |
from io import BytesIO | |
def generate_minecraft_command(description, image): | |
try: | |
headers = { | |
'Authorization': f'Bearer {os.getenv("API_KEY")}' | |
} | |
# Преобразуем изображение в формат bytes | |
image_bytes = BytesIO() | |
image.save(image_bytes, format='PNG') | |
image_data = image_bytes.getvalue() | |
files = {'image': ('image.png', image_data, 'image/png')} | |
data = { | |
'messages': [{'role': 'system', 'content': description}], | |
'max_tokens': 10000, | |
'model': os.getenv("MODEL") | |
} | |
response = requests.post(os.getenv("BASE_URL"), headers=headers, data=data, files=files) | |
response.raise_for_status() | |
data = json.loads(response.text) | |
if 'choices' in data and len(data['choices']) > 0: | |
command = data['choices'][0]['message']['content'].strip() | |
return command | |
elif 'error' in data: | |
error_message = data['error']['message'] | |
return f'Ошибка: {error_message}' | |
else: | |
return f'Не удалось сгенерировать команду. {data}' | |
except Exception as e: | |
return f'Произошла ошибка: {str(e)}' | |
iface = gr.Interface( | |
fn=generate_minecraft_command, | |
inputs=[ | |
gr.Textbox(label="Запрос"), | |
gr.Image(label="Изображение", type="pil") # Указываем Gradio использовать PIL для обработки изображения | |
], | |
outputs=gr.Textbox(label="Ответ"), | |
title="GPT" | |
) | |
iface.launch() | |