Rooni commited on
Commit
c0e5dbf
·
1 Parent(s): 771e484

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -23
app.py CHANGED
@@ -4,33 +4,38 @@ import json
4
  import os
5
 
6
  def generate_minecraft_command(input_data):
7
- if isinstance(input_data, str):
8
- description = input_data
9
- else:
10
- description = input_data.read()
 
11
 
12
- headers = {
13
- 'Content-Type': 'application/json',
14
- 'Authorization': f'Bearer {os.getenv("API_KEY")}'
15
- }
16
 
17
- payload = {
18
- 'messages': [{'role': 'system', 'content': f'{description}'}],
19
- 'max_tokens': 10000,
20
- 'model': os.getenv("MODEL")
21
- }
22
 
23
- response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
24
- data = json.loads(response.text)
25
 
26
- if 'choices' in data and len(data['choices']) > 0:
27
- command = data['choices'][0]['message']['content'].strip()
28
- return command
29
- elif 'error' in data:
30
- error_message = data['error']['message']
31
- return f'Ошибка: {error_message}'
32
- else:
33
- return f'Не удалось сгенерировать команду. {data}'
 
 
 
 
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,