RaghadAbdulaziz commited on
Commit
fa9d1d9
·
1 Parent(s): be12119
Files changed (1) hide show
  1. app.py +48 -26
app.py CHANGED
@@ -1,56 +1,78 @@
 
 
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()
 
1
+ # app.py
2
+
3
  import torch
4
  from transformers import AutoTokenizer, AutoModelForCausalLM
5
  import gradio as gr
6
 
7
+ # ====================== 1) Load model ======================
8
+ model_name = "ALLaM-AI/ALLaM-7B-Instruct-preview" # ✅ الصحيح
9
+ tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True, trust_remote_code=True)
 
10
  model = AutoModelForCausalLM.from_pretrained(
11
  model_name,
12
  device_map="auto",
13
+ torch_dtype=torch.float16,
14
+ trust_remote_code=True,
15
+ revision="main"
16
  )
17
 
18
+ # ====================== 2) System prompts ======================
19
  allowed_keywords = [
20
+ # 🌴 Palm-related
21
  "palm", "palms", "نخلة", "نخل", "نخلي", "فسيلة", "فسائل", "جذع", "سعف", "خوص", "جريد", "شماريخ", "عذوق", "مزرعة نخل", "offshoot", "date palm",
22
+ # 🐛 Diseases
23
  "مرض النخيل", "ذبول", "لفحة", "اللفحة السوداء", "فطريات", "قمل أبيض", "ذبول الفيوزاريم", "تبقعات الأوراق", "نقص المغنيسيوم", "نقص البوتاسيوم", "اصفرار الخوص", "احتراق الجريد", "الثمر ما يكتمل",
24
  "Black scorch", "Fusarium wilt", "Rachis blight", "Leaf spot", "Mites", "Insects", "White bugs", "Parlatoria", "Magnesium deficiency", "Potassium deficiency", "Manganese deficiency",
25
+ # 🍇 Dates
26
  "تمر", "تمور", "تمر سكري", "سكري", "خلاص", "عجوة", "مجدول", "روثانة", "رطب", "برني", "عنبري", "صفاوي", "صقعي", "خضري", "فاخر", "premium dates", "best dates", "Ajwa", "Medjool", "Sukkary", "Khalas", "Safawi", "Sagai", "Khudri", "Ruthana", "Barni", "Anbara",
27
+ # 🌱 Care & Maintenance
28
  "ري", "سقي", "تسميد", "مبيد", "متى أسقي النخلة", "نصائح العناية", "رش", "تقليم", "عناية", "كيف أعتني",
29
  "palm care", "how to water", "fertilizer", "pest control", "sunlight", "organic spray", "how to prune",
30
+ # 🌦️ Stats & Weather
31
  "كم نخلة", "كم تمر", "عدد النخل", "الجو", "الطقس", "الحرارة", "الرطوبة", "مناسب للتلقيح", "is it good weather", "weather", "temperature", "total palms", "healthy palms", "sick palms",
32
+ # 🛒 Consumer
33
  "أين أشتري تمر", "أفضل تمر", "تمور مغشوشة", "تمور القصيم", "تمور المدينة", "جودة التمر", "التغليف", "شراء تمر", "buy dates", "where to find", "identify good dates", "how to store dates",
34
+ # 📸 App features
35
  "افتح الكاميرا", "حلل الصورة", "قيم النخلة", "قيم التمر", "camera", "analyze", "scan", "image detection",
36
+ # ℹ️ General
37
  "تطبيق لينة", "نظام لينة", "عن لينة", "Lina app", "explain Lina", "help with Lina", "what is Lina"
38
  ]
39
 
40
  greetings = ["سلام", "السلام عليكم", "أهلاً", "هاي", "hi", "hello", "hey"]
41
  intro_questions = ["من أنت", "مين انت", "what is this", "who are you", "explain the app", "about lina"]
42
 
43
+ # ====================== 3) Chat function ======================
44
+ def chat(user_message):
45
+ # Check if input is allowed
46
+ if not any(keyword in user_message.lower() for keyword in allowed_keywords + greetings + intro_questions):
47
+ return " عذرًا، هذا النظام متخصص فقط بالنخيل والتمور. حاول أن تسأل عن شيء متعلق بذلك 🌴."
48
 
49
+ # Build prompt
50
+ prompt = f"<|user|>\n{user_message}\n<|assistant|>"
 
51
 
52
+ # Generate answer
53
+ inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
54
+ output = model.generate(
55
+ **inputs,
56
+ max_new_tokens=500,
57
+ temperature=0.7,
58
+ top_p=0.9,
59
+ do_sample=True,
60
+ repetition_penalty=1.1,
61
+ pad_token_id=tokenizer.eos_token_id
62
+ )
63
+ decoded_output = tokenizer.decode(output[0], skip_special_tokens=True)
64
 
65
+ # Extract the assistant's reply
66
+ reply = decoded_output.split("<|assistant|>")[-1].strip()
67
+ return reply
 
68
 
69
+ # ====================== 4) Gradio Interface ======================
70
+ iface = gr.Interface(
71
+ fn=chat,
72
+ inputs=gr.Textbox(lines=2, placeholder="اكتب سؤالك عن النخيل أو التمر هنا..."),
73
+ outputs=gr.Textbox(),
74
+ title="مساعد لينة - Lina Assistant 🌴",
75
+ description="اسألني عن النخيل، التمور، العناية، الأمراض، الطقس أو أي شيء متعلق! 🍃"
76
+ )
77
 
78
+ iface.launch()