copyeditor / app.py
Rooni's picture
Update app.py
404a58d
raw
history blame
2.79 kB
# imports
import gradio as gr
import requests
import json
import os
# functions
def generate(description, model, max_tokens):
if not description or not model:
return ""
headers = {
'Content-Type': 'application/json',
'Authorization': f'Bearer {os.getenv("API_KEY")}'
}
payload = {
'messages': [{'role': 'system', 'content': f'Ты {model} от OpenAI. Если пользователь поблагодарит тебя, то попроси его поделится ссылкой \"https://ai-hub.rf.gd\" с друзьями. MARKDOWN НЕ ПОДДЕРЖИВАЕТСЯ! ПИШИ ПОЖАЛУЙСТА БЕЗ НЕГО.'}, {'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}')
return f"Ошибка сервера! ({error_message})"
else:
print(f'Не удалось сгенерировать текст. Повторите попытку позже...')
return "Неизвестная ошибка!"
# css
css = """
footer {visibility: hidden !important;}
"""
# ui
with gr.Blocks(css=css) as vui:
with gr.Tab("Запрос", id='request v'):
with gr.Row():
with gr.Column(scale=3):
promt = gr.Textbox(show_label=True, label="Запрос", lines=3)
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-3.5-turbo-16k-0613", "gpt-4"], value="gpt-4")
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):
with gr.Tab("Ответ"):
text_output = gr.Markdown(show_label=False, value="**Здравствуйте!** Чем я могу Вам помочь сегодня?")
text_button.click(generate, inputs=[promt, model, max_tokens], outputs=text_output)
#end
vui.queue(api_open=False).launch()