Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,56 +1,31 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
-
import torch
|
| 4 |
-
|
| 5 |
-
# تحميل النموذج من صفحتك
|
| 6 |
-
model_name = "kawkabelaloom/astramind"
|
| 7 |
-
|
| 8 |
-
print("🚀 جاري تحميل النموذج...")
|
| 9 |
-
try:
|
| 10 |
-
# إنشاء pipeline للدردشة
|
| 11 |
-
generator = pipeline(
|
| 12 |
-
'text-generation',
|
| 13 |
-
model=model_name,
|
| 14 |
-
device=-1, # استخدام CPU
|
| 15 |
-
torch_dtype=torch.float32
|
| 16 |
-
)
|
| 17 |
-
print("✅ تم تحميل النموذج!")
|
| 18 |
-
except Exception as e:
|
| 19 |
-
print(f"❌ خطأ في التحميل: {e}")
|
| 20 |
-
generator = None
|
| 21 |
-
|
| 22 |
def chat(message, history):
|
| 23 |
-
"""دالة الدردشة"""
|
| 24 |
if generator is None:
|
| 25 |
return "النموذج غير متاح حالياً"
|
| 26 |
|
| 27 |
-
#
|
|
|
|
|
|
|
| 28 |
try:
|
|
|
|
| 29 |
result = generator(
|
| 30 |
-
|
| 31 |
-
|
| 32 |
temperature=0.7,
|
| 33 |
-
do_sample=True
|
|
|
|
| 34 |
)
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
except Exception as e:
|
| 37 |
-
return f"حدث خطأ: {str(e)}"
|
| 38 |
-
|
| 39 |
-
# واجهة Gradio
|
| 40 |
-
with gr.Blocks(title="Astramind Chatbot") as demo:
|
| 41 |
-
gr.Markdown("# 🤖 Astramind - روبوت الدردشة العربي")
|
| 42 |
-
gr.Markdown("نموذج ذكي للرد على استفساراتك باللغة العربية")
|
| 43 |
-
|
| 44 |
-
chatbot = gr.Chatbot(height=400)
|
| 45 |
-
msg = gr.Textbox(label="اكتب رسالتك هنا...")
|
| 46 |
-
clear = gr.Button("مسح المحادثة")
|
| 47 |
-
|
| 48 |
-
def respond(message, chat_history):
|
| 49 |
-
bot_message = chat(message, chat_history)
|
| 50 |
-
chat_history.append((message, bot_message))
|
| 51 |
-
return "", chat_history
|
| 52 |
-
|
| 53 |
-
msg.submit(respond, [msg, chatbot], [msg, chatbot])
|
| 54 |
-
clear.click(lambda: None, None, chatbot, queue=False)
|
| 55 |
-
|
| 56 |
-
demo.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
def chat(message, history):
|
| 2 |
+
"""دالة الدردشة المعدلة"""
|
| 3 |
if generator is None:
|
| 4 |
return "النموذج غير متاح حالياً"
|
| 5 |
|
| 6 |
+
# بناء رسالة بالصيغة الصحيحة (ChatML format)
|
| 7 |
+
formatted_message = f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n"
|
| 8 |
+
|
| 9 |
try:
|
| 10 |
+
# توليد الرد
|
| 11 |
result = generator(
|
| 12 |
+
formatted_message,
|
| 13 |
+
max_new_tokens=200,
|
| 14 |
temperature=0.7,
|
| 15 |
+
do_sample=True,
|
| 16 |
+
stop_strings=["<|im_end|>"]
|
| 17 |
)
|
| 18 |
+
|
| 19 |
+
# استخراج الرد فقط
|
| 20 |
+
response_text = result[0]['generated_text']
|
| 21 |
+
|
| 22 |
+
# إزالة prompt الأصلي
|
| 23 |
+
if formatted_message in response_text:
|
| 24 |
+
response_text = response_text.replace(formatted_message, "")
|
| 25 |
+
|
| 26 |
+
# إزالة أي tags متبقية
|
| 27 |
+
response_text = response_text.replace("<|im_end|>", "").strip()
|
| 28 |
+
|
| 29 |
+
return response_text
|
| 30 |
except Exception as e:
|
| 31 |
+
return f"حدث خطأ: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|