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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
filename="microsoft_Phi-4-mini-instruct-Q4_K_M.gguf",
|
| 13 |
-
n_ctx=2048, # lower = less RAM
|
| 14 |
-
n_threads=2,
|
| 15 |
-
verbose=False
|
| 16 |
-
)
|
| 17 |
-
return llm
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
def chat(message, history):
|
| 21 |
-
model = load_model()
|
| 22 |
-
|
| 23 |
-
prompt = f"""<|im_start|>user
|
| 24 |
-
{message}<|im_end|>
|
| 25 |
-
<|im_start|>assistant
|
| 26 |
-
"""
|
| 27 |
-
|
| 28 |
-
output = model(
|
| 29 |
-
prompt,
|
| 30 |
-
max_tokens=512,
|
| 31 |
-
temperature=0.7,
|
| 32 |
-
stop=["<|im_end|>"],
|
| 33 |
-
stream=True
|
| 34 |
)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
for chunk in output:
|
| 38 |
-
response += chunk["choices"][0]["text"]
|
| 39 |
-
yield response
|
| 40 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
chat,
|
| 44 |
-
title="🧠 Phi-4 Mini (CPU Stable Space)"
|
| 45 |
-
).launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from llama_cpp import Llama
|
| 3 |
+
import os
|
| 4 |
+
|
| 5 |
+
# ============================================================
|
| 6 |
+
# تحميل النموذج
|
| 7 |
+
# ============================================================
|
| 8 |
+
MODEL_REPO = "DavidAU/Qwen2.5-MOE-2X1.5B-DeepSeek-Uncensored-Censored-4B-gguf"
|
| 9 |
+
MODEL_FILE = "Qwen2.5-MOE-2X1.5B-DeepSeek-Uncensored-Censored-4B-Q4_K_M.gguf"
|
| 10 |
+
|
| 11 |
+
print("⏳ جاري تحميل النموذج...")
|
| 12 |
+
|
| 13 |
+
llm = Llama.from_pretrained(
|
| 14 |
+
repo_id=MODEL_REPO,
|
| 15 |
+
filename=MODEL_FILE,
|
| 16 |
+
n_ctx=8192, # حجم السياق
|
| 17 |
+
n_threads=4, # عدد خيوط المعالجة
|
| 18 |
+
n_gpu_layers=-1, # استخدام GPU إذا كان متوفراً (-1 = كل الطبقات)
|
| 19 |
+
verbose=False,
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
print("✅ تم تحميل النموذج بنجاح!")
|
| 23 |
+
|
| 24 |
+
# ============================================================
|
| 25 |
+
# دالة التوليد
|
| 26 |
+
# ============================================================
|
| 27 |
+
def chat(
|
| 28 |
+
message,
|
| 29 |
+
history,
|
| 30 |
+
system_prompt,
|
| 31 |
+
max_tokens,
|
| 32 |
+
temperature,
|
| 33 |
+
top_p,
|
| 34 |
+
repeat_penalty,
|
| 35 |
+
):
|
| 36 |
+
# بناء المحادثة بصيغة ChatML
|
| 37 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 38 |
+
|
| 39 |
+
for user_msg, assistant_msg in history:
|
| 40 |
+
messages.append({"role": "user", "content": user_msg})
|
| 41 |
+
if assistant_msg:
|
| 42 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 43 |
+
|
| 44 |
+
messages.append({"role": "user", "content": message})
|
| 45 |
+
|
| 46 |
+
# توليد الرد
|
| 47 |
+
response = llm.create_chat_completion(
|
| 48 |
+
messages=messages,
|
| 49 |
+
max_tokens=int(max_tokens),
|
| 50 |
+
temperature=float(temperature),
|
| 51 |
+
top_p=float(top_p),
|
| 52 |
+
repeat_penalty=float(repeat_penalty),
|
| 53 |
+
stream=True,
|
| 54 |
+
)
|
| 55 |
+
|
| 56 |
+
partial = ""
|
| 57 |
+
for chunk in response:
|
| 58 |
+
delta = chunk["choices"][0]["delta"].get("content", "")
|
| 59 |
+
partial += delta
|
| 60 |
+
yield partial
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# ============================================================
|
| 64 |
+
# واجهة Gradio
|
| 65 |
+
# ============================================================
|
| 66 |
+
with gr.Blocks(
|
| 67 |
+
title="Qwen2.5 MOE 4B - DeepSeek",
|
| 68 |
+
theme=gr.themes.Soft(primary_hue="violet"),
|
| 69 |
+
css="""
|
| 70 |
+
.gradio-container { max-width: 900px !important; margin: auto; }
|
| 71 |
+
.title-box { text-align: center; padding: 20px; }
|
| 72 |
+
footer { display: none !important; }
|
| 73 |
+
""",
|
| 74 |
+
) as demo:
|
| 75 |
+
|
| 76 |
+
gr.HTML("""
|
| 77 |
+
<div class='title-box'>
|
| 78 |
+
<h1>🧠 Qwen2.5 MOE 2×1.5B DeepSeek</h1>
|
| 79 |
+
<p style='color: #666; font-size: 14px;'>
|
| 80 |
+
نموذج مزيج من الخبراء — نموذجان 1.5B يعملان معاً كنموذج 4B
|
| 81 |
+
</p>
|
| 82 |
+
<p style='color: #888; font-size: 12px;'>
|
| 83 |
+
DavidAU/Qwen2.5-MOE-2X1.5B-DeepSeek-Uncensored-Censored-4B
|
| 84 |
+
</p>
|
| 85 |
+
</div>
|
| 86 |
+
""")
|
| 87 |
+
|
| 88 |
+
with gr.Row():
|
| 89 |
+
with gr.Column(scale=3):
|
| 90 |
+
chatbot = gr.Chatbot(
|
| 91 |
+
label="المحادثة",
|
| 92 |
+
height=500,
|
| 93 |
+
show_label=True,
|
| 94 |
+
bubble_full_width=False,
|
| 95 |
+
)
|
| 96 |
+
with gr.Row():
|
| 97 |
+
msg = gr.Textbox(
|
| 98 |
+
placeholder="اكتب رسالتك هنا...",
|
| 99 |
+
label="رسالتك",
|
| 100 |
+
scale=4,
|
| 101 |
+
lines=2,
|
| 102 |
+
)
|
| 103 |
+
send_btn = gr.Button("إرسال 🚀", scale=1, variant="primary")
|
| 104 |
+
|
| 105 |
+
clear_btn = gr.Button("مسح المحادثة 🗑️", variant="secondary")
|
| 106 |
+
|
| 107 |
+
with gr.Column(scale=1):
|
| 108 |
+
gr.Markdown("### ⚙️ الإعدادات")
|
| 109 |
+
|
| 110 |
+
system_prompt = gr.Textbox(
|
| 111 |
+
value="You are a helpful, creative, and intelligent assistant. Think carefully before answering.",
|
| 112 |
+
label="System Prompt",
|
| 113 |
+
lines=4,
|
| 114 |
+
)
|
| 115 |
+
max_tokens = gr.Slider(
|
| 116 |
+
minimum=64,
|
| 117 |
+
maximum=4096,
|
| 118 |
+
value=1024,
|
| 119 |
+
step=64,
|
| 120 |
+
label="Max Tokens (أقصى طول للرد)",
|
| 121 |
+
)
|
| 122 |
+
temperature = gr.Slider(
|
| 123 |
+
minimum=0.1,
|
| 124 |
+
maximum=2.0,
|
| 125 |
+
value=0.7,
|
| 126 |
+
step=0.05,
|
| 127 |
+
label="Temperature (الإبداع)",
|
| 128 |
+
info="0.4-0.8 للتفكير المنطقي | 1.0+ للإبداع",
|
| 129 |
+
)
|
| 130 |
+
top_p = gr.Slider(
|
| 131 |
+
minimum=0.1,
|
| 132 |
+
maximum=1.0,
|
| 133 |
+
value=0.95,
|
| 134 |
+
step=0.05,
|
| 135 |
+
label="Top P",
|
| 136 |
+
)
|
| 137 |
+
repeat_penalty = gr.Slider(
|
| 138 |
+
minimum=1.0,
|
| 139 |
+
maximum=1.5,
|
| 140 |
+
value=1.06,
|
| 141 |
+
step=0.01,
|
| 142 |
+
label="Repeat Penalty (تجنب التكرار)",
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
gr.Markdown("""
|
| 146 |
+
### 💡 نصائح
|
| 147 |
+
- 🔵 **Temperature 0.4-0.8** → للتفكير والمنطق
|
| 148 |
+
- 🟣 **Temperature 1.0+** → للإبداع والقصص
|
| 149 |
+
- 📝 كن تفصيلياً في أسئلتك
|
| 150 |
+
- 🔄 جرب 2-4 مرات للحصول على أفضل نتيجة
|
| 151 |
+
""")
|
| 152 |
+
|
| 153 |
+
# ============================================================
|
| 154 |
+
# منطق الأحداث
|
| 155 |
+
# ============================================================
|
| 156 |
+
def user_submit(user_message, history):
|
| 157 |
+
return "", history + [[user_message, None]]
|
| 158 |
+
|
| 159 |
+
def bot_respond(history, system_prompt, max_tokens, temperature, top_p, repeat_penalty):
|
| 160 |
+
if not history or history[-1][1] is not None:
|
| 161 |
+
return history
|
| 162 |
+
|
| 163 |
+
user_message = history[-1][0]
|
| 164 |
+
history_without_last = history[:-1]
|
| 165 |
+
|
| 166 |
+
for partial_response in chat(
|
| 167 |
+
user_message,
|
| 168 |
+
history_without_last,
|
| 169 |
+
system_prompt,
|
| 170 |
+
max_tokens,
|
| 171 |
+
temperature,
|
| 172 |
+
top_p,
|
| 173 |
+
repeat_penalty,
|
| 174 |
+
):
|
| 175 |
+
history[-1][1] = partial_response
|
| 176 |
+
yield history
|
| 177 |
+
|
| 178 |
+
msg.submit(
|
| 179 |
+
user_submit,
|
| 180 |
+
[msg, chatbot],
|
| 181 |
+
[msg, chatbot],
|
| 182 |
+
).then(
|
| 183 |
+
bot_respond,
|
| 184 |
+
[chatbot, system_prompt, max_tokens, temperature, top_p, repeat_penalty],
|
| 185 |
+
chatbot,
|
| 186 |
+
)
|
| 187 |
|
| 188 |
+
send_btn.click(
|
| 189 |
+
user_submit,
|
| 190 |
+
[msg, chatbot],
|
| 191 |
+
[msg, chatbot],
|
| 192 |
+
).then(
|
| 193 |
+
bot_respond,
|
| 194 |
+
[chatbot, system_prompt, max_tokens, temperature, top_p, repeat_penalty],
|
| 195 |
+
chatbot,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
)
|
| 197 |
|
| 198 |
+
clear_btn.click(lambda: [], None, chatbot)
|
|
|
|
|
|
|
|
|
|
| 199 |
|
| 200 |
+
gr.Markdown("""
|
| 201 |
+
---
|
| 202 |
+
<div style='text-align:center; color: #aaa; font-size: 12px;'>
|
| 203 |
+
النموذج: <b>DavidAU/Qwen2.5-MOE-2X1.5B-DeepSeek-Uncensored-Censored-4B</b> |
|
| 204 |
+
الصيغة: GGUF Q4_K_M | السياق: 8K
|
| 205 |
+
</div>
|
| 206 |
+
""")
|
| 207 |
|
| 208 |
+
demo.launch()
|
|
|
|
|
|
|
|
|