Spaces:
Sleeping
Sleeping
Upload app.py with huggingface_hub
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 4 |
+
import torch
|
| 5 |
+
|
| 6 |
+
model_name = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 7 |
+
|
| 8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 9 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 10 |
+
model_name,
|
| 11 |
+
torch_dtype=torch.float16,
|
| 12 |
+
device_map="auto"
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
SYSTEM_PROMPT = """
|
| 16 |
+
أنت مساعد ذكي تتحدث العربية بطلاقة.
|
| 17 |
+
رد على المستخدم بالعربية الفصحى أو باللهجة السعودية حسب أسلوبه.
|
| 18 |
+
"""
|
| 19 |
+
|
| 20 |
+
def chat(prompt):
|
| 21 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 22 |
+
outputs = model.generate(**inputs, max_new_tokens=200)
|
| 23 |
+
return tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 24 |
+
|
| 25 |
+
def respond(message, history):
|
| 26 |
+
full_prompt = SYSTEM_PROMPT + "\n"
|
| 27 |
+
for user, bot in history:
|
| 28 |
+
full_prompt += f"User: {user}\nBot: {bot}\n"
|
| 29 |
+
full_prompt += f"User: {message}\nBot:"
|
| 30 |
+
reply = chat(full_prompt)
|
| 31 |
+
return reply
|
| 32 |
+
|
| 33 |
+
demo = gr.ChatInterface(respond, title="Arabic Chatbot")
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|