Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,13 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Загружаем модели для анализа тональности, суммаризации текста, генерации подписей к изображениям, ответов на
|
| 5 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 6 |
summarization_pipeline = pipeline("summarization")
|
| 7 |
image_captioning_pipeline = pipeline("image-to-text")
|
| 8 |
qa_pipeline = pipeline("question-answering")
|
| 9 |
translation_pipeline = pipeline("translation_en_to_ru", model="Helsinki-NLP/opus-mt-en-ru")
|
|
|
|
| 10 |
|
| 11 |
# Функция для анализа тональности текста
|
| 12 |
def analyze_sentiment(text):
|
|
@@ -33,6 +34,11 @@ def translate_text(text):
|
|
| 33 |
result = translation_pipeline(text)
|
| 34 |
return result[0]['translation_text']
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
# Примеры текстов для анализа тональности
|
| 37 |
sentiment_examples = [
|
| 38 |
"I love programming, it's so much fun!",
|
|
@@ -70,6 +76,15 @@ translation_examples = [
|
|
| 70 |
"The weather is beautiful today."
|
| 71 |
]
|
| 72 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
# Создаем интерфейс Gradio с вкладками
|
| 74 |
with gr.Blocks() as demo:
|
| 75 |
with gr.Tab("Sentiment Analysis"):
|
|
@@ -125,6 +140,16 @@ with gr.Blocks() as demo:
|
|
| 125 |
examples=translation_examples,
|
| 126 |
examples_per_page=2
|
| 127 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
# Запускаем интерфейс
|
| 130 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Загружаем модели для анализа тональности, суммаризации текста, генерации подписей к изображениям, ответов на вопросы, перевода текста и определения эмоций
|
| 5 |
sentiment_pipeline = pipeline("sentiment-analysis")
|
| 6 |
summarization_pipeline = pipeline("summarization")
|
| 7 |
image_captioning_pipeline = pipeline("image-to-text")
|
| 8 |
qa_pipeline = pipeline("question-answering")
|
| 9 |
translation_pipeline = pipeline("translation_en_to_ru", model="Helsinki-NLP/opus-mt-en-ru")
|
| 10 |
+
emotion_pipeline = pipeline("text-classification", model="bhadresh-savani/distilbert-base-uncased-emotion")
|
| 11 |
|
| 12 |
# Функция для анализа тональности текста
|
| 13 |
def analyze_sentiment(text):
|
|
|
|
| 34 |
result = translation_pipeline(text)
|
| 35 |
return result[0]['translation_text']
|
| 36 |
|
| 37 |
+
# Функция для определения эмоций
|
| 38 |
+
def detect_emotion(text):
|
| 39 |
+
result = emotion_pipeline(text)[0]
|
| 40 |
+
return f"Emotion: {result['label']}, Confidence: {result['score']:.4f}"
|
| 41 |
+
|
| 42 |
# Примеры текстов для анализа тональности
|
| 43 |
sentiment_examples = [
|
| 44 |
"I love programming, it's so much fun!",
|
|
|
|
| 76 |
"The weather is beautiful today."
|
| 77 |
]
|
| 78 |
|
| 79 |
+
# Примеры текстов для определения эмоций
|
| 80 |
+
emotion_examples = [
|
| 81 |
+
"I am so happy today!",
|
| 82 |
+
"I feel really sad about what happened.",
|
| 83 |
+
"This situation makes me angry.",
|
| 84 |
+
"I am scared of the dark.",
|
| 85 |
+
"I am surprised by the results."
|
| 86 |
+
]
|
| 87 |
+
|
| 88 |
# Создаем интерфейс Gradio с вкладками
|
| 89 |
with gr.Blocks() as demo:
|
| 90 |
with gr.Tab("Sentiment Analysis"):
|
|
|
|
| 140 |
examples=translation_examples,
|
| 141 |
examples_per_page=2
|
| 142 |
)
|
| 143 |
+
with gr.Tab("Emotion Detection"):
|
| 144 |
+
gr.Interface(
|
| 145 |
+
fn=detect_emotion,
|
| 146 |
+
inputs=gr.Textbox(lines=2, placeholder="Введите текст для определения эмоции..."),
|
| 147 |
+
outputs="text",
|
| 148 |
+
title="Определение эмоций",
|
| 149 |
+
description="Введите текст, чтобы определить эмоцию.",
|
| 150 |
+
examples=emotion_examples,
|
| 151 |
+
examples_per_page=2
|
| 152 |
+
)
|
| 153 |
|
| 154 |
# Запускаем интерфейс
|
| 155 |
demo.launch()
|