Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,7 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
| 4 |
-
# ─── تحميل النموذج GGUF ───────────────────────────────────────────
|
| 5 |
print("⏳ جاري تحميل النموذج...")
|
| 6 |
-
|
| 7 |
llm = Llama.from_pretrained(
|
| 8 |
repo_id="rahimdzx/AraCode-7B-GGUF",
|
| 9 |
filename="aracode-7b.Q4_K_M.gguf",
|
|
@@ -12,7 +10,6 @@ llm = Llama.from_pretrained(
|
|
| 12 |
n_gpu_layers=0,
|
| 13 |
verbose=False,
|
| 14 |
)
|
| 15 |
-
|
| 16 |
print("✅ تم تحميل النموذج!")
|
| 17 |
|
| 18 |
SYSTEM = """أنت AraCode، مساعد برمجي ذكي متخصص في اللغة العربية.
|
|
@@ -21,11 +18,10 @@ SYSTEM = """أنت AraCode، مساعد برمجي ذكي متخصص في الل
|
|
| 21 |
|
| 22 |
|
| 23 |
def chat(message, history, temperature, max_tokens):
|
|
|
|
| 24 |
messages = [{"role": "system", "content": SYSTEM}]
|
| 25 |
-
for
|
| 26 |
-
messages.append({"role": "
|
| 27 |
-
if a:
|
| 28 |
-
messages.append({"role": "assistant", "content": a})
|
| 29 |
messages.append({"role": "user", "content": message})
|
| 30 |
|
| 31 |
partial = ""
|
|
@@ -51,7 +47,7 @@ with gr.Blocks(title="AraCode-7B", theme=gr.themes.Soft()) as demo:
|
|
| 51 |
<div style='text-align:center; padding:15px;'>
|
| 52 |
<h1>🐪 AraCode-7B</h1>
|
| 53 |
<p style='color:#555; font-size:16px;'>أول مساعد برمجي ذكي متخصص باللغة العربية</p>
|
| 54 |
-
<div style='display:flex; justify-content:center; gap:8px; flex-wrap:wrap;
|
| 55 |
<span style='background:#e8f4fd; padding:4px 10px; border-radius:15px; font-size:13px;'>✅ يشرح الكود</span>
|
| 56 |
<span style='background:#e8f4fd; padding:4px 10px; border-radius:15px; font-size:13px;'>✅ يولد كود</span>
|
| 57 |
<span style='background:#e8f4fd; padding:4px 10px; border-radius:15px; font-size:13px;'>✅ يصحح الأخطاء</span>
|
|
@@ -63,13 +59,15 @@ with gr.Blocks(title="AraCode-7B", theme=gr.themes.Soft()) as demo:
|
|
| 63 |
with gr.Row():
|
| 64 |
with gr.Column(scale=4):
|
| 65 |
chatbot = gr.Chatbot(
|
| 66 |
-
height=480,
|
| 67 |
show_copy_button=True,
|
| 68 |
avatar_images=("👤", "🐪"),
|
|
|
|
|
|
|
| 69 |
)
|
| 70 |
msg = gr.Textbox(
|
| 71 |
placeholder="اكتب سؤالك البرمجي هنا...",
|
| 72 |
-
label="سؤالك", lines=2,
|
| 73 |
)
|
| 74 |
with gr.Row():
|
| 75 |
send = gr.Button("إرسال 🚀", variant="primary")
|
|
@@ -84,7 +82,7 @@ with gr.Blocks(title="AraCode-7B", theme=gr.themes.Soft()) as demo:
|
|
| 84 |
gr.Markdown("""
|
| 85 |
### 📊 النموذج
|
| 86 |
- **AraCode-7B Q4_K_M**
|
| 87 |
-
- عربي + إنجليزي
|
| 88 |
- Apache 2.0
|
| 89 |
|
| 90 |
🔗 [النموذج](https://huggingface.co/rahimdzx/AraCode-7B-GGUF)
|
|
@@ -92,24 +90,26 @@ with gr.Blocks(title="AraCode-7B", theme=gr.themes.Soft()) as demo:
|
|
| 92 |
""")
|
| 93 |
|
| 94 |
gr.Examples([
|
| 95 |
-
["اكتب دالة بايثون للبحث الثنائي مع شرح
|
| 96 |
["ما الفرق بين list و tuple في بايثون؟"],
|
| 97 |
-
["اكتب API بسيط بـ Flask
|
| 98 |
-
["اشرح مفهوم Recursion بمثال
|
| 99 |
["كيف أصحح: IndexError: list index out of range"],
|
| 100 |
], inputs=msg)
|
| 101 |
|
| 102 |
def submit(message, history):
|
| 103 |
if not message.strip():
|
| 104 |
return "", history
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
def respond(history, temp, tokens):
|
| 108 |
if not history:
|
| 109 |
return history
|
| 110 |
-
history[-1][
|
| 111 |
-
|
| 112 |
-
|
|
|
|
| 113 |
yield history
|
| 114 |
|
| 115 |
msg.submit(submit, [msg, chatbot], [msg, chatbot]).then(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
|
|
|
| 4 |
print("⏳ جاري تحميل النموذج...")
|
|
|
|
| 5 |
llm = Llama.from_pretrained(
|
| 6 |
repo_id="rahimdzx/AraCode-7B-GGUF",
|
| 7 |
filename="aracode-7b.Q4_K_M.gguf",
|
|
|
|
| 10 |
n_gpu_layers=0,
|
| 11 |
verbose=False,
|
| 12 |
)
|
|
|
|
| 13 |
print("✅ تم تحميل النموذج!")
|
| 14 |
|
| 15 |
SYSTEM = """أنت AraCode، مساعد برمجي ذكي متخصص في اللغة العربية.
|
|
|
|
| 18 |
|
| 19 |
|
| 20 |
def chat(message, history, temperature, max_tokens):
|
| 21 |
+
# تحويل history من gradio 6 (list of dicts) لـ messages
|
| 22 |
messages = [{"role": "system", "content": SYSTEM}]
|
| 23 |
+
for item in history:
|
| 24 |
+
messages.append({"role": item["role"], "content": item["content"]})
|
|
|
|
|
|
|
| 25 |
messages.append({"role": "user", "content": message})
|
| 26 |
|
| 27 |
partial = ""
|
|
|
|
| 47 |
<div style='text-align:center; padding:15px;'>
|
| 48 |
<h1>🐪 AraCode-7B</h1>
|
| 49 |
<p style='color:#555; font-size:16px;'>أول مساعد برمجي ذكي متخصص باللغة العربية</p>
|
| 50 |
+
<div style='display:flex; justify-content:center; gap:8px; flex-wrap:wrap;'>
|
| 51 |
<span style='background:#e8f4fd; padding:4px 10px; border-radius:15px; font-size:13px;'>✅ يشرح الكود</span>
|
| 52 |
<span style='background:#e8f4fd; padding:4px 10px; border-radius:15px; font-size:13px;'>✅ يولد كود</span>
|
| 53 |
<span style='background:#e8f4fd; padding:4px 10px; border-radius:15px; font-size:13px;'>✅ يصحح الأخطاء</span>
|
|
|
|
| 59 |
with gr.Row():
|
| 60 |
with gr.Column(scale=4):
|
| 61 |
chatbot = gr.Chatbot(
|
| 62 |
+
height=480,
|
| 63 |
show_copy_button=True,
|
| 64 |
avatar_images=("👤", "🐪"),
|
| 65 |
+
type="messages", # gradio 6 يستخدم type="messages"
|
| 66 |
+
rtl=True,
|
| 67 |
)
|
| 68 |
msg = gr.Textbox(
|
| 69 |
placeholder="اكتب سؤالك البرمجي هنا...",
|
| 70 |
+
label="سؤالك", lines=2,
|
| 71 |
)
|
| 72 |
with gr.Row():
|
| 73 |
send = gr.Button("إرسال 🚀", variant="primary")
|
|
|
|
| 82 |
gr.Markdown("""
|
| 83 |
### 📊 النموذج
|
| 84 |
- **AraCode-7B Q4_K_M**
|
| 85 |
+
- عربي + إنجليزي
|
| 86 |
- Apache 2.0
|
| 87 |
|
| 88 |
🔗 [النموذج](https://huggingface.co/rahimdzx/AraCode-7B-GGUF)
|
|
|
|
| 90 |
""")
|
| 91 |
|
| 92 |
gr.Examples([
|
| 93 |
+
["اكتب دالة بايثون للبحث الثنائي مع شرح"],
|
| 94 |
["ما الفرق بين list و tuple في بايثون؟"],
|
| 95 |
+
["اكتب API بسيط بـ Flask"],
|
| 96 |
+
["اشرح مفهوم Recursion بمثال"],
|
| 97 |
["كيف أصحح: IndexError: list index out of range"],
|
| 98 |
], inputs=msg)
|
| 99 |
|
| 100 |
def submit(message, history):
|
| 101 |
if not message.strip():
|
| 102 |
return "", history
|
| 103 |
+
history = history + [{"role": "user", "content": message}]
|
| 104 |
+
return "", history
|
| 105 |
|
| 106 |
def respond(history, temp, tokens):
|
| 107 |
if not history:
|
| 108 |
return history
|
| 109 |
+
user_msg = history[-1]["content"]
|
| 110 |
+
history = history + [{"role": "assistant", "content": ""}]
|
| 111 |
+
for chunk in chat(user_msg, history[:-1], temp, tokens):
|
| 112 |
+
history[-1]["content"] = chunk
|
| 113 |
yield history
|
| 114 |
|
| 115 |
msg.submit(submit, [msg, chatbot], [msg, chatbot]).then(
|