import gradio as gr from transformers import pipeline # Загружаем модели для анализа тональности и суммаризации текста sentiment_pipeline = pipeline("sentiment-analysis") summarization_pipeline = pipeline("summarization") # Функция для анализа тональности текста def analyze_sentiment(text): result = sentiment_pipeline(text)[0] return f"Label: {result['label']}, Confidence: {result['score']:.4f}" # Функция для суммаризации текста def summarize_text(text): result = summarization_pipeline(text, max_length=50, min_length=25, do_sample=False) return result[0]['summary_text'] # Примеры текстов для анализа тональности sentiment_examples = [ "I love programming, it's so much fun!", "This movie was terrible, I hated it.", "The weather is nice today.", "I feel so frustrated with this project.", "Gradio is an amazing tool for building ML demos!" ] # Примеры текстов для суммаризации summarization_examples = [ "Gradio is a powerful tool for building machine learning demos. It allows developers to quickly create interactive interfaces for their models.", "The weather today is sunny with a slight breeze. It's a perfect day to go outside and enjoy nature.", "Artificial intelligence is transforming industries by automating tasks and providing insights from large datasets." ] # Создаем интерфейс Gradio с вкладками with gr.Blocks() as demo: with gr.Tab("Sentiment Analysis"): gr.Interface( fn=analyze_sentiment, inputs=gr.Textbox(lines=2, placeholder="Введите текст для анализа тональности..."), outputs="text", title="Анализ тональности текста", description="Введите текст, чтобы определить его тональность.", examples=sentiment_examples, examples_per_page=5 ) with gr.Tab("Text Summarization"): gr.Interface( fn=summarize_text, inputs=gr.Textbox(lines=5, placeholder="Введите текст для суммаризации..."), outputs="text", title="Суммаризация текста", description="Введите текст, чтобы получить его краткое содержание.", examples=summarization_examples, examples_per_page=3 ) # Запускаем интерфейс demo.launch()