Laylo18's picture
Update app.py
87220be verified
raw
history blame
1.71 kB
# Простейший пример анализа тональности текста
import gradio as gr
from transformers import pipeline
# Загружаем готовый pipeline с Hugging Face
translation_pipe = pipeline("translation_ru_to_uz", model="sarahai/nllb-ru-uz")
sentiment_pipe = pipeline(
"sentiment-analysis",
model="blanchefort/rubert-base-cased-sentiment"
)
def analyze_sentiment(text: str) -> str:
text = text.strip()
if not text:
return "Введите текст для анализа."
translation = translator_pipe(text)
result = sentiment_pipe(translation)[0]
label = result["label"]
score = result["score"]
# Перевод меток модели
if label.upper().startswith("NEG"):
label_ru = "Негативная тональность"
elif label.upper().startswith("POS"):
label_ru = "Позитивная тональность"
else:
label_ru = f"Тональность: {label}"
return f"{label_ru} (уверенность модели: {score:.2f})"
# Описание интерфейса
demo = gr.Interface(
fn=analyze_sentiment,
inputs=gr.Textbox(
lines=5,
label="Введите текст (желательно на русском)",
placeholder="Например: Мне нравится этот продукт!"
),
outputs=gr.Textbox(label="Результат анализа"),
title="Sentiment Demo",
description=(
"Пример простого приложения на Hugging Face.\n"
"Модель определяет тональность текста."
),
)
if name == "main":
demo.launch()