Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import json | |
import os | |
import markdown2 | |
def generate_minecraft_command(description): | |
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) | |
data = json.loads(response.text) | |
if 'choices' in data and len(data['choices']) > 0: | |
command = data['choices'][0]['message']['content'].strip() | |
# Преобразование Markdown в HTML | |
command_html = markdown2.markdown(command) | |
return command_html | |
elif 'error' in data: | |
error_message = data['error']['message'] | |
return f'Ошибка: {error_message}' | |
else: | |
return f'Не удалось сгенерировать команду. {data}' | |
iface = gr.Interface(fn=generate_minecraft_command, inputs=[ | |
gr.Textbox(label="Запрос") | |
], outputs=gr.HTML(label="Ответ"), title="GPT") # Используем gr.HTML вместо gr.Textbox | |
iface.launch() | |