Spaces:
Sleeping
Sleeping
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() | |