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