Spaces:
Running
Running
RaghadAbdulaziz
commited on
Commit
·
269ac07
1
Parent(s):
7893b49
app.py
CHANGED
@@ -24,23 +24,26 @@ allowed_keywords = [
|
|
24 |
def is_relevant_question(user_input):
|
25 |
return any(keyword in user_input for keyword in allowed_keywords)
|
26 |
|
27 |
-
# ===== 2) تحميل النموذج =====
|
28 |
model_name = "ALLaM-AI/ALLaM-7B-Instruct-preview"
|
|
|
|
|
|
|
|
|
29 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False, trust_remote_code=True)
|
30 |
model = AutoModelForCausalLM.from_pretrained(
|
31 |
model_name,
|
32 |
trust_remote_code=True,
|
33 |
-
torch_dtype=
|
34 |
device_map="auto"
|
35 |
)
|
36 |
|
37 |
-
# ===== 3) توليد الرد =====
|
38 |
def chatbot_response(user_input):
|
39 |
if not is_relevant_question(user_input):
|
40 |
return "أنا متخصص فقط في النخيل والتمور. يرجى طرح أسئلة ذات صلة."
|
41 |
|
42 |
prompt = f"سؤال: {user_input}\nجواب:"
|
43 |
-
inputs = tokenizer(prompt, return_tensors="pt")
|
|
|
44 |
|
45 |
outputs = model.generate(
|
46 |
**inputs,
|
@@ -53,7 +56,6 @@ def chatbot_response(user_input):
|
|
53 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
54 |
return response.replace(prompt, "").strip()
|
55 |
|
56 |
-
# ===== 4) واجهة Gradio =====
|
57 |
iface = gr.Interface(
|
58 |
fn=chatbot_response,
|
59 |
inputs=gr.Textbox(lines=3, placeholder="اكتب سؤالك هنا..."),
|
|
|
24 |
def is_relevant_question(user_input):
|
25 |
return any(keyword in user_input for keyword in allowed_keywords)
|
26 |
|
|
|
27 |
model_name = "ALLaM-AI/ALLaM-7B-Instruct-preview"
|
28 |
+
|
29 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
30 |
+
dtype = torch.float16 if device == "cuda" else torch.float32
|
31 |
+
|
32 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=False, trust_remote_code=True)
|
33 |
model = AutoModelForCausalLM.from_pretrained(
|
34 |
model_name,
|
35 |
trust_remote_code=True,
|
36 |
+
torch_dtype=dtype,
|
37 |
device_map="auto"
|
38 |
)
|
39 |
|
|
|
40 |
def chatbot_response(user_input):
|
41 |
if not is_relevant_question(user_input):
|
42 |
return "أنا متخصص فقط في النخيل والتمور. يرجى طرح أسئلة ذات صلة."
|
43 |
|
44 |
prompt = f"سؤال: {user_input}\nجواب:"
|
45 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
46 |
+
inputs = {k: v.to(model.device) for k, v in inputs.items()}
|
47 |
|
48 |
outputs = model.generate(
|
49 |
**inputs,
|
|
|
56 |
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
57 |
return response.replace(prompt, "").strip()
|
58 |
|
|
|
59 |
iface = gr.Interface(
|
60 |
fn=chatbot_response,
|
61 |
inputs=gr.Textbox(lines=3, placeholder="اكتب سؤالك هنا..."),
|