Spaces:
Sleeping
Sleeping
File size: 2,764 Bytes
11a668a f92a51b 6eccccd ec1e4af 11a668a 814439a d8abe74 6eccccd 23a6746 d8abe74 c8178ad 6eccccd 11a668a 814439a d8abe74 c8178ad 5395fc0 2ed3994 5395fc0 11a668a 57c09a9 11a668a 730b245 11a668a 730b245 11a668a f3da532 11a668a 6106374 |
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# imports
import gradio as gr
import requests
import json
import os
# functions
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}, а если ты помнишь что ты не {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}')
else:
print(f'Не удалось сгенерировать текст. Повторная попытка...')
# css
css = """
footer {visibility: hidden !important;}
.gradio-tabs-header {display: none !important;}
"""
# ui
with gr.Blocks(css=css, title="UI") as vui:
with gr.Tabs() as tabs:
with gr.Row():
with gr.Tab("Запрос", id='request v'):
with gr.Row():
with gr.Column(scale=3):
promt = gr.Textbox(show_label=True, label="Запрос")
with gr.Tab("Настройки", id='settingsv'):
with gr.Row():
with gr.Column(scale=3):
with gr.Row():
model = 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")
with gr.Row():
max_tokens = gr.Slider(show_label=True, label="Максимум токенов", minimum=100, maximum=15000, value=5000, step=1)
with gr.Column():
text_button = gr.Button("Генерация", variant='primary', elem_id="generate")
with gr.Column(scale=2):
text_output = gr.Textbox(show_label=False)
text_button.click(generate, inputs=[promt, model, max_tokens], outputs=text_output)
#end
vui.queue(api_open=False).launch()
|