Spaces:
Sleeping
Sleeping
RaghadAbdulaziz
commited on
Commit
·
ac729f1
1
Parent(s):
4c9f743
Fix: updated app.py and requirements.txt
Browse files- app.py +54 -24
- requirements.txt +4 -1
app.py
CHANGED
@@ -2,34 +2,64 @@ import gradio as gr
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
-
#
|
6 |
-
model_name = "microsoft/DialoGPT-medium" # Replace with your model if different
|
7 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
8 |
-
model = AutoModelForCausalLM.from_pretrained(model_name)
|
9 |
-
|
10 |
-
# ✅ Keyword filter (from your notebook)
|
11 |
allowed_keywords = [
|
12 |
-
"عجوة", "Ajwa", "جالكسي", "Galaxy", "مجدول", "Medjool", "منيفي",
|
13 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
]
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
|
|
|
|
|
|
28 |
|
29 |
-
|
30 |
-
return
|
31 |
|
32 |
-
#
|
33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
-
|
|
|
2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
3 |
import torch
|
4 |
|
5 |
+
# ===== 1) كلمات مفتاحية =====
|
|
|
|
|
|
|
|
|
|
|
6 |
allowed_keywords = [
|
7 |
+
"عجوة", "Ajwa", "جالكسي", "Galaxy", "مجدول", "Medjool", "منيفي", "Meneifi",
|
8 |
+
"نبتة علي", "Nabtat Ali", "رطب", "Rutab", "شائشة", "Shaishe", "صقعي", "Sokari",
|
9 |
+
"مبروم", "Mabroom", "سكري", "Sukkary",
|
10 |
+
"اللفحة السوداء", "Black Scorch", "حرق النخيل", "احتراق الجريد",
|
11 |
+
"ذبول الفيوزاريم", "Fusarium Wilt", "ذبول النخل", "النخل ناشف من جوّا",
|
12 |
+
"تبقعات الأوراق", "Leaf Spots", "نقاط على الجريد", "بقع بالنخل",
|
13 |
+
"نقص المغنيسيوم", "Magnesium Deficiency", "صفرة في الجريد", "ضعف العذوق",
|
14 |
+
"نقص المنغنيز", "Manganese Deficiency", "اصفرار الخوص", "النخل ما يعصر زين",
|
15 |
+
"نقص البوتاسيوم", "Potassium Deficiency", "الجريد يطيح بسرعة", "النخل ما يعطي ثمر واجد",
|
16 |
+
"بارلاتوريا بلانشارد", "Parlatoria Blanchardi", "حشرة بالنخل", "نخلتي فيها قمل أبيض",
|
17 |
+
"لفحة الشماريخ", "Rachis Blight", "الشماريخ يابسة", "الثمر ما يكمل",
|
18 |
+
"نخيل", "نخلة", "فسيلة", "فسائل", "مزرعة نخل", "نخل السعودية", "تمور", "تمر",
|
19 |
+
"أنواع التمر", "جودة التمر", "وش أفضل تمر", "تمور المملكة", "تمور القصيم",
|
20 |
+
"تمور المدينة", "تمر المدينة", "تمر القصيم", "تمر فاخر", "تمور فاخرة",
|
21 |
+
"تمر سكري", "خلاص", "نخلة مريضة", "مرض النخيل", "مشاكل النخيل", "أمراض التمر"
|
22 |
]
|
23 |
|
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=torch.float16,
|
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").to("cuda")
|
44 |
|
45 |
+
outputs = model.generate(
|
46 |
+
**inputs,
|
47 |
+
max_new_tokens=256,
|
48 |
+
do_sample=True,
|
49 |
+
top_p=0.9,
|
50 |
+
temperature=0.8
|
51 |
+
)
|
52 |
|
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="اكتب سؤالك هنا..."),
|
60 |
+
outputs=gr.Textbox(label="رد الشات بوت"),
|
61 |
+
title="مساعد التمور والنخيل",
|
62 |
+
description="روبوت دردشة متخصص في النخيل والتمور"
|
63 |
+
)
|
64 |
|
65 |
+
iface.launch()
|
requirements.txt
CHANGED
@@ -1,3 +1,6 @@
|
|
|
|
1 |
transformers
|
2 |
torch
|
3 |
-
|
|
|
|
|
|
1 |
+
gradio
|
2 |
transformers
|
3 |
torch
|
4 |
+
accelerate
|
5 |
+
sentencepiece
|
6 |
+
safetensors
|