Spaces:
Sleeping
Sleeping
File size: 1,596 Bytes
f92a51b 6eccccd ec1e4af c7ba344 b2b3945 f92a51b fde6e17 d8abe74 6eccccd 23a6746 d8abe74 c8178ad 6eccccd b2592e7 b2b3945 01868c2 d8abe74 c8178ad ec1e4af 6eccccd 2ed3994 6eccccd 18f89c2 c8178ad 715e65d b2b3945 d74acb3 2ed3994 715e65d 10954e8 715e65d 10954e8 adc769b fde6e17 018ca34 c8178ad |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
import gradio as gr
import requests
import json
import os
import markdown2
from pygments import highlight
from pygments.lexers import get_lexer_by_name
from pygments.formatters import HtmlFormatter
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)
# Подсветка синтаксиса кода
lexer = get_lexer_by_name("html", stripall=True)
formatter = HtmlFormatter(style="colorful", full=True, noclasses=True)
highlighted_code = highlight(command, lexer, formatter)
return highlighted_code
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.Code(label="Ответ", language="html", live=True), title="GPT")
iface.launch()
|