Student-gpt / app.py
Rooni's picture
Update app.py
ccb8280
raw
history blame
3.25 kB
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()