PartyPlus commited on
Commit
d66aaf7
1 Parent(s): 7e8ce10

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -0
app.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import requests
3
+ import json
4
+ import os
5
+
6
+ def generate(description, model, max_tokens):
7
+ headers = {
8
+ 'Content-Type': 'application/json',
9
+ 'Authorization': f'Bearer {os.getenv("API_KEY")}'
10
+ }
11
+
12
+ payload = {
13
+ 'messages': [{'role': 'system', 'content': f'{description}'}],
14
+ 'max_tokens': max_tokens,
15
+ 'model': model
16
+ }
17
+
18
+ response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
19
+ data = json.loads(response.text)
20
+
21
+ if 'choices' in data and len(data['choices']) > 0:
22
+ command = data['choices'][0]['message']['content'].strip()
23
+ return command
24
+ elif 'error' in data:
25
+ error_message = data['error']['message']
26
+ return f'Ошибка: {error_message}'
27
+ else:
28
+ return f'Не удалось сгенерировать текст. {data}'
29
+
30
+ iface = gr.Interface(fn=generate, inputs=[
31
+ gr.Textbox(label="Запрос"),
32
+ gr.Radio(show_label=True, label="Модель", interactive=True, choices=["gpt-3.5-turbo", "gpt-3.5-turbo-16k", "gpt-4"], value="gpt-3.5-turbo"),
33
+ gr.Slider(show_label=True, label="Максимум токенов", minimum=100, maximum=15000, value=10000, step=1)
34
+ ], outputs=gr.Textbox(label="Ответ"), title="EasyGPT")
35
+ iface.launch()