Spaces:
Sleeping
Sleeping
# imports | |
import gradio as gr | |
import requests | |
import json | |
import os | |
from deep_translator import GoogleTranslator | |
from langdetect import detect | |
from transformers import pipeline | |
# Загрузка модели для генерации текста | |
generator = pipeline("text-generation", model="gpt2") | |
# functions | |
def generate(prompt, max_tokens): | |
if not prompt: | |
return "" | |
language = detect(prompt) | |
prompt = GoogleTranslator(source=language, target='en').translate(prompt) | |
print(prompt) | |
output = generator(prompt, max_length=max_tokens)[0]['generated_text'] | |
language = detect(output) | |
output = GoogleTranslator(source=language, target='ru').translate(output) | |
print(output) | |
return output | |
# css | |
css = """ | |
footer {visibility: hidden !important;} | |
""" | |
# ui | |
with gr.Blocks(css=css) 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(): | |
max_tokens = gr.Slider(show_label=True, label="Длина ответа", minimum=10, maximum=2500, value=100, 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, placeholder="Здравствуйте, я GPT-2! Чем я могу Вам помочь сегодня?") | |
text_button.click(generate, inputs=[promt, max_tokens], outputs=text_output) | |
#end | |
vui.queue(api_open=False).launch() | |