Rooni commited on
Commit
00ccf46
·
1 Parent(s): 78b61f3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -31
app.py CHANGED
@@ -3,44 +3,42 @@ import requests
3
  import json
4
  import os
5
 
6
- def gpt3_turbo(image=None, text=None):
 
 
 
 
7
 
8
- api_key = os.getenv("API_KEY")
9
- endpoint = os.getenv("BASE_URL")
10
-
11
- # Формируем данные для отправки запроса
12
- data = {}
13
- if image is not None:
14
- with open(image.name, "rb") as img_file:
15
- data['image'] = img_file.read()
16
- if text:
17
- data['text'] = text
18
-
19
- # Отправляем запрос на API GPT-3.5-turbo
20
  headers = {
21
  'Content-Type': 'application/json',
22
- 'Authorization': f'Bearer {api_key}'
 
 
 
 
 
 
23
  }
24
-
25
- try:
26
- response = requests.post(endpoint, headers=headers, json={"prompt": data, "max_tokens": 100, "model": 'gpt-3.5-turbo'})
27
- response.raise_for_status() # Проверка статуса ответа
28
- result = response.json()
29
- return result['choices'][0]['text']
30
- except requests.RequestException as e:
31
- return f"Ошибка при получении ответа от модели: {e}"
32
 
33
- # Создаем интерфейс Gradio
 
 
 
 
 
 
 
 
 
 
 
34
  iface = gr.Interface(
35
- fn=gpt3_turbo,
36
  inputs=[
37
- gr.Image(type="pil", label="Выберите изображение (необязательно)"),
38
- "text"
39
  ],
40
- outputs="text",
41
- title="GPT",
42
- description="Помощь ученикам, решение заданий"
43
  )
44
-
45
- # Запускаем интерфейс
46
  iface.launch()
 
3
  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,
37
  inputs=[
38
+ gr.Textbox(label="Запрос"),
39
+ gr.File(label="Загрузить изображение", type="file")
40
  ],
41
+ outputs=gr.Textbox(label="Ответ"),
42
+ title="GPT"
 
43
  )
 
 
44
  iface.launch()