StevenMSAI commited on
Commit
9c4ec4e
·
verified ·
1 Parent(s): 971d379

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -5
app.py CHANGED
@@ -22,7 +22,9 @@ GEN_MODEL_NAME = "MBZUAI/LaMini-Flan-T5-248M" # CPU-friendly text2text model
22
  DOMAIN_INSTRUCTIONS = (
23
  "You are a concise assistant about cats in ancient Egypt. "
24
  "Keep focus on Bastet, cat mummies, daily life, worship, and other ancient Egypt facts. "
25
- "If the user asks something unrelated, say briefly that you only cover those topics and suggest one."
 
 
26
  )
27
 
28
  HELP_TEXT = (
@@ -30,6 +32,18 @@ HELP_TEXT = (
30
  "Type anything else to try the AI fallback."
31
  )
32
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  # -------- Lazy singletons --------
34
  _t2t = None
35
  _vader = None
@@ -73,7 +87,10 @@ def is_question(text: str) -> bool:
73
  if "?" in t:
74
  return True
75
  # Heuristic for question-like openings
76
- return bool(re.match(r"^(who|what|when|where|why|how|do|does|did|can|could|is|are|was|were|should|would|will)\b", t.lower()))
 
 
 
77
 
78
  def is_thanks_or_praise(text: str) -> bool:
79
  t = (text or "").lower()
@@ -132,9 +149,10 @@ def ai_fallback(prompt: str) -> str:
132
  out = gen(
133
  prefixed,
134
  max_new_tokens=48,
135
- do_sample=False
 
136
  )[0]["generated_text"]
137
- return (out or "").strip()
138
  except Exception as e:
139
  print("AI fallback error:", repr(e))
140
  return "AI fallback had an issue. Please try a simpler question or use the topics in 'help'."
@@ -146,16 +164,25 @@ def reply(message, history):
146
 
147
  # 2) rules-first responses
148
  msg = (message or "").strip().lower()
149
- if msg in {"hi", "hello", "hey"} or "help" in msg:
 
 
 
 
150
  base = "Hi! I share facts about cats in ancient Egypt.\n\n" + HELP_TEXT
 
151
  elif "bastet" in msg or "bast" in msg:
152
  base = "Bastet (later cat-headed) … major cult center at Bubastis in the Nile Delta."
 
153
  elif any(w in msg for w in ["mummy", "mummies", "mummified", "offering"]):
154
  base = "Millions of animal mummies (cats common), esp. Late Period (664–332 BCE)."
 
155
  elif any(w in msg for w in ["daily", "life", "pest", "mouse", "rat", "snake"]):
156
  base = "Cats protected grain stores; art shows them under chairs/on leashes with owners."
 
157
  elif any(w in msg for w in ["worship", "god", "goddess", "taboo"]):
158
  base = "People didn’t worship pet cats as gods; they revered cats via Bastet and votive offerings."
 
159
  else:
160
  base = ai_fallback(message)
161
 
 
22
  DOMAIN_INSTRUCTIONS = (
23
  "You are a concise assistant about cats in ancient Egypt. "
24
  "Keep focus on Bastet, cat mummies, daily life, worship, and other ancient Egypt facts. "
25
+ "If the user asks something unrelated, say briefly that you only cover those topics and suggest one. "
26
+ "Do not include greetings or apologies and do not say phrases like 'as an AI language model'. "
27
+ "Start directly with the answer."
28
  )
29
 
30
  HELP_TEXT = (
 
32
  "Type anything else to try the AI fallback."
33
  )
34
 
35
+ # -------- Output cleanup (remove model-added fluff) --------
36
+ DISCLAIMER_PATTERNS = [
37
+ r"^\s*(hi|hello|hey)[,!.?\s-]*", # stray greeting
38
+ r"^\s*i'?m\s+sorr(y|ied)[^.\n]*[.\n]*", # leading apology
39
+ r"^\s*as an ai language model[^.\n]*[.\n]*" # “as an AI language model…”
40
+ ]
41
+ def strip_preamble(text: str) -> str:
42
+ t = text or ""
43
+ for pat in DISCLAIMER_PATTERNS:
44
+ t = re.sub(pat, "", t, flags=re.IGNORECASE)
45
+ return t.strip()
46
+
47
  # -------- Lazy singletons --------
48
  _t2t = None
49
  _vader = None
 
87
  if "?" in t:
88
  return True
89
  # Heuristic for question-like openings
90
+ return bool(re.match(
91
+ r"^(who|what|when|where|why|how|do|does|did|can|could|is|are|was|were|should|would|will)\b",
92
+ t.lower()
93
+ ))
94
 
95
  def is_thanks_or_praise(text: str) -> bool:
96
  t = (text or "").lower()
 
149
  out = gen(
150
  prefixed,
151
  max_new_tokens=48,
152
+ do_sample=False,
153
+ no_repeat_ngram_size=3
154
  )[0]["generated_text"]
155
+ return strip_preamble(out)
156
  except Exception as e:
157
  print("AI fallback error:", repr(e))
158
  return "AI fallback had an issue. Please try a simpler question or use the topics in 'help'."
 
164
 
165
  # 2) rules-first responses
166
  msg = (message or "").strip().lower()
167
+
168
+ # Robust greeting/help rule: catches "hi there", "hello!", etc.
169
+ if re.search(r"\b(hi|hello|hey|hiya|yo|greetings)\b", msg) or any(
170
+ k in msg for k in ["help", "menu", "topics", "instructions"]
171
+ ):
172
  base = "Hi! I share facts about cats in ancient Egypt.\n\n" + HELP_TEXT
173
+
174
  elif "bastet" in msg or "bast" in msg:
175
  base = "Bastet (later cat-headed) … major cult center at Bubastis in the Nile Delta."
176
+
177
  elif any(w in msg for w in ["mummy", "mummies", "mummified", "offering"]):
178
  base = "Millions of animal mummies (cats common), esp. Late Period (664–332 BCE)."
179
+
180
  elif any(w in msg for w in ["daily", "life", "pest", "mouse", "rat", "snake"]):
181
  base = "Cats protected grain stores; art shows them under chairs/on leashes with owners."
182
+
183
  elif any(w in msg for w in ["worship", "god", "goddess", "taboo"]):
184
  base = "People didn’t worship pet cats as gods; they revered cats via Bastet and votive offerings."
185
+
186
  else:
187
  base = ai_fallback(message)
188