Create utils.py
Browse files
utils.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import HfApi, login
|
| 2 |
+
|
| 3 |
+
def validate_hf_token(token):
|
| 4 |
+
"""
|
| 5 |
+
التحقق من صحة التوكن الخاص بالمستخدم
|
| 6 |
+
"""
|
| 7 |
+
if not token:
|
| 8 |
+
return False, "الرجاء إدخال رمز Hugging Face."
|
| 9 |
+
try:
|
| 10 |
+
api = HfApi(token=token)
|
| 11 |
+
user_info = api.whoami()
|
| 12 |
+
return True, f"تم تسجيل الدخول بنجاح كـ: {user_info['name']}"
|
| 13 |
+
except Exception as e:
|
| 14 |
+
return False, f"رمز غير صالح: {str(e)}"
|
| 15 |
+
|
| 16 |
+
def format_chat_history(chat_history):
|
| 17 |
+
"""
|
| 18 |
+
تنسيق سجل المحادثة للعرض في Gradio
|
| 19 |
+
"""
|
| 20 |
+
formatted_text = ""
|
| 21 |
+
for message in chat_history:
|
| 22 |
+
role = message.get('name', 'Unknown')
|
| 23 |
+
content = message.get('content', '')
|
| 24 |
+
if content:
|
| 25 |
+
formatted_text += f"**{role}:**\n{content}\n\n---\n\n"
|
| 26 |
+
return formatted_text
|
| 27 |
+
|