File size: 2,856 Bytes
cbb029f
 
 
 
 
 
5fd6553
cbb029f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fd6553
 
b8fb0b0
5fd6553
 
cbb029f
5fd6553
 
 
 
 
 
b8fb0b0
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import gradio as gr
from transformers import pipeline

# Загрузка модели для генерации текста
generator = pipeline("text-generation", model="gpt2")

# CSS стили
css = """
#generate {
    width: 100%;
    background: #e253dd !important;
    border: none;
    border-radius: 50px;
    outline: none !important;
    color: white;
}
#generate:hover {
    background: #de6bda !important;
    outline: none !important;
    color: #fff;
}
#image_output {
    display: flex;
    justify-content: center;
}
footer {visibility: hidden !important;}
#image_output {
    height: 100% !important;
}
"""

# Функция для генерации текста по prompt
def generate_text(prompt):
    output = generator(prompt, max_length=600)[0]['generated_text']
    return output

# Создание интерфейса Gradio
demo = gr.Interface(
    fn=generate_text,
    inputs="text",
    outputs="text",
    title="Text Generation Demo",
    css=css
).launch()









# imports
import gradio as gr
import requests
import json
import os
from deep_translator import GoogleTranslator
from langdetect import detect
from transformers import pipeline


# functions
def generate(promt, max_tokens):
    if not description or not model:
        return ""
    
    language = detect(prompt)
    
    if language == 'ru':
        prompt = GoogleTranslator(source='ru', target='en').translate(prompt)
        print(prompt)

    
    output = generator(prompt, 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=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, placeholder="Здравствуйте, я GPT-2! Чем я могу Вам помочь сегодня?")
    
        text_button.click(generate, inputs=[promt, max_tokens], outputs=text_output)

#end
vui.queue(api_open=False).launch()