Spaces:
Running
Running
Upload utils.py with huggingface_hub
Browse files
utils.py
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
import time
|
| 3 |
+
|
| 4 |
+
def analyze_sentiment(text):
|
| 5 |
+
"""
|
| 6 |
+
Simulates the sentiment analysis model.
|
| 7 |
+
"""
|
| 8 |
+
positive_keywords = ['خوب', 'عالی', 'خوشحال', 'محشر', 'دوست', 'عشق', 'موفق', 'سالم', 'خنده', '🙂', '😊', '❤️', 'good', 'great', 'love']
|
| 9 |
+
negative_keywords = ['بد', 'غمگین', 'ترس', 'تنفر', 'مرگ', 'درد', 'خسته', 'مشکل', '😔', '😢', '😡', 'bad', 'sad', 'hate']
|
| 10 |
+
|
| 11 |
+
text_lower = text.lower()
|
| 12 |
+
|
| 13 |
+
pos_count = sum(1 for word in positive_keywords if word in text_lower)
|
| 14 |
+
neg_count = sum(1 for word in negative_keywords if word in text_lower)
|
| 15 |
+
|
| 16 |
+
if pos_count > neg_count:
|
| 17 |
+
label = "POSITIVE"
|
| 18 |
+
score = random.uniform(0.7, 0.99)
|
| 19 |
+
elif neg_count > pos_count:
|
| 20 |
+
label = "NEGATIVE"
|
| 21 |
+
score = random.uniform(0.7, 0.99)
|
| 22 |
+
else:
|
| 23 |
+
label = "NEUTRAL"
|
| 24 |
+
score = random.uniform(0.4, 0.6)
|
| 25 |
+
|
| 26 |
+
return {"label": label, "score": score}
|
| 27 |
+
|
| 28 |
+
def generate_ai_response(text):
|
| 29 |
+
"""Generates a response based on sentiment."""
|
| 30 |
+
result = analyze_sentiment(text)
|
| 31 |
+
label = result['label']
|
| 32 |
+
score = f"{result['score']*100:.1f}"
|
| 33 |
+
|
| 34 |
+
if label == "POSITIVE":
|
| 35 |
+
return f"😊 خوشحالم که حالتان خوب است! (اطمینان: {score}%)"
|
| 36 |
+
elif label == "NEGATIVE":
|
| 37 |
+
return f"😔 متاسفم که اینطور احساس میکنید. امیدوارم روزتان بهتر شود. (اطمینان: {score}%)"
|
| 38 |
+
else:
|
| 39 |
+
return f"🤔 پیام شما دریافت شد. (اطمینان: {score}%)"
|
| 40 |
+
|
| 41 |
+
def get_mock_rooms():
|
| 42 |
+
return [
|
| 43 |
+
{"id": 1, "name": "اتاق عمومی", "description": "گفتگوی آزاد"},
|
| 44 |
+
{"id": 2, "name": "گفتگوی فنی", "description": "بحث درباره برنامه نویسی"},
|
| 45 |
+
{"id": 3, "name": "پشتیبانی", "description": "سوالات و پاسخها"}
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
def get_mock_users():
|
| 49 |
+
return [
|
| 50 |
+
{"id": 1, "name": "علی رضایی", "online": True},
|
| 51 |
+
{"id": 2, "name": "سارا محمدی", "online": True},
|
| 52 |
+
{"id": 3, "name": "رضا کریمی", "online": False},
|
| 53 |
+
{"id": 4, "name": "مریم حسینی", "online": True},
|
| 54 |
+
{"id": 5, "name": "سیستم ادمین", "online": True},
|
| 55 |
+
]
|
| 56 |
+
|
| 57 |
+
def get_initial_messages():
|
| 58 |
+
return [
|
| 59 |
+
{
|
| 60 |
+
"id": 1,
|
| 61 |
+
"room_id": 1,
|
| 62 |
+
"user_id": "admin",
|
| 63 |
+
"username": "سیستم ادمین",
|
| 64 |
+
"text": "به چت روم خوش آمدید! لطفاً قوانین را رعایت کنید.",
|
| 65 |
+
"time": "10:00",
|
| 66 |
+
"is_deleted": False
|
| 67 |
+
},
|
| 68 |
+
{
|
| 69 |
+
"id": 2,
|
| 70 |
+
"room_id": 1,
|
| 71 |
+
"user_id": "user1",
|
| 72 |
+
"username": "علی رضایی",
|
| 73 |
+
"text": "سلام به همه! حالتون چطوره؟",
|
| 74 |
+
"time": "10:05",
|
| 75 |
+
"is_deleted": False
|
| 76 |
+
}
|
| 77 |
+
]
|