Spaces:
Runtime error
Runtime error
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() | |
processed_image = process_image(image_data) | |
headers = { | |
'Content-Type': 'application/json', | |
'Authorization': f'Bearer {os.getenv("API_KEY")}' | |
} | |
payload = { | |
'messages': [{'role': 'system', 'content': f'{description}'}], | |
'max_tokens': 10000, | |
'model': os.getenv("MODEL") | |
} | |
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['messages'].append({'role': 'user', 'content': f'data:image/png;base64,{img_str}'}) | |
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload) | |
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.File(label="Загрузить изображение", type="binary") | |
], | |
outputs=gr.Textbox(label="Ответ"), | |
title="GPT" | |
) | |
iface.launch() | |