nischal5490 commited on
Commit
62045af
·
verified ·
1 Parent(s): baa6136

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -25
app.py CHANGED
@@ -16,7 +16,6 @@ from torchvision.models import resnet18
16
  # ======================================================
17
  # 1) Load your local disease model (no HF token required)
18
  # ======================================================
19
- # Expecting turmeric_model.pth to be in the same repo as app.py
20
  MODEL_LOCAL_PATH = os.path.join(os.path.dirname(__file__), "turmeric_model.pth")
21
 
22
  DISEASE_CLASSES = ['Dry_Leaf', 'Healthy_Leaf', 'Leaf_Blotch', 'Rhizome_Disease', 'Rhizome_Healthy']
@@ -56,9 +55,9 @@ BASE_CONDITIONS = {
56
 
57
  def highlight_text(text):
58
  if text.startswith("✅"):
59
- return f"<span style='color:green; font-weight:bold; animation: blink-green 1s infinite;'>{text}</span>"
60
  else:
61
- return f"<span style='color:red; font-weight:bold; animation: blink-red 1s infinite;'>{text}</span>"
62
 
63
  def turmeric_advisor(planting_date, temp, humidity, soil_moisture, light_intensity, ph):
64
  try:
@@ -104,7 +103,6 @@ def turmeric_advisor(planting_date, temp, humidity, soil_moisture, light_intensi
104
  | 🧪 pH | {stage_ideal['ph'][0]} – {stage_ideal['ph'][1]} |
105
  """
106
 
107
- # Simple progress chart
108
  stages = list(GROWTH_STAGES.keys())
109
  days = [GROWTH_STAGES[s][1] for s in stages]
110
  colors = ["green" if d >= days_since else "lightgray" for d in days]
@@ -151,19 +149,18 @@ SYSTEM_PROMPT = (
151
  "Ignore or refuse unrelated topics and keep focus on turmeric cultivation, environment, pests, and processing."
152
  )
153
 
154
- _llm_kind = None # "chat" (causal chat) or "t2t" (seq2seq)
155
  _tokenizer = None
156
  _model = None
157
- _gen_pipe = None # used for seq2seq pipeline fallback
158
 
159
  def _clean_text(s: str) -> str:
160
- s = re.sub(r"[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]", " ", s) # control chars
161
  s = s.replace("�", "")
162
  s = re.sub(r"\s+", " ", s).strip()
163
  return s
164
 
165
  def _load_small_llm():
166
- """Try to load a small chat model; fall back to flan-t5-small on CPU."""
167
  global _llm_kind, _tokenizer, _model, _gen_pipe
168
  try:
169
  from transformers import AutoTokenizer, AutoModelForCausalLM
@@ -172,22 +169,19 @@ def _load_small_llm():
172
  _tokenizer.pad_token = _tokenizer.eos_token
173
  _model = AutoModelForCausalLM.from_pretrained(
174
  SMALL_LLM_MODEL,
175
- low_cpu_mem_usage=True, # helps CPU Spaces
176
- torch_dtype=torch.float32 # safest on CPU
177
  )
178
  _llm_kind = "chat"
179
  return
180
  except Exception as e:
181
- print(f"[mini-llm] Chat model load failed ({SMALL_LLM_MODEL}): {e}. Falling back to flan-t5-small.")
182
 
183
- # Fallback: lightweight instruction model that runs fast on CPU
184
  try:
185
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
186
  fallback_name = "google/flan-t5-small"
187
  _tokenizer = AutoTokenizer.from_pretrained(fallback_name)
188
- _model = AutoModelForSeq2SeqLM.from_pretrained(
189
- fallback_name,
190
- )
191
  _gen_pipe = pipeline("text2text-generation", model=_model, tokenizer=_tokenizer)
192
  _llm_kind = "t2t"
193
  except Exception as e:
@@ -197,7 +191,6 @@ def _load_small_llm():
197
  _load_small_llm()
198
 
199
  def _build_chat_prompt(history: List[Tuple[str, str]], user_msg: str):
200
- """Use model chat template if available; otherwise a simple role format."""
201
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
202
  for u, a in (history or []):
203
  if u:
@@ -212,30 +205,29 @@ def _build_chat_prompt(history: List[Tuple[str, str]], user_msg: str):
212
  messages, add_generation_prompt=True, return_tensors="pt"
213
  )
214
  except Exception:
215
- # Fallback to naive format if template missing
216
  txt = "System: " + SYSTEM_PROMPT + "\n"
217
  for u, a in (history or []):
218
  txt += f"User: {u}\nAssistant: {a}\n"
219
  txt += f"User: {user_msg}\nAssistant:"
220
  return _tokenizer([txt], return_tensors="pt").input_ids
221
  else:
222
- # For t2t we just build a compact instruction prompt
223
  context = " ".join([f"Q:{u} A:{a}" for (u, a) in (history or [])[-3:]])
 
224
  prompt = (
225
  f"{SYSTEM_PROMPT}\n"
226
- f"{('Context: ' + context + '\n') if context else ''}"
227
  f"Question: {user_msg}\n"
228
  f"Answer:"
229
  )
230
- return prompt # plain string for pipeline
231
 
232
  def small_llm_reply(message, chat_history):
233
- """Gradio handler: returns (textbox_cleared, updated_history)."""
234
  if _llm_kind is None:
235
  msg = "Local mini-LLM is unavailable on this Space. Please try again later."
236
  return "", (chat_history or []) + [(message, msg)]
237
 
238
- # ===== Chat causal LLM path =====
239
  if _llm_kind == "chat":
240
  input_ids = _build_chat_prompt(chat_history, message)
241
  input_ids = input_ids.to(_model.device)
@@ -251,7 +243,6 @@ def small_llm_reply(message, chat_history):
251
  pad_token_id=_tokenizer.pad_token_id,
252
  )
253
  gen_text = _tokenizer.decode(out_ids[0][input_ids.shape[-1]:], skip_special_tokens=True)
254
- # Cut if the model starts a new user turn marker
255
  for stop in ["\nUser:", "\n<|user|>", "\n### User:"]:
256
  pos = gen_text.find(stop)
257
  if pos != -1:
@@ -262,7 +253,7 @@ def small_llm_reply(message, chat_history):
262
  answer = "I'm not sure. Ask about turmeric planting windows, environment setpoints, or common diseases."
263
  return "", (chat_history or []) + [(message, answer)]
264
 
265
- # ===== Seq2Seq pipeline path (flan-t5-small) =====
266
  prompt = _build_chat_prompt(chat_history, message)
267
  out = _gen_pipe(
268
  prompt,
@@ -369,6 +360,5 @@ with gr.Blocks(css="""
369
  )
370
  clear.click(lambda: None, None, chatbot)
371
 
372
- # For Spaces, no need to expose share link
373
  if __name__ == "__main__":
374
  demo.launch()
 
16
  # ======================================================
17
  # 1) Load your local disease model (no HF token required)
18
  # ======================================================
 
19
  MODEL_LOCAL_PATH = os.path.join(os.path.dirname(__file__), "turmeric_model.pth")
20
 
21
  DISEASE_CLASSES = ['Dry_Leaf', 'Healthy_Leaf', 'Leaf_Blotch', 'Rhizome_Disease', 'Rhizome_Healthy']
 
55
 
56
  def highlight_text(text):
57
  if text.startswith("✅"):
58
+ return "<span style='color:green; font-weight:bold; animation: blink-green 1s infinite;'>{}</span>".format(text)
59
  else:
60
+ return "<span style='color:red; font-weight:bold; animation: blink-red 1s infinite;'>{}</span>".format(text)
61
 
62
  def turmeric_advisor(planting_date, temp, humidity, soil_moisture, light_intensity, ph):
63
  try:
 
103
  | 🧪 pH | {stage_ideal['ph'][0]} – {stage_ideal['ph'][1]} |
104
  """
105
 
 
106
  stages = list(GROWTH_STAGES.keys())
107
  days = [GROWTH_STAGES[s][1] for s in stages]
108
  colors = ["green" if d >= days_since else "lightgray" for d in days]
 
149
  "Ignore or refuse unrelated topics and keep focus on turmeric cultivation, environment, pests, and processing."
150
  )
151
 
152
+ _llm_kind = None
153
  _tokenizer = None
154
  _model = None
155
+ _gen_pipe = None
156
 
157
  def _clean_text(s: str) -> str:
158
+ s = re.sub(r"[\x00-\x08\x0B\x0C\x0E-\x1F\x7F-\x9F]", " ", s)
159
  s = s.replace("�", "")
160
  s = re.sub(r"\s+", " ", s).strip()
161
  return s
162
 
163
  def _load_small_llm():
 
164
  global _llm_kind, _tokenizer, _model, _gen_pipe
165
  try:
166
  from transformers import AutoTokenizer, AutoModelForCausalLM
 
169
  _tokenizer.pad_token = _tokenizer.eos_token
170
  _model = AutoModelForCausalLM.from_pretrained(
171
  SMALL_LLM_MODEL,
172
+ low_cpu_mem_usage=True,
173
+ torch_dtype=torch.float32
174
  )
175
  _llm_kind = "chat"
176
  return
177
  except Exception as e:
178
+ print(f"[mini-llm] Chat model load failed: {e}. Falling back to flan-t5-small.")
179
 
 
180
  try:
181
  from transformers import AutoTokenizer, AutoModelForSeq2SeqLM, pipeline
182
  fallback_name = "google/flan-t5-small"
183
  _tokenizer = AutoTokenizer.from_pretrained(fallback_name)
184
+ _model = AutoModelForSeq2SeqLM.from_pretrained(fallback_name)
 
 
185
  _gen_pipe = pipeline("text2text-generation", model=_model, tokenizer=_tokenizer)
186
  _llm_kind = "t2t"
187
  except Exception as e:
 
191
  _load_small_llm()
192
 
193
  def _build_chat_prompt(history: List[Tuple[str, str]], user_msg: str):
 
194
  messages = [{"role": "system", "content": SYSTEM_PROMPT}]
195
  for u, a in (history or []):
196
  if u:
 
205
  messages, add_generation_prompt=True, return_tensors="pt"
206
  )
207
  except Exception:
208
+ # Plain prompt if template missing
209
  txt = "System: " + SYSTEM_PROMPT + "\n"
210
  for u, a in (history or []):
211
  txt += f"User: {u}\nAssistant: {a}\n"
212
  txt += f"User: {user_msg}\nAssistant:"
213
  return _tokenizer([txt], return_tensors="pt").input_ids
214
  else:
215
+ # Seq2Seq prompt (avoid \n inside f-string expressions)
216
  context = " ".join([f"Q:{u} A:{a}" for (u, a) in (history or [])[-3:]])
217
+ context_str = f"Context: {context}\n" if context else ""
218
  prompt = (
219
  f"{SYSTEM_PROMPT}\n"
220
+ f"{context_str}"
221
  f"Question: {user_msg}\n"
222
  f"Answer:"
223
  )
224
+ return prompt
225
 
226
  def small_llm_reply(message, chat_history):
 
227
  if _llm_kind is None:
228
  msg = "Local mini-LLM is unavailable on this Space. Please try again later."
229
  return "", (chat_history or []) + [(message, msg)]
230
 
 
231
  if _llm_kind == "chat":
232
  input_ids = _build_chat_prompt(chat_history, message)
233
  input_ids = input_ids.to(_model.device)
 
243
  pad_token_id=_tokenizer.pad_token_id,
244
  )
245
  gen_text = _tokenizer.decode(out_ids[0][input_ids.shape[-1]:], skip_special_tokens=True)
 
246
  for stop in ["\nUser:", "\n<|user|>", "\n### User:"]:
247
  pos = gen_text.find(stop)
248
  if pos != -1:
 
253
  answer = "I'm not sure. Ask about turmeric planting windows, environment setpoints, or common diseases."
254
  return "", (chat_history or []) + [(message, answer)]
255
 
256
+ # Seq2Seq fallback
257
  prompt = _build_chat_prompt(chat_history, message)
258
  out = _gen_pipe(
259
  prompt,
 
360
  )
361
  clear.click(lambda: None, None, chatbot)
362
 
 
363
  if __name__ == "__main__":
364
  demo.launch()