Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,25 +4,66 @@ import json
|
|
4 |
import os
|
5 |
import markdown2
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def generate_minecraft_command(description):
|
|
|
8 |
headers = {
|
9 |
'Content-Type': 'application/json',
|
10 |
'Authorization': f'Bearer {os.getenv("API_KEY")}'
|
11 |
}
|
12 |
-
|
13 |
payload = {
|
14 |
'messages': [{'role': 'system', 'content': f'{description}'}],
|
15 |
'max_tokens': 10000,
|
16 |
'model': os.getenv("MODEL")
|
17 |
}
|
18 |
-
|
19 |
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
|
20 |
data = json.loads(response.text)
|
21 |
|
22 |
if 'choices' in data and len(data['choices']) > 0:
|
23 |
command = data['choices'][0]['message']['content'].strip()
|
24 |
-
|
25 |
-
# Преобразование Markdown в HTML
|
26 |
command_html = markdown2.markdown(command)
|
27 |
return command_html
|
28 |
elif 'error' in data:
|
@@ -33,5 +74,9 @@ def generate_minecraft_command(description):
|
|
33 |
|
34 |
iface = gr.Interface(fn=generate_minecraft_command, inputs=[
|
35 |
gr.Textbox(label="Запрос")
|
36 |
-
], outputs=gr.HTML(label="Ответ"
|
37 |
-
|
|
|
|
|
|
|
|
|
|
4 |
import os
|
5 |
import markdown2
|
6 |
|
7 |
+
# HTML-код для анимации загрузки
|
8 |
+
loading_html = """
|
9 |
+
<div>
|
10 |
+
<style>
|
11 |
+
.lds-ring {
|
12 |
+
display: inline-block;
|
13 |
+
position: relative;
|
14 |
+
width: 80px;
|
15 |
+
height: 80px;
|
16 |
+
}
|
17 |
+
.lds-ring div {
|
18 |
+
box-sizing: border-box;
|
19 |
+
display: block;
|
20 |
+
position: absolute;
|
21 |
+
width: 64px;
|
22 |
+
height: 64px;
|
23 |
+
margin: 8px;
|
24 |
+
border: 8px solid #000;
|
25 |
+
border-radius: 50%;
|
26 |
+
animation: lds-ring 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
|
27 |
+
border-color: #000 transparent transparent transparent;
|
28 |
+
}
|
29 |
+
.lds-ring div:nth-child(1) {
|
30 |
+
animation-delay: -0.45s;
|
31 |
+
}
|
32 |
+
.lds-ring div:nth-child(2) {
|
33 |
+
animation-delay: -0.3s;
|
34 |
+
}
|
35 |
+
.lds-ring div:nth-child(3) {
|
36 |
+
animation-delay: -0.15s;
|
37 |
+
}
|
38 |
+
@keyframes lds-ring {
|
39 |
+
0% {
|
40 |
+
transform: rotate(0deg);
|
41 |
+
}
|
42 |
+
100% {
|
43 |
+
transform: rotate(360deg);
|
44 |
+
}
|
45 |
+
}
|
46 |
+
</style>
|
47 |
+
<div class="lds-ring"><div></div><div></div><div></div><div></div></div>
|
48 |
+
</div>
|
49 |
+
"""
|
50 |
+
|
51 |
def generate_minecraft_command(description):
|
52 |
+
# Оставим это без изменений
|
53 |
headers = {
|
54 |
'Content-Type': 'application/json',
|
55 |
'Authorization': f'Bearer {os.getenv("API_KEY")}'
|
56 |
}
|
|
|
57 |
payload = {
|
58 |
'messages': [{'role': 'system', 'content': f'{description}'}],
|
59 |
'max_tokens': 10000,
|
60 |
'model': os.getenv("MODEL")
|
61 |
}
|
|
|
62 |
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
|
63 |
data = json.loads(response.text)
|
64 |
|
65 |
if 'choices' in data and len(data['choices']) > 0:
|
66 |
command = data['choices'][0]['message']['content'].strip()
|
|
|
|
|
67 |
command_html = markdown2.markdown(command)
|
68 |
return command_html
|
69 |
elif 'error' in data:
|
|
|
74 |
|
75 |
iface = gr.Interface(fn=generate_minecraft_command, inputs=[
|
76 |
gr.Textbox(label="Запрос")
|
77 |
+
], outputs=gr.HTML(label="Ответ"), title="GPT")
|
78 |
+
|
79 |
+
# Добавляем анимацию загрузки через HTML
|
80 |
+
iface.launch(inline=True, examples=[
|
81 |
+
["Пример запроса"]
|
82 |
+
], examples_per_page=1, enable_queue=True, layout="unaligned", server_port=os.getenv("PORT"))
|