Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,36 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 4 |
-
import traceback
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
| 7 |
MODEL_NAME = "kawkabelaloom/astramindx"
|
| 8 |
-
|
| 9 |
SYSTEM_PROMPT = "أنت مساعد عربي ذكي، تجيب بوضوح وبأسلوب بسيط ومفيد."
|
| 10 |
|
|
|
|
|
|
|
|
|
|
| 11 |
print("🔄 Loading tokenizer...")
|
| 12 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 13 |
MODEL_NAME,
|
| 14 |
trust_remote_code=True
|
| 15 |
)
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
MODEL_NAME,
|
| 20 |
-
device_map=
|
| 21 |
-
torch_dtype=torch.float32,
|
| 22 |
-
low_cpu_mem_usage=
|
| 23 |
-
trust_remote_code=True
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
|
|
|
| 27 |
|
|
|
|
|
|
|
|
|
|
| 28 |
generator = pipeline(
|
| 29 |
"text-generation",
|
| 30 |
model=model,
|
| 31 |
tokenizer=tokenizer
|
| 32 |
)
|
| 33 |
|
|
|
|
|
|
|
|
|
|
| 34 |
def build_prompt(history, user_message):
|
| 35 |
prompt = SYSTEM_PROMPT + "\n\n"
|
| 36 |
for user, bot in history:
|
|
@@ -38,13 +53,14 @@ def build_prompt(history, user_message):
|
|
| 38 |
prompt += f"المستخدم: {user_message}\nالمساعد:"
|
| 39 |
return prompt
|
| 40 |
|
|
|
|
| 41 |
def chat(user_message, history):
|
| 42 |
try:
|
| 43 |
prompt = build_prompt(history, user_message)
|
| 44 |
|
| 45 |
output = generator(
|
| 46 |
prompt,
|
| 47 |
-
max_new_tokens=128,
|
| 48 |
temperature=0.7,
|
| 49 |
top_p=0.9,
|
| 50 |
do_sample=True
|
|
@@ -55,13 +71,16 @@ def chat(user_message, history):
|
|
| 55 |
return history, ""
|
| 56 |
|
| 57 |
except Exception as e:
|
| 58 |
-
history.append((user_message, str(e)))
|
| 59 |
return history, ""
|
| 60 |
|
|
|
|
|
|
|
|
|
|
| 61 |
with gr.Blocks() as demo:
|
| 62 |
gr.Markdown("# 🤖 Astramindx Chatbot")
|
| 63 |
chatbot = gr.Chatbot(height=450)
|
| 64 |
-
msg = gr.Textbox()
|
| 65 |
msg.submit(chat, [msg, chatbot], [chatbot, msg])
|
| 66 |
|
| 67 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
|
|
|
| 4 |
|
| 5 |
+
# =========================
|
| 6 |
+
# CONFIG
|
| 7 |
+
# =========================
|
| 8 |
MODEL_NAME = "kawkabelaloom/astramindx"
|
|
|
|
| 9 |
SYSTEM_PROMPT = "أنت مساعد عربي ذكي، تجيب بوضوح وبأسلوب بسيط ومفيد."
|
| 10 |
|
| 11 |
+
# =========================
|
| 12 |
+
# LOAD TOKENIZER
|
| 13 |
+
# =========================
|
| 14 |
print("🔄 Loading tokenizer...")
|
| 15 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 16 |
MODEL_NAME,
|
| 17 |
trust_remote_code=True
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# =========================
|
| 21 |
+
# LOAD MODEL (FORCE CPU – NO QUANTIZATION)
|
| 22 |
+
# =========================
|
| 23 |
+
print("🔄 Loading model (CPU, no quantization)...")
|
| 24 |
+
|
| 25 |
model = AutoModelForCausalLM.from_pretrained(
|
| 26 |
MODEL_NAME,
|
| 27 |
+
device_map=None, # ❌ لا device_map auto
|
| 28 |
+
torch_dtype=torch.float32, # CPU safe
|
| 29 |
+
low_cpu_mem_usage=False, # مهم
|
| 30 |
+
trust_remote_code=True,
|
| 31 |
+
quantization_config=None # 🔥 الحل الأساسي
|
| 32 |
)
|
| 33 |
|
| 34 |
+
model.eval()
|
| 35 |
+
print("✅ Model loaded successfully")
|
| 36 |
|
| 37 |
+
# =========================
|
| 38 |
+
# PIPELINE
|
| 39 |
+
# =========================
|
| 40 |
generator = pipeline(
|
| 41 |
"text-generation",
|
| 42 |
model=model,
|
| 43 |
tokenizer=tokenizer
|
| 44 |
)
|
| 45 |
|
| 46 |
+
# =========================
|
| 47 |
+
# CHAT LOGIC
|
| 48 |
+
# =========================
|
| 49 |
def build_prompt(history, user_message):
|
| 50 |
prompt = SYSTEM_PROMPT + "\n\n"
|
| 51 |
for user, bot in history:
|
|
|
|
| 53 |
prompt += f"المستخدم: {user_message}\nالمساعد:"
|
| 54 |
return prompt
|
| 55 |
|
| 56 |
+
|
| 57 |
def chat(user_message, history):
|
| 58 |
try:
|
| 59 |
prompt = build_prompt(history, user_message)
|
| 60 |
|
| 61 |
output = generator(
|
| 62 |
prompt,
|
| 63 |
+
max_new_tokens=128,
|
| 64 |
temperature=0.7,
|
| 65 |
top_p=0.9,
|
| 66 |
do_sample=True
|
|
|
|
| 71 |
return history, ""
|
| 72 |
|
| 73 |
except Exception as e:
|
| 74 |
+
history.append((user_message, f"❌ Error: {str(e)}"))
|
| 75 |
return history, ""
|
| 76 |
|
| 77 |
+
# =========================
|
| 78 |
+
# GRADIO UI
|
| 79 |
+
# =========================
|
| 80 |
with gr.Blocks() as demo:
|
| 81 |
gr.Markdown("# 🤖 Astramindx Chatbot")
|
| 82 |
chatbot = gr.Chatbot(height=450)
|
| 83 |
+
msg = gr.Textbox(placeholder="اكتب سؤالك هنا...")
|
| 84 |
msg.submit(chat, [msg, chatbot], [chatbot, msg])
|
| 85 |
|
| 86 |
demo.launch()
|