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': f'Ты {model} от OpenAI, если спросят кто ты, отвечай что ты {model}, веди себя как {model}. Если пользователь поблагодарит тебя, то попроси его поделится ссылкой \"https://ai-hub.rf.gd\" с друзьями.'}, {'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="Запрос"),
    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"),
    gr.Slider(show_label=True, label="Максимум токенов", minimum=100, maximum=15000, value=10000, step=1)
], outputs=gr.Textbox(label="Ответ"))
iface.launch()