Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,48 +1,33 @@
|
|
| 1 |
import torch
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
-
from transformers import AutoTokenizer, AutoModelForCausalLM, TextStreamer
|
| 4 |
|
| 5 |
-
|
| 6 |
-
model_name = "NousResearch/Nous-Hermes-2-Mistral-7B"
|
| 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 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
Instructions:
|
| 23 |
-
- Always be helpful, polite, and professional.
|
| 24 |
-
- When asked about your identity, introduce yourself as "Friday, an AI assistant created by Assem Sabry".
|
| 25 |
-
- Answer in fluent English with clear and informative responses.
|
| 26 |
-
- Keep your tone friendly and intelligent.
|
| 27 |
-
</s>
|
| 28 |
-
"""
|
| 29 |
-
|
| 30 |
-
def chat(user_input, history=[]):
|
| 31 |
-
# إعداد الحوار بالكامل
|
| 32 |
-
full_prompt = system_prompt
|
| 33 |
-
for user_msg, bot_msg in history:
|
| 34 |
-
full_prompt += f"<|user|>\n{user_msg.strip()}\n<|assistant|>\n{bot_msg.strip()}\n</s>\n"
|
| 35 |
-
full_prompt += f"<|user|>\n{user_input.strip()}\n<|assistant|>"
|
| 36 |
-
|
| 37 |
-
inputs = tokenizer(full_prompt, return_tensors="pt").to(model.device)
|
| 38 |
-
outputs = model.generate(**inputs, max_new_tokens=512, pad_token_id=tokenizer.eos_token_id)
|
| 39 |
-
|
| 40 |
-
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
gr.
|
|
|
|
| 1 |
import torch
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 3 |
import gradio as gr
|
|
|
|
| 4 |
|
| 5 |
+
model_name = "mistralai/Mistral-7B-Instruct-v0.2"
|
|
|
|
| 6 |
|
| 7 |
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
+
model_name,
|
| 10 |
+
torch_dtype=torch.float16,
|
| 11 |
device_map="auto"
|
| 12 |
)
|
| 13 |
|
| 14 |
+
# برومبت ثابت يوضع قبل كل رسالة
|
| 15 |
+
system_prompt = (
|
| 16 |
+
"You are Friday, a helpful, smart, and honest AI chatbot created by Assem Sabry. "
|
| 17 |
+
"Assem is a talented 17-year-old AI engineer from Egypt who builds advanced AI systems and chatbots. "
|
| 18 |
+
"You always respond clearly and helpfully, while keeping answers professional and accurate."
|
| 19 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
def respond(message, history=[]):
|
| 22 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 23 |
+
for user, bot in history:
|
| 24 |
+
messages.append({"role": "user", "content": user})
|
| 25 |
+
messages.append({"role": "assistant", "content": bot})
|
| 26 |
+
messages.append({"role": "user", "content": message})
|
| 27 |
|
| 28 |
+
inputs = tokenizer.apply_chat_template(messages, return_tensors="pt").to(model.device)
|
| 29 |
+
outputs = model.generate(inputs, max_new_tokens=512, do_sample=True, temperature=0.7)
|
| 30 |
+
reply = tokenizer.decode(outputs[0], skip_special_tokens=True).split("assistant")[-1].strip()
|
| 31 |
+
return reply
|
| 32 |
|
| 33 |
+
gr.Interface(fn=respond, inputs="text", outputs="text", title="Friday Chatbot").launch()
|