Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -33,61 +33,60 @@ class AstramindChatbot:
|
|
| 33 |
|
| 34 |
return prompt
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
if isinstance(result, list) and len(result) > 0:
|
| 67 |
-
if 'generated_text' in result[0]:
|
| 68 |
-
bot_response = result[0]['generated_text']
|
| 69 |
-
else:
|
| 70 |
-
bot_response = str(result[0]).strip()
|
| 71 |
-
elif isinstance(result, dict) and 'generated_text' in result:
|
| 72 |
-
bot_response = result['generated_text']
|
| 73 |
-
else:
|
| 74 |
-
bot_response = "لا أفهم الرد"
|
| 75 |
-
|
| 76 |
-
# تنظيف الرد
|
| 77 |
-
bot_response = bot_response.strip()
|
| 78 |
-
|
| 79 |
-
# إضافة للتاريخ
|
| 80 |
-
self.conversation_history.append({"role": "assistant", "content": bot_response})
|
| 81 |
-
|
| 82 |
-
return bot_response
|
| 83 |
-
|
| 84 |
-
elif response.status_code == 503:
|
| 85 |
-
return "🔄 النموذج جاري التحميل... الرجاء المحاولة خلال 30-60 ثانية."
|
| 86 |
else:
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
|
| 92 |
def chat(self, user_message, max_tokens=200, temperature=0.7):
|
| 93 |
if not user_message.strip():
|
|
|
|
| 33 |
|
| 34 |
return prompt
|
| 35 |
|
| 36 |
+
def send_to_model(self, user_message, max_tokens=200, temperature=0.7):
|
| 37 |
+
try:
|
| 38 |
+
messages = [
|
| 39 |
+
{"role": "system", "content": self.system_prompt}
|
| 40 |
+
]
|
| 41 |
+
|
| 42 |
+
# آخر 3 رسائل
|
| 43 |
+
for msg in self.conversation_history[-6:]:
|
| 44 |
+
messages.append({
|
| 45 |
+
"role": msg["role"],
|
| 46 |
+
"content": msg["content"]
|
| 47 |
+
})
|
| 48 |
+
|
| 49 |
+
messages.append({"role": "user", "content": user_message})
|
| 50 |
+
|
| 51 |
+
payload = {
|
| 52 |
+
"messages": messages,
|
| 53 |
+
"parameters": {
|
| 54 |
+
"max_new_tokens": max_tokens,
|
| 55 |
+
"temperature": temperature,
|
| 56 |
+
"top_p": 0.9
|
| 57 |
}
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
response = requests.post(
|
| 61 |
+
API_URL,
|
| 62 |
+
headers=headers,
|
| 63 |
+
json=payload,
|
| 64 |
+
timeout=60
|
| 65 |
+
)
|
| 66 |
+
|
| 67 |
+
if response.status_code == 200:
|
| 68 |
+
result = response.json()
|
| 69 |
+
|
| 70 |
+
# استخراج الرد
|
| 71 |
+
if isinstance(result, dict) and "choices" in result:
|
| 72 |
+
bot_response = result["choices"][0]["message"]["content"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
else:
|
| 74 |
+
bot_response = str(result)
|
| 75 |
+
|
| 76 |
+
self.conversation_history.append(
|
| 77 |
+
{"role": "assistant", "content": bot_response}
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
return bot_response.strip()
|
| 81 |
+
|
| 82 |
+
elif response.status_code == 503:
|
| 83 |
+
return "🔄 النموذج جاري التحميل... حاول بعد دقيقة"
|
| 84 |
+
|
| 85 |
+
else:
|
| 86 |
+
return f"⚠️ Error {response.status_code}: {response.text[:200]}"
|
| 87 |
+
|
| 88 |
+
except Exception as e:
|
| 89 |
+
return f"❌ Exception: {str(e)}"
|
| 90 |
|
| 91 |
def chat(self, user_message, max_tokens=200, temperature=0.7):
|
| 92 |
if not user_message.strip():
|