Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,6 @@ import gradio as gr
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import json
|
| 5 |
-
from datetime import datetime
|
| 6 |
|
| 7 |
# ===============================
|
| 8 |
# 🔧 الإعدادات
|
|
@@ -24,24 +23,28 @@ class AstramindChatbot:
|
|
| 24 |
self.conversation_history = []
|
| 25 |
self.system_prompt = (
|
| 26 |
"أنت مساعد عربي ذكي اسمك أسترا. "
|
| 27 |
-
"
|
| 28 |
)
|
| 29 |
|
| 30 |
def send_to_model(self, user_message, max_tokens=200, temperature=0.7):
|
| 31 |
try:
|
| 32 |
-
# =====
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
for msg in self.conversation_history[-6:]:
|
| 36 |
-
|
| 37 |
"role": msg["role"],
|
| 38 |
"content": msg["content"]
|
| 39 |
})
|
| 40 |
|
| 41 |
-
|
| 42 |
|
| 43 |
payload = {
|
| 44 |
-
"
|
| 45 |
"parameters": {
|
| 46 |
"max_new_tokens": max_tokens,
|
| 47 |
"temperature": temperature,
|
|
@@ -49,9 +52,11 @@ class AstramindChatbot:
|
|
| 49 |
}
|
| 50 |
}
|
| 51 |
|
| 52 |
-
# =====
|
|
|
|
|
|
|
| 53 |
print("\n==============================")
|
| 54 |
-
print("📤
|
| 55 |
print(json.dumps(payload, ensure_ascii=False, indent=2))
|
| 56 |
print("==============================")
|
| 57 |
|
|
@@ -62,18 +67,21 @@ class AstramindChatbot:
|
|
| 62 |
timeout=60
|
| 63 |
)
|
| 64 |
|
| 65 |
-
print(f"📥 STATUS
|
| 66 |
-
print(f"📥
|
| 67 |
|
| 68 |
-
# =====
|
|
|
|
|
|
|
| 69 |
if response.status_code == 200:
|
| 70 |
result = response.json()
|
| 71 |
|
| 72 |
-
#
|
| 73 |
-
if isinstance(result,
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
else:
|
| 78 |
bot_response = str(result)
|
| 79 |
|
|
@@ -83,11 +91,15 @@ class AstramindChatbot:
|
|
| 83 |
|
| 84 |
return bot_response.strip()
|
| 85 |
|
| 86 |
-
# =====
|
|
|
|
|
|
|
| 87 |
elif response.status_code == 503:
|
| 88 |
-
return "🔄 النموذج جاري التحميل… حاول
|
| 89 |
|
| 90 |
-
# =====
|
|
|
|
|
|
|
| 91 |
else:
|
| 92 |
return (
|
| 93 |
f"❌ API ERROR\n"
|
|
@@ -122,7 +134,7 @@ def respond(message, history, tokens, temp):
|
|
| 122 |
|
| 123 |
status = "✅ تم التنفيذ"
|
| 124 |
if response.startswith("❌"):
|
| 125 |
-
status = "🚨
|
| 126 |
|
| 127 |
return history, "", status
|
| 128 |
|
|
@@ -140,7 +152,7 @@ def welcome_message():
|
|
| 140 |
# ===============================
|
| 141 |
with gr.Blocks(title="Astramind Chatbot") as demo:
|
| 142 |
gr.Markdown("# 🤖 Astramind")
|
| 143 |
-
gr.Markdown("### مساعد دردشة عربي
|
| 144 |
|
| 145 |
status_box = gr.Textbox(
|
| 146 |
value=welcome_message(),
|
|
|
|
| 2 |
import requests
|
| 3 |
import os
|
| 4 |
import json
|
|
|
|
| 5 |
|
| 6 |
# ===============================
|
| 7 |
# 🔧 الإعدادات
|
|
|
|
| 23 |
self.conversation_history = []
|
| 24 |
self.system_prompt = (
|
| 25 |
"أنت مساعد عربي ذكي اسمك أسترا. "
|
| 26 |
+
"أجب بلغة عربية واضحة ومفيدة."
|
| 27 |
)
|
| 28 |
|
| 29 |
def send_to_model(self, user_message, max_tokens=200, temperature=0.7):
|
| 30 |
try:
|
| 31 |
+
# ===============================
|
| 32 |
+
# ✅ FORMAT CORRECT FOR HF
|
| 33 |
+
# ===============================
|
| 34 |
+
inputs = [
|
| 35 |
+
{"role": "system", "content": self.system_prompt}
|
| 36 |
+
]
|
| 37 |
|
| 38 |
for msg in self.conversation_history[-6:]:
|
| 39 |
+
inputs.append({
|
| 40 |
"role": msg["role"],
|
| 41 |
"content": msg["content"]
|
| 42 |
})
|
| 43 |
|
| 44 |
+
inputs.append({"role": "user", "content": user_message})
|
| 45 |
|
| 46 |
payload = {
|
| 47 |
+
"inputs": inputs,
|
| 48 |
"parameters": {
|
| 49 |
"max_new_tokens": max_tokens,
|
| 50 |
"temperature": temperature,
|
|
|
|
| 52 |
}
|
| 53 |
}
|
| 54 |
|
| 55 |
+
# ===============================
|
| 56 |
+
# 🐞 DEBUG LOGS
|
| 57 |
+
# ===============================
|
| 58 |
print("\n==============================")
|
| 59 |
+
print("📤 PAYLOAD:")
|
| 60 |
print(json.dumps(payload, ensure_ascii=False, indent=2))
|
| 61 |
print("==============================")
|
| 62 |
|
|
|
|
| 67 |
timeout=60
|
| 68 |
)
|
| 69 |
|
| 70 |
+
print(f"📥 STATUS: {response.status_code}")
|
| 71 |
+
print(f"📥 RESPONSE RAW: {response.text[:500]}")
|
| 72 |
|
| 73 |
+
# ===============================
|
| 74 |
+
# ✅ SUCCESS
|
| 75 |
+
# ===============================
|
| 76 |
if response.status_code == 200:
|
| 77 |
result = response.json()
|
| 78 |
|
| 79 |
+
# HF غالباً بيرجع List
|
| 80 |
+
if isinstance(result, list) and len(result) > 0:
|
| 81 |
+
if "generated_text" in result[0]:
|
| 82 |
+
bot_response = result[0]["generated_text"]
|
| 83 |
+
else:
|
| 84 |
+
bot_response = str(result[0])
|
| 85 |
else:
|
| 86 |
bot_response = str(result)
|
| 87 |
|
|
|
|
| 91 |
|
| 92 |
return bot_response.strip()
|
| 93 |
|
| 94 |
+
# ===============================
|
| 95 |
+
# ⏳ MODEL LOADING
|
| 96 |
+
# ===============================
|
| 97 |
elif response.status_code == 503:
|
| 98 |
+
return "🔄 النموذج جاري التحميل… حاول بعد دقيقة."
|
| 99 |
|
| 100 |
+
# ===============================
|
| 101 |
+
# ❌ API ERROR
|
| 102 |
+
# ===============================
|
| 103 |
else:
|
| 104 |
return (
|
| 105 |
f"❌ API ERROR\n"
|
|
|
|
| 134 |
|
| 135 |
status = "✅ تم التنفيذ"
|
| 136 |
if response.startswith("❌"):
|
| 137 |
+
status = "🚨 حدث خطأ — راجع الرسالة"
|
| 138 |
|
| 139 |
return history, "", status
|
| 140 |
|
|
|
|
| 152 |
# ===============================
|
| 153 |
with gr.Blocks(title="Astramind Chatbot") as demo:
|
| 154 |
gr.Markdown("# 🤖 Astramind")
|
| 155 |
+
gr.Markdown("### مساعد دردشة عربي")
|
| 156 |
|
| 157 |
status_box = gr.Textbox(
|
| 158 |
value=welcome_message(),
|