Spaces:
Sleeping
Sleeping
import gradio as gr | |
import requests | |
import json | |
import os | |
import markdown2 | |
# HTML-код для анимации загрузки | |
loading_html = """ | |
<div> | |
<style> | |
.lds-ring { | |
display: inline-block; | |
position: relative; | |
width: 80px; | |
height: 80px; | |
} | |
.lds-ring div { | |
box-sizing: border-box; | |
display: block; | |
position: absolute; | |
width: 64px; | |
height: 64px; | |
margin: 8px; | |
border: 8px solid #000; | |
border-radius: 50%; | |
animation: lds-ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite; | |
border-color: #000 transparent transparent transparent; | |
} | |
.lds-ring div:nth-child(1) { | |
animation-delay: -0.45s; | |
} | |
.lds-ring div:nth-child(2) { | |
animation-delay: -0.3s; | |
} | |
.lds-ring div:nth-child(3) { | |
animation-delay: -0.15s; | |
} | |
@keyframes lds-ring { | |
0% { | |
transform: rotate(0deg); | |
} | |
100% { | |
transform: rotate(360deg); | |
} | |
} | |
</style> | |
<div class="lds-ring"><div></div><div></div><div></div><div></div></div> | |
</div> | |
""" | |
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() | |
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") | |
# Добавляем анимацию загрузки через HTML | |
iface.launch(inline=True, examples=[ | |
["Пример запроса"] | |
], examples_per_page=1, enable_queue=True, layout="unaligned", server_port=os.getenv("PORT")) | |