Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
|
| 4 |
+
print("🧠 جاري تحميل دماغ فرانكشتاين...")
|
| 5 |
+
# تحميل النموذج
|
| 6 |
+
llm = Llama(
|
| 7 |
+
model_path="model-q4_k_m.gguf",
|
| 8 |
+
n_ctx=2048,
|
| 9 |
+
n_threads=2, # تحديد الأنوية لضمان استقرار الخادم المجاني
|
| 10 |
+
verbose=False
|
| 11 |
+
)
|
| 12 |
+
|
| 13 |
+
def generate_response(message, history):
|
| 14 |
+
# بناء السياق والذاكرة لتشبه نظام Qwen
|
| 15 |
+
prompt = ""
|
| 16 |
+
for user_msg, bot_msg in history:
|
| 17 |
+
prompt += f"<|im_start|>user\n{user_msg}<|im_end|>\n<|im_start|>assistant\n{bot_msg}<|im_end|>\n"
|
| 18 |
+
|
| 19 |
+
# إضافة السؤال الجديد مع الحقن الإجباري للتفكير
|
| 20 |
+
prompt += f"<|im_start|>user\n{message}<|im_end|>\n<|im_start|>assistant\n<|deep_think|>\n"
|
| 21 |
+
|
| 22 |
+
# التوليد مع خاصية التدفق (Streaming) لتبدو كالمحادثة الحقيقية
|
| 23 |
+
response = llm(
|
| 24 |
+
prompt,
|
| 25 |
+
max_tokens=512,
|
| 26 |
+
stop=["<|im_end|>"],
|
| 27 |
+
temperature=0.1,
|
| 28 |
+
stream=True # السر هنا!
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
output = ""
|
| 32 |
+
for chunk in response:
|
| 33 |
+
if "choices" in chunk and len(chunk["choices"]) > 0:
|
| 34 |
+
delta = chunk["choices"][0].get("text", "")
|
| 35 |
+
output += delta
|
| 36 |
+
yield output
|
| 37 |
+
|
| 38 |
+
# بناء الواجهة الرسومية
|
| 39 |
+
demo = gr.ChatInterface(
|
| 40 |
+
fn=generate_response,
|
| 41 |
+
title="🤖 Frankenstein Agent (1.5B)",
|
| 42 |
+
description="وكيلك الذكي (RAG + رياضيات + برمجة). تفضل بسؤاله أو إعطائه سياقاً!",
|
| 43 |
+
theme="soft",
|
| 44 |
+
submit_btn="إرسال 🚀",
|
| 45 |
+
retry_btn="إعادة المحاولة 🔄",
|
| 46 |
+
undo_btn="تراجع ↩️",
|
| 47 |
+
clear_btn="مسح المحادثة 🗑️",
|
| 48 |
+
)
|
| 49 |
+
|
| 50 |
+
if __name__ == "__main__":
|
| 51 |
+
# تشغيل الواجهة على المنفذ الصحيح لـ Hugging Face
|
| 52 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|