Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -4,33 +4,38 @@ import json
|
|
4 |
import os
|
5 |
|
6 |
def generate_minecraft_command(input_data):
|
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 |
iface = gr.Interface(
|
36 |
fn=generate_minecraft_command,
|
|
|
4 |
import os
|
5 |
|
6 |
def generate_minecraft_command(input_data):
|
7 |
+
try:
|
8 |
+
if isinstance(input_data, str):
|
9 |
+
description = input_data
|
10 |
+
else:
|
11 |
+
description = input_data.read()
|
12 |
|
13 |
+
headers = {
|
14 |
+
'Content-Type': 'application/json',
|
15 |
+
'Authorization': f'Bearer {os.getenv("API_KEY")}'
|
16 |
+
}
|
17 |
|
18 |
+
payload = {
|
19 |
+
'messages': [{'role': 'system', 'content': f'{description}'}],
|
20 |
+
'max_tokens': 10000,
|
21 |
+
'model': os.getenv("MODEL")
|
22 |
+
}
|
23 |
|
24 |
+
response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
|
25 |
+
response.raise_for_status() # Raises an HTTPError if the HTTP request returned an unsuccessful status code
|
26 |
|
27 |
+
data = json.loads(response.text)
|
28 |
+
|
29 |
+
if 'choices' in data and len(data['choices']) > 0:
|
30 |
+
command = data['choices'][0]['message']['content'].strip()
|
31 |
+
return command
|
32 |
+
elif 'error' in data:
|
33 |
+
error_message = data['error']['message']
|
34 |
+
return f'Ошибка: {error_message}'
|
35 |
+
else:
|
36 |
+
return f'Не удалось сгенерировать команду. {data}'
|
37 |
+
except Exception as e:
|
38 |
+
return f'Произошла ошибка: {str(e)}'
|
39 |
|
40 |
iface = gr.Interface(
|
41 |
fn=generate_minecraft_command,
|