Spaces:
Running
Running
RaghadAbdulaziz
commited on
Commit
·
be12119
1
Parent(s):
3e803bf
app.py
CHANGED
@@ -1,101 +1,56 @@
|
|
1 |
import torch
|
2 |
-
from transformers import
|
3 |
import gradio as gr
|
4 |
|
5 |
# ====================== 1) Load Model ======================
|
6 |
-
model_name = "
|
|
|
7 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
8 |
model = AutoModelForCausalLM.from_pretrained(
|
9 |
model_name,
|
10 |
device_map="auto",
|
11 |
-
torch_dtype=torch.float16
|
12 |
-
trust_remote_code=True
|
13 |
-
)
|
14 |
-
pipe = pipeline(
|
15 |
-
"text-generation",
|
16 |
-
model=model,
|
17 |
-
tokenizer=tokenizer,
|
18 |
-
device=0,
|
19 |
-
max_new_tokens=512,
|
20 |
-
temperature=0.6,
|
21 |
-
repetition_penalty=1.1
|
22 |
)
|
23 |
|
24 |
-
# ====================== 2)
|
25 |
-
def build_prompt(history, user_input):
|
26 |
-
prompt = ""
|
27 |
-
for user_msg, bot_msg in history:
|
28 |
-
prompt += f"<|user|>\n{user_msg}\n<|assistant|>\n{bot_msg}\n"
|
29 |
-
prompt += f"<|user|>\n{user_input}\n<|assistant|>\n"
|
30 |
-
return prompt
|
31 |
-
|
32 |
-
# ====================== 3) Keywords ======================
|
33 |
allowed_keywords = [
|
34 |
-
# 🌴 Palm-related
|
35 |
"palm", "palms", "نخلة", "نخل", "نخلي", "فسيلة", "فسائل", "جذع", "سعف", "خوص", "جريد", "شماريخ", "عذوق", "مزرعة نخل", "offshoot", "date palm",
|
36 |
-
|
37 |
-
# 🐛 Diseases
|
38 |
"مرض النخيل", "ذبول", "لفحة", "اللفحة السوداء", "فطريات", "قمل أبيض", "ذبول الفيوزاريم", "تبقعات الأوراق", "نقص المغنيسيوم", "نقص البوتاسيوم", "اصفرار الخوص", "احتراق الجريد", "الثمر ما يكتمل",
|
39 |
"Black scorch", "Fusarium wilt", "Rachis blight", "Leaf spot", "Mites", "Insects", "White bugs", "Parlatoria", "Magnesium deficiency", "Potassium deficiency", "Manganese deficiency",
|
40 |
-
|
41 |
-
# 🍇 Dates
|
42 |
"تمر", "تمور", "تمر سكري", "سكري", "خلاص", "عجوة", "مجدول", "روثانة", "رطب", "برني", "عنبري", "صفاوي", "صقعي", "خضري", "فاخر", "premium dates", "best dates", "Ajwa", "Medjool", "Sukkary", "Khalas", "Safawi", "Sagai", "Khudri", "Ruthana", "Barni", "Anbara",
|
43 |
-
|
44 |
-
# 🌱 Care & Maintenance
|
45 |
"ري", "سقي", "تسميد", "مبيد", "متى أسقي النخلة", "نصائح العناية", "رش", "تقليم", "عناية", "كيف أعتني",
|
46 |
"palm care", "how to water", "fertilizer", "pest control", "sunlight", "organic spray", "how to prune",
|
47 |
-
|
48 |
-
# 🌦️ Stats & Weather
|
49 |
"كم نخلة", "كم تمر", "عدد النخل", "الجو", "الطقس", "الحرارة", "الرطوبة", "مناسب للتلقيح", "is it good weather", "weather", "temperature", "total palms", "healthy palms", "sick palms",
|
50 |
-
|
51 |
-
# 🛒 Consumer
|
52 |
"أين أشتري تمر", "أفضل تمر", "تمور مغشوشة", "تمور القصيم", "تمور المدينة", "جودة التمر", "التغليف", "شراء تمر", "buy dates", "where to find", "identify good dates", "how to store dates",
|
53 |
-
|
54 |
-
# 📸 App features
|
55 |
"افتح الكاميرا", "حلل الصورة", "قيم النخلة", "قيم التمر", "camera", "analyze", "scan", "image detection",
|
56 |
-
|
57 |
-
# ℹ️ General
|
58 |
"تطبيق لينة", "نظام لينة", "عن لينة", "Lina app", "explain Lina", "help with Lina", "what is Lina"
|
59 |
]
|
60 |
|
61 |
greetings = ["سلام", "السلام عليكم", "أهلاً", "هاي", "hi", "hello", "hey"]
|
62 |
intro_questions = ["من أنت", "مين انت", "what is this", "who are you", "explain the app", "about lina"]
|
63 |
|
64 |
-
# ======================
|
65 |
-
def
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
lower_input = user_input.lower()
|
70 |
|
71 |
-
#
|
72 |
-
if any(
|
73 |
-
|
74 |
-
# If asking about app intro
|
75 |
-
elif any(intro in lower_input for intro in intro_questions):
|
76 |
-
output = "أنا مساعد ذكي مختص بالنخيل والتمور. 🌴🍇 اسألني عن أي شيء!"
|
77 |
-
else:
|
78 |
-
prompt = build_prompt(history, user_input)
|
79 |
-
generated = pipe(prompt)[0]['generated_text']
|
80 |
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
else:
|
85 |
-
output = generated.strip()
|
86 |
|
87 |
-
|
88 |
-
|
|
|
|
|
89 |
|
90 |
-
|
91 |
-
with gr.Blocks() as demo:
|
92 |
-
gr.Markdown("<h1>🕌 Arabic Palm Chatbot (نظام لينة)</h1>")
|
93 |
-
chatbot = gr.Chatbot(label="المحادثة")
|
94 |
-
with gr.Row():
|
95 |
-
txt = gr.Textbox(placeholder="اكتب رسالتك هنا...", label="أدخل نصك").style(container=False)
|
96 |
-
send = gr.Button("إرسال")
|
97 |
|
98 |
-
|
99 |
-
|
100 |
|
101 |
-
|
|
|
1 |
import torch
|
2 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
3 |
import gradio as gr
|
4 |
|
5 |
# ====================== 1) Load Model ======================
|
6 |
+
model_name = "akhooli/arabic-llama-2-7b-chat"
|
7 |
+
|
8 |
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
9 |
model = AutoModelForCausalLM.from_pretrained(
|
10 |
model_name,
|
11 |
device_map="auto",
|
12 |
+
torch_dtype=torch.float16
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
)
|
14 |
|
15 |
+
# ====================== 2) Keywords ======================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
allowed_keywords = [
|
|
|
17 |
"palm", "palms", "نخلة", "نخل", "نخلي", "فسيلة", "فسائل", "جذع", "سعف", "خوص", "جريد", "شماريخ", "عذوق", "مزرعة نخل", "offshoot", "date palm",
|
|
|
|
|
18 |
"مرض النخيل", "ذبول", "لفحة", "اللفحة السوداء", "فطريات", "قمل أبيض", "ذبول الفيوزاريم", "تبقعات الأوراق", "نقص المغنيسيوم", "نقص البوتاسيوم", "اصفرار الخوص", "احتراق الجريد", "الثمر ما يكتمل",
|
19 |
"Black scorch", "Fusarium wilt", "Rachis blight", "Leaf spot", "Mites", "Insects", "White bugs", "Parlatoria", "Magnesium deficiency", "Potassium deficiency", "Manganese deficiency",
|
|
|
|
|
20 |
"تمر", "تمور", "تمر سكري", "سكري", "خلاص", "عجوة", "مجدول", "روثانة", "رطب", "برني", "عنبري", "صفاوي", "صقعي", "خضري", "فاخر", "premium dates", "best dates", "Ajwa", "Medjool", "Sukkary", "Khalas", "Safawi", "Sagai", "Khudri", "Ruthana", "Barni", "Anbara",
|
|
|
|
|
21 |
"ري", "سقي", "تسميد", "مبيد", "متى أسقي النخلة", "نصائح العناية", "رش", "تقليم", "عناية", "كيف أعتني",
|
22 |
"palm care", "how to water", "fertilizer", "pest control", "sunlight", "organic spray", "how to prune",
|
|
|
|
|
23 |
"كم نخلة", "كم تمر", "عدد النخل", "الجو", "الطقس", "الحرارة", "الرطوبة", "مناسب للتلقيح", "is it good weather", "weather", "temperature", "total palms", "healthy palms", "sick palms",
|
|
|
|
|
24 |
"أين أشتري تمر", "أفضل تمر", "تمور مغشوشة", "تمور القصيم", "تمور المدينة", "جودة التمر", "التغليف", "شراء تمر", "buy dates", "where to find", "identify good dates", "how to store dates",
|
|
|
|
|
25 |
"افتح الكاميرا", "حلل الصورة", "قيم النخلة", "قيم التمر", "camera", "analyze", "scan", "image detection",
|
|
|
|
|
26 |
"تطبيق لينة", "نظام لينة", "عن لينة", "Lina app", "explain Lina", "help with Lina", "what is Lina"
|
27 |
]
|
28 |
|
29 |
greetings = ["سلام", "السلام عليكم", "أهلاً", "هاي", "hi", "hello", "hey"]
|
30 |
intro_questions = ["من أنت", "مين انت", "what is this", "who are you", "explain the app", "about lina"]
|
31 |
|
32 |
+
# ====================== 3) Define Chat ======================
|
33 |
+
def chatbot(input_text):
|
34 |
+
# Check greetings
|
35 |
+
if any(greet in input_text.lower() for greet in greetings):
|
36 |
+
return "مرحباً! 👋 كيف أقدر أساعدك اليوم؟"
|
|
|
37 |
|
38 |
+
# Check intro questions
|
39 |
+
if any(q in input_text.lower() for q in intro_questions):
|
40 |
+
return "أنا Lina ✨ نظام ذكي لمساعدة مزارعي النخيل ومستهلكي التمور!"
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Check if allowed
|
43 |
+
if not any(keyword in input_text.lower() for keyword in allowed_keywords):
|
44 |
+
return "❌ عذراً، لا يمكنني التعامل مع هذا السؤال. حاول أن تسألني عن النخيل أو التمور أو الرعاية الزراعية."
|
|
|
|
|
45 |
|
46 |
+
# If allowed, generate answer
|
47 |
+
inputs = tokenizer(input_text, return_tensors="pt").to(model.device)
|
48 |
+
output = model.generate(**inputs, max_new_tokens=256, do_sample=True, temperature=0.7)
|
49 |
+
decoded = tokenizer.decode(output[0], skip_special_tokens=True)
|
50 |
|
51 |
+
return decoded
|
|
|
|
|
|
|
|
|
|
|
|
|
52 |
|
53 |
+
# ====================== 4) Gradio App ======================
|
54 |
+
app = gr.Interface(fn=chatbot, inputs="text", outputs="text", title="🌴 Lina Chatbot")
|
55 |
|
56 |
+
app.launch()
|