File size: 1,902 Bytes
9cecf78
 
 
 
 
 
 
 
 
905dc49
 
9cecf78
 
ddaba32
 
9cecf78
 
 
 
1d314e4
 
9cecf78
 
93df5ef
9cecf78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1d314e4
9cecf78
 
 
 
 
 
 
 
 
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
# 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()