Rooni commited on
Commit
ccb8280
·
1 Parent(s): 73bef9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -22,39 +22,57 @@ def generate_minecraft_command(input_data, image_data=None):
22
  else:
23
  description = input_data.read()
24
 
25
- processed_image = process_image(image_data)
26
-
27
  headers = {
28
  'Content-Type': 'application/json',
29
  'Authorization': f'Bearer {os.getenv("API_KEY")}'
30
  }
31
 
32
- payload = {
33
  'messages': [{'role': 'system', 'content': f'{description}'}],
34
  'max_tokens': 10000,
35
  'model': os.getenv("MODEL")
36
  }
37
 
 
 
 
 
 
 
38
  if processed_image is not None:
39
- # Конвертируем обработанное изображение в base64 и добавляем к запросу
40
  buffered = io.BytesIO()
41
  processed_image.save(buffered, format="PNG")
42
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
43
- payload['messages'].append({'role': 'user', 'content': f'data:image/png;base64,{img_str}'})
44
 
45
- response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
46
- response.raise_for_status()
 
 
 
 
 
 
 
47
 
48
- data = json.loads(response.text)
 
 
 
 
 
 
 
49
 
50
- if 'choices' in data and len(data['choices']) > 0:
51
- command = data['choices'][0]['message']['content'].strip()
52
  return command
53
- elif 'error' in data:
54
- error_message = data['error']['message']
55
  return f'Ошибка: {error_message}'
56
  else:
57
- return f'Не удалось сгенерировать команду. {data}'
 
58
  except Exception as e:
59
  return f'Произошла ошибка: {str(e)}'
60
 
 
22
  else:
23
  description = input_data.read()
24
 
 
 
25
  headers = {
26
  'Content-Type': 'application/json',
27
  'Authorization': f'Bearer {os.getenv("API_KEY")}'
28
  }
29
 
30
+ payload_text = {
31
  'messages': [{'role': 'system', 'content': f'{description}'}],
32
  'max_tokens': 10000,
33
  'model': os.getenv("MODEL")
34
  }
35
 
36
+ response_text = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload_text)
37
+ response_text.raise_for_status()
38
+ data_text = json.loads(response_text.text)
39
+
40
+ processed_image = process_image(image_data)
41
+
42
  if processed_image is not None:
43
+ # Конвертируем обработанное изображение в base64 и добавляем к запросу изображения
44
  buffered = io.BytesIO()
45
  processed_image.save(buffered, format="PNG")
46
  img_str = base64.b64encode(buffered.getvalue()).decode('utf-8')
 
47
 
48
+ payload_image = {
49
+ 'messages': [{'role': 'user', 'content': f'data:image/png;base64,{img_str}'}],
50
+ 'max_tokens': 10000,
51
+ 'model': os.getenv("MODEL")
52
+ }
53
+
54
+ response_image = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload_image)
55
+ response_image.raise_for_status()
56
+ data_image = json.loads(response_image.text)
57
 
58
+ if 'choices' in data_image and len(data_image['choices']) > 0:
59
+ command = data_image['choices'][0]['message']['content'].strip()
60
+ return command
61
+ elif 'error' in data_image:
62
+ error_message = data_image['error']['message']
63
+ return f'Ошибка: {error_message}'
64
+ else:
65
+ return f'Не удалось сгенерировать команду. {data_image}'
66
 
67
+ elif 'choices' in data_text and len(data_text['choices']) > 0:
68
+ command = data_text['choices'][0]['message']['content'].strip()
69
  return command
70
+ elif 'error' in data_text:
71
+ error_message = data_text['error']['message']
72
  return f'Ошибка: {error_message}'
73
  else:
74
+ return f'Не удалось сгенерировать команду. {data_text}'
75
+
76
  except Exception as e:
77
  return f'Произошла ошибка: {str(e)}'
78