File size: 1,557 Bytes
f92a51b
 
6eccccd
ec1e4af
5395fc0
f92a51b
814439a
d8abe74
6eccccd
23a6746
d8abe74
c8178ad
6eccccd
d278191
814439a
 
d8abe74
c8178ad
5395fc0
 
 
2ed3994
5395fc0
 
 
 
 
 
 
 
 
 
e5955c1
ba1200f
 
 
86c9f85
ba1200f
 
 
 
 
 
1
2
3
4
5
6
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
36
37
38
39
40
41
42
43
44
import gradio as gr
import requests
import json
import os
import time

def generate(description, model, max_tokens):
    headers = {
        'Content-Type': 'application/json',
        'Authorization': f'Bearer {os.getenv("API_KEY")}'
    }

    payload = {
        'messages': [{'role': 'system', 'content': description}],
        'max_tokens': max_tokens,
        'model': model
    }

    while True:
        response = requests.post(os.getenv("BASE_URL"), headers=headers, json=payload)
        data = json.loads(response.text)

        if 'choices' in data and len(data['choices']) > 0:
            command = data['choices'][0]['message']['content'].strip()
            return command
        elif 'error' in data:
            error_message = data['error']['message']
            print(f'Ошибка: {error_message}')
            time.sleep(1)
        else:
            print(f'Не удалось сгенерировать текст. Повторная попытка...')
            time.sleep(1)
            
iface = gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label="Запрос", placeholder="Введите текст...", state="active" if gr.Textbox().data != "" else "disabled"),
        gr.Radio(show_label=True, label="Модель", interactive=True, choices=["gpt-3.5-turbo", "gpt-3.5-turbo-16k"], value="gpt-3.5-turbo"),
        gr.Slider(show_label=True, label="Максимум токенов", minimum=100, maximum=15000, value=10000, step=1)
    ],
    outputs=gr.Textbox(label="Ответ")
)
iface.launch()