Spaces:
Runtime error
Runtime error
| import re | |
| import os | |
| import pymorphy3 | |
| from transformers import pipeline | |
| morph = pymorphy3.MorphAnalyzer() | |
| MODEL_PATH = "./" | |
| try: | |
| if os.path.exists(MODEL_PATH): | |
| classifier = pipeline("sentiment-analysis", model=MODEL_PATH, tokenizer=MODEL_PATH) | |
| else: | |
| classifier = None | |
| except: | |
| classifier = None | |
| def analyze_sentiment(texts): | |
| if not classifier: | |
| return ["Нейтрально"] * len(texts) | |
| results = classifier([str(t)[:512] for t in texts]) | |
| bad = ['ужас', 'обман', 'верните', 'плохо', 'дорого'] | |
| good = ['супер', 'класс', '🔥', '❤️'] | |
| final = [] | |
| for i, res in enumerate(results): | |
| text = texts[i].lower() | |
| if 'label_1' in res['label'].lower(): | |
| pred = 'Позитив' | |
| elif 'label_2' in res['label'].lower(): | |
| pred = 'Негатив' | |
| else: | |
| pred = 'Нейтрально' | |
| if any(w in text for w in bad): | |
| pred = 'Негатив' | |
| if pred == 'Нейтрально' and any(w in text for w in good): | |
| pred = 'Позитив' | |
| final.append(pred) | |
| return final | |
| def classify_theme(text): | |
| t = text.lower() | |
| if any(w in t for w in ['цена', 'билет', 'стоимость']): | |
| return 'Деньги' | |
| if any(w in t for w in ['когда', 'где']): | |
| return 'Логистика' | |
| return 'Общее' | |
| def calculate_priority_score(row): | |
| text = str(row.get('comment_text', '')).lower() | |
| score = 1.0 | |
| if len(text) > 80: | |
| score += 1 | |
| if '?' in text: | |
| score += 1 | |
| if any(w in text for w in ['цена', 'верните']): | |
| score += 2 | |
| return min(score, 5) | |
| def get_lemmas(text): | |
| """ | |
| Совместимость со старым app.py | |
| """ | |
| return text.lower().split() | |
| def analyze_sentiment_single(text): | |
| """ | |
| Обёртка для одного текста (для Streamlit/app.py) | |
| """ | |
| result = analyze_sentiment([text])[0] | |
| return result |