File size: 1,654 Bytes
1a132bd
b1dd40a
1a132bd
53f98c5
 
 
 
 
 
 
39aba33
 
53f98c5
39aba33
c1dde7b
 
 
 
53f98c5
c1dde7b
53f98c5
 
 
 
c1dde7b
53f98c5
 
 
 
 
 
 
c1dde7b
53f98c5
c1dde7b
53f98c5
 
 
 
 
c1dde7b
 
 
5654015
c1dde7b
 
39aba33
1a132bd
53f98c5
 
 
c1dde7b
 
 
 
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
import gradio as gr
from transformers import pipeline

# Определяем язык текста
lang_detector = pipeline(
    "text-classification",
    model="papluca/xlm-roberta-base-language-detection"
)

# Многоязычный анализ тональности
sentiment_pipe = pipeline(
    "sentiment-analysis",
    model="nlptown/bert-base-multilingual-uncased-sentiment"
)

def analyze_sentiment(text: str) -> str:
    text = text.strip()
    if not text:
        return "Введите текст."

    # Определяем язык
    lang = lang_detector(text)[0]["label"]

    # Анализ тональности
    result = sentiment_pipe(text)[0]
    stars = int(result["label"][0])  # "4 stars" → 4

    # Конвертация в позитив/нейтрал/негатив
    if stars <= 2:
        sentiment = "НЕГАТИВНАЯ ТОНАЛЬНОСТЬ"
    elif stars == 3:
        sentiment = "НЕЙТРАЛЬНАЯ ТОНАЛЬНОСТЬ"
    else:
        sentiment = "ПОЗИТИВНАЯ ТОНАЛЬНОСТЬ"

    return (
        f"Язык определён: {lang}\n"
        f"Тональность: {sentiment}\n"
        f"Модель уверена: {result['score']:.2f}"
    )


demo = gr.Interface(
    fn=analyze_sentiment,
    inputs=gr.Textbox(
        lines=5,
        label="Введите текст (Русский / O'zbekcha / English)",
    ),
    outputs="text",
    title="Multilingual Sentiment (RU+UZ+EN)",
    description="Автоматическое определение языка + анализ тональности."
)

if __name__ == "__main__":
    demo.launch()