Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,27 +1,84 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
-
def validate_hf_token(token):
|
| 4 |
"""
|
| 5 |
-
التحقق من صحة
|
| 6 |
"""
|
| 7 |
-
if not token:
|
| 8 |
-
return False, "
|
|
|
|
| 9 |
try:
|
| 10 |
api = HfApi(token=token)
|
| 11 |
user_info = api.whoami()
|
| 12 |
-
|
|
|
|
| 13 |
except Exception as e:
|
| 14 |
-
return False, f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
def format_chat_history(chat_history):
|
| 17 |
"""
|
| 18 |
-
تنسيق
|
|
|
|
| 19 |
"""
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
for message in chat_history:
|
| 22 |
-
role = message.get(
|
| 23 |
-
content = message.get(
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from huggingface_hub import HfApi
|
| 3 |
|
| 4 |
+
def validate_hf_token(token: str):
|
| 5 |
"""
|
| 6 |
+
التحقق من صحة توكن المستخدم وجلب بيانات الحساب لضمان صلاحية الاستهلاك.
|
| 7 |
"""
|
| 8 |
+
if not token or not token.startswith("hf_"):
|
| 9 |
+
return False, "⚠️ عذراً، يجب إدخال Hugging Face Token صحيح يبدأ بـ hf_"
|
| 10 |
+
|
| 11 |
try:
|
| 12 |
api = HfApi(token=token)
|
| 13 |
user_info = api.whoami()
|
| 14 |
+
username = user_info.get('name', 'User')
|
| 15 |
+
return True, f"✅ تم الاتصال بنجاح. مرحباً كاتبنا المبدع: {username}"
|
| 16 |
except Exception as e:
|
| 17 |
+
return False, f"❌ خطأ في التحقق من الرمز: {str(e)}"
|
| 18 |
+
|
| 19 |
+
def clean_ai_output(text: str):
|
| 20 |
+
"""
|
| 21 |
+
تنظيف مخرجات النماذج من العلامات الزائدة أو النصوص التقنية
|
| 22 |
+
التي قد تظهر أثناء توليد النص الروائي.
|
| 23 |
+
"""
|
| 24 |
+
if not text:
|
| 25 |
+
return ""
|
| 26 |
+
|
| 27 |
+
# إزالة علامات التفكير أو الوسوم التقنية إن وجدت
|
| 28 |
+
text = re.sub(r'<\|thought\|>.*?<\|/thought\|>', '', text, flags=re.DOTALL)
|
| 29 |
+
text = re.sub(r'\[SYSTEM_NOTE\].*?\[/SYSTEM_NOTE\]', '', text, flags=re.DOTALL)
|
| 30 |
+
|
| 31 |
+
return text.strip()
|
| 32 |
|
| 33 |
def format_chat_history(chat_history):
|
| 34 |
"""
|
| 35 |
+
تنسيق المحادثة الجماعية بين الوكلاء الـ 9 لعرضها في واجهة Gradio.
|
| 36 |
+
يتم استخدام Markdown لتمييز الأدوار.
|
| 37 |
"""
|
| 38 |
+
if not chat_history:
|
| 39 |
+
return "جاري بدء النقاش بين الوكلاء..."
|
| 40 |
+
|
| 41 |
+
formatted_output = ""
|
| 42 |
+
|
| 43 |
+
# أيقونات مخصصة لكل وكيل لتمييزهم بصرياً
|
| 44 |
+
icons = {
|
| 45 |
+
"Analyst": "🔍",
|
| 46 |
+
"Style_Guardian": "🛡️",
|
| 47 |
+
"Architect": "📐",
|
| 48 |
+
"Draft_Writer": "✍️",
|
| 49 |
+
"Humanizer": "🎭",
|
| 50 |
+
"Continuity_Guard": "⏳",
|
| 51 |
+
"Psychologist": "🧠",
|
| 52 |
+
"Critic": "⚖️",
|
| 53 |
+
"Editor_In_Chief": "👑"
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
for message in chat_history:
|
| 57 |
+
role = message.get("name", "Unknown")
|
| 58 |
+
content = message.get("content", "")
|
| 59 |
+
|
| 60 |
+
if not content:
|
| 61 |
+
continue
|
| 62 |
+
|
| 63 |
+
icon = icons.get(role, "🤖")
|
| 64 |
+
|
| 65 |
+
# تنسيق مخصص لكل رسالة
|
| 66 |
+
formatted_output += f"### {icon} {role}\n"
|
| 67 |
+
formatted_output += f"{content}\n\n"
|
| 68 |
+
formatted_output += "---\n\n"
|
| 69 |
+
|
| 70 |
+
return formatted_output
|
| 71 |
+
|
| 72 |
+
def extract_final_story(chat_history):
|
| 73 |
+
"""
|
| 74 |
+
محاولة استخراج النص الروائي النهائي المدمج من رسالة Editor-in-Chief الأخيرة.
|
| 75 |
+
"""
|
| 76 |
+
if not chat_history:
|
| 77 |
+
return "لم يتم توليد نص بعد."
|
| 78 |
+
|
| 79 |
+
# نبحث عن آخر رسالة من رئيس التحرير أو آخر رسالة في الحوار
|
| 80 |
+
for message in reversed(chat_history):
|
| 81 |
+
if message.get("name") == "Editor_In_Chief" or message.get("role") == "assistant":
|
| 82 |
+
return clean_ai_output(message.get("content", ""))
|
| 83 |
+
|
| 84 |
+
return "فشل استخراج النص النهائي."
|