Student-gpt / app.py
Rooni's picture
Update app.py
c0e5dbf
raw
history blame
1.58 kB
import gradio as gr
import requests
import json
import os
def generate_minecraft_command(input_data):
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 = {
'messages': [{'role': 'system', 'content': f'{description}'}],
'max_tokens': 10000,
'model': os.getenv("MODEL")
}
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
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()