Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,132 +1,83 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
import
|
| 3 |
-
import
|
| 4 |
-
import json
|
| 5 |
|
| 6 |
# ===============================
|
| 7 |
-
#
|
| 8 |
# ===============================
|
| 9 |
-
MODEL_NAME =
|
| 10 |
-
HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
} if HF_TOKEN else {}
|
| 19 |
-
|
| 20 |
-
# ===============================
|
| 21 |
-
# 🤖 Chatbot
|
| 22 |
-
# ===============================
|
| 23 |
-
class AstramindChatbot:
|
| 24 |
-
def __init__(self):
|
| 25 |
-
self.history = []
|
| 26 |
-
self.system_prompt = (
|
| 27 |
-
"أنت مساعد عربي ذكي اسمك أسترا. "
|
| 28 |
-
"أجب بلغة عربية واضحة ومفيدة.\n\n"
|
| 29 |
-
)
|
| 30 |
-
|
| 31 |
-
def build_prompt(self, user_message):
|
| 32 |
-
prompt = self.system_prompt
|
| 33 |
-
for msg in self.history[-6:]:
|
| 34 |
-
if msg["role"] == "user":
|
| 35 |
-
prompt += f"المستخدم: {msg['content']}\n"
|
| 36 |
-
else:
|
| 37 |
-
prompt += f"المساعد: {msg['content']}\n"
|
| 38 |
-
prompt += f"المستخدم: {user_message}\nالمساعد:"
|
| 39 |
-
return prompt
|
| 40 |
-
|
| 41 |
-
def send_to_model(self, user_message, max_tokens, temperature):
|
| 42 |
-
try:
|
| 43 |
-
prompt = self.build_prompt(user_message)
|
| 44 |
-
|
| 45 |
-
payload = {
|
| 46 |
-
"inputs": prompt,
|
| 47 |
-
"parameters": {
|
| 48 |
-
"max_new_tokens": max_tokens,
|
| 49 |
-
"temperature": temperature,
|
| 50 |
-
"top_p": 0.9,
|
| 51 |
-
"do_sample": True,
|
| 52 |
-
"return_full_text": False
|
| 53 |
-
}
|
| 54 |
-
}
|
| 55 |
-
|
| 56 |
-
print("\n📤 PROMPT:\n", prompt)
|
| 57 |
-
print("\n📤 PAYLOAD:\n", json.dumps(payload, ensure_ascii=False, indent=2))
|
| 58 |
-
|
| 59 |
-
response = requests.post(
|
| 60 |
-
API_URL,
|
| 61 |
-
headers=headers,
|
| 62 |
-
json=payload,
|
| 63 |
-
timeout=60
|
| 64 |
-
)
|
| 65 |
-
|
| 66 |
-
print("📥 STATUS:", response.status_code)
|
| 67 |
-
print("📥 RAW:", response.text[:500])
|
| 68 |
-
|
| 69 |
-
if response.status_code == 200:
|
| 70 |
-
result = response.json()
|
| 71 |
-
|
| 72 |
-
if isinstance(result, list) and "generated_text" in result[0]:
|
| 73 |
-
answer = result[0]["generated_text"]
|
| 74 |
-
else:
|
| 75 |
-
answer = str(result)
|
| 76 |
-
|
| 77 |
-
self.history.append({"role": "user", "content": user_message})
|
| 78 |
-
self.history.append({"role": "assistant", "content": answer})
|
| 79 |
-
|
| 80 |
-
return answer.strip()
|
| 81 |
-
|
| 82 |
-
elif response.status_code == 503:
|
| 83 |
-
return "🔄 النموذج بيحمّل… حاول بعد دقيقة"
|
| 84 |
-
|
| 85 |
-
else:
|
| 86 |
-
return f"❌ API ERROR {response.status_code}\n{response.text}"
|
| 87 |
-
|
| 88 |
-
except Exception as e:
|
| 89 |
-
return f"❌ PYTHON ERROR\n{str(e)}"
|
| 90 |
-
|
| 91 |
-
def chat(self, msg, max_tokens, temp):
|
| 92 |
-
if not msg.strip():
|
| 93 |
-
return "⚠️ اكتب رسالة"
|
| 94 |
-
return self.send_to_model(msg, max_tokens, temp)
|
| 95 |
|
| 96 |
# ===============================
|
| 97 |
-
#
|
| 98 |
# ===============================
|
| 99 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
|
| 101 |
# ===============================
|
| 102 |
-
#
|
| 103 |
# ===============================
|
| 104 |
-
def respond(msg, history, tokens, temp):
|
| 105 |
-
history = history or []
|
| 106 |
-
reply = bot.chat(msg, tokens, temp)
|
| 107 |
-
history.append((msg, reply))
|
| 108 |
-
return history, "", "✅ تم التنفيذ"
|
| 109 |
-
|
| 110 |
-
def clear():
|
| 111 |
-
bot.history = []
|
| 112 |
-
return [], "🗑️ تم المسح"
|
| 113 |
-
|
| 114 |
with gr.Blocks(title="Astramind") as demo:
|
| 115 |
gr.Markdown("# 🤖 Astramind Chatbot")
|
|
|
|
| 116 |
|
| 117 |
-
|
| 118 |
-
chat_ui = gr.Chatbot(height=400)
|
| 119 |
|
| 120 |
with gr.Row():
|
| 121 |
msg = gr.Textbox(placeholder="اكتب رسالتك...", lines=2)
|
| 122 |
send = gr.Button("إرسال")
|
| 123 |
|
| 124 |
with gr.Accordion("⚙️ الإعدادات"):
|
| 125 |
-
tokens = gr.Slider(50, 500, 200, step=10)
|
| 126 |
-
temp = gr.Slider(0.1, 1.0, 0.7)
|
|
|
|
|
|
|
|
|
|
| 127 |
|
| 128 |
-
|
| 129 |
-
msg.submit(respond, [msg, chat_ui, tokens, temp], [chat_ui, msg, status])
|
| 130 |
-
gr.Button("مسح").click(clear, outputs=[chat_ui, status])
|
| 131 |
|
| 132 |
-
demo.launch(
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import torch
|
|
|
|
| 4 |
|
| 5 |
# ===============================
|
| 6 |
+
# 🤖 Load Model LOCALLY
|
| 7 |
# ===============================
|
| 8 |
+
MODEL_NAME = "kawkabelaloom/astramind"
|
|
|
|
| 9 |
|
| 10 |
+
generator = pipeline(
|
| 11 |
+
"text-generation",
|
| 12 |
+
model=MODEL_NAME,
|
| 13 |
+
torch_dtype=torch.float16 if torch.cuda.is_available() else torch.float32,
|
| 14 |
+
device=0 if torch.cuda.is_available() else -1
|
| 15 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
# ===============================
|
| 18 |
+
# 🧠 Chat Logic
|
| 19 |
# ===============================
|
| 20 |
+
history = []
|
| 21 |
+
|
| 22 |
+
SYSTEM_PROMPT = (
|
| 23 |
+
"أنت مساعد عربي ذكي اسمك أسترا. "
|
| 24 |
+
"أجب بلغة عربية واضحة ومفيدة.\n\n"
|
| 25 |
+
)
|
| 26 |
+
|
| 27 |
+
def build_prompt(user_message):
|
| 28 |
+
prompt = SYSTEM_PROMPT
|
| 29 |
+
for h in history[-6:]:
|
| 30 |
+
if h["role"] == "user":
|
| 31 |
+
prompt += f"المستخدم: {h['content']}\n"
|
| 32 |
+
else:
|
| 33 |
+
prompt += f"المساعد: {h['content']}\n"
|
| 34 |
+
prompt += f"المستخدم: {user_message}\nالمساعد:"
|
| 35 |
+
return prompt
|
| 36 |
+
|
| 37 |
+
def chat(user_message, chat_history, max_tokens, temperature):
|
| 38 |
+
if not user_message.strip():
|
| 39 |
+
return chat_history, ""
|
| 40 |
+
|
| 41 |
+
prompt = build_prompt(user_message)
|
| 42 |
+
|
| 43 |
+
output = generator(
|
| 44 |
+
prompt,
|
| 45 |
+
max_new_tokens=max_tokens,
|
| 46 |
+
temperature=temperature,
|
| 47 |
+
do_sample=True,
|
| 48 |
+
return_full_text=False
|
| 49 |
+
)[0]["generated_text"]
|
| 50 |
+
|
| 51 |
+
history.append({"role": "user", "content": user_message})
|
| 52 |
+
history.append({"role": "assistant", "content": output})
|
| 53 |
+
|
| 54 |
+
chat_history.append((user_message, output))
|
| 55 |
+
return chat_history, ""
|
| 56 |
+
|
| 57 |
+
def clear_chat():
|
| 58 |
+
history.clear()
|
| 59 |
+
return []
|
| 60 |
|
| 61 |
# ===============================
|
| 62 |
+
# 🖥️ Gradio UI
|
| 63 |
# ===============================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
with gr.Blocks(title="Astramind") as demo:
|
| 65 |
gr.Markdown("# 🤖 Astramind Chatbot")
|
| 66 |
+
gr.Markdown("تشغيل محلي داخل HuggingFace Space")
|
| 67 |
|
| 68 |
+
chatbot = gr.Chatbot(height=400)
|
|
|
|
| 69 |
|
| 70 |
with gr.Row():
|
| 71 |
msg = gr.Textbox(placeholder="اكتب رسالتك...", lines=2)
|
| 72 |
send = gr.Button("إرسال")
|
| 73 |
|
| 74 |
with gr.Accordion("⚙️ الإعدادات"):
|
| 75 |
+
tokens = gr.Slider(50, 500, 200, step=10, label="طول الرد")
|
| 76 |
+
temp = gr.Slider(0.1, 1.0, 0.7, step=0.1, label="الإبداع")
|
| 77 |
+
|
| 78 |
+
send.click(chat, [msg, chatbot, tokens, temp], [chatbot, msg])
|
| 79 |
+
msg.submit(chat, [msg, chatbot, tokens, temp], [chatbot, msg])
|
| 80 |
|
| 81 |
+
gr.Button("مسح المحادثة").click(clear_chat, outputs=chatbot)
|
|
|
|
|
|
|
| 82 |
|
| 83 |
+
demo.launch()
|