StevenMSAI commited on
Commit
d511a4d
·
verified ·
1 Parent(s): 9779c60

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -21
app.py CHANGED
@@ -1,54 +1,67 @@
 
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- import gradio as gr
5
- from transformers import pipeline
 
 
 
 
6
 
7
- t2t = None
 
 
 
 
8
  def get_t2t():
9
- global t2t
10
- if t2t is None:
11
- t2t = pipeline(
12
  "text2text-generation",
13
- model="google/flan-t5-small" # instruction-tuned T5
 
 
14
  )
15
- return t2t
16
 
17
- def ai_fallback(prompt):
18
  try:
19
  gen = get_t2t()
20
- # FLAN-T5 follows instructions well; keep outputs short & tame
21
  out = gen(
22
- f"Answer briefly: {prompt}",
23
  max_new_tokens=48,
24
- do_sample=False # deterministic, less rambling
25
  )[0]["generated_text"]
26
- return out.strip()
27
  except Exception:
28
  return "AI fallback had an issue. Please try a simpler question or use the topics in 'help'."
29
 
30
-
31
  HELP_TEXT = (
32
  "Ask me about: Bastet • cat mummies • daily life • worship\n"
33
  "Type anything else to try the AI fallback."
34
  )
35
 
36
-
37
-
38
  def reply(message, history):
39
  msg = (message or "").strip().lower()
40
  if msg in {"hi", "hello", "hey"} or "help" in msg:
41
  return "Hi! I share facts about cats in ancient Egypt.\n\n" + HELP_TEXT
42
  if "bastet" in msg or "bast" in msg:
43
- return ("Bastet (later cat-headed) … major cult center at Bubastis in the Nile Delta.")
44
  if any(w in msg for w in ["mummy", "mummies", "mummified", "offering"]):
45
- return ("Millions of animal mummies (cats common), esp. Late Period (664–332 BCE).")
46
  if any(w in msg for w in ["daily", "life", "pest", "mouse", "rat", "snake"]):
47
- return ("Cats protected grain stores; art shows them under chairs/on leashes with owners.")
48
  if any(w in msg for w in ["worship", "god", "goddess", "taboo"]):
49
- return ("People didn’t worship pet cats as gods; they revered cats via Bastet and votive offerings.")
50
  return ai_fallback(message)
51
 
52
- demo = gr.ChatInterface(fn=reply, title="Cats of Ancient Egypt — Hybrid (Rules + Tiny Transformer)")
 
 
 
 
53
  if __name__ == "__main__":
54
  demo.launch()
 
1
+ import os
2
+ import torch
3
  import gradio as gr
4
  from transformers import pipeline
5
 
6
+ # Tidy tokenizer threading + CPU thread count for smoother free-tier performance
7
+ os.environ["TOKENIZERS_PARALLELISM"] = "false"
8
+ try:
9
+ torch.set_num_threads(2)
10
+ except Exception:
11
+ pass # safe fallback if torch is missing threads control on some envs
12
 
13
+ # --- Model ---
14
+ GEN_MODEL_NAME = "MBZUAI/LaMini-Flan-T5-248M" # text2text model that runs on CPU
15
+
16
+ # Lazy-loaded text2text generator
17
+ _t2t = None
18
  def get_t2t():
19
+ global _t2t
20
+ if _t2t is None:
21
+ _t2t = pipeline(
22
  "text2text-generation",
23
+ model=GEN_MODEL_NAME,
24
+ tokenizer=GEN_MODEL_NAME, # explicit to avoid tokenizer mismatch
25
+ device_map="cpu" # ensure CPU-only on free tier
26
  )
27
+ return _t2t
28
 
29
+ def ai_fallback(prompt: str) -> str:
30
  try:
31
  gen = get_t2t()
32
+ # Keep answers short & deterministic (good for CPU + chatbot UX)
33
  out = gen(
34
+ f"Answer briefly and concisely: {prompt}",
35
  max_new_tokens=48,
36
+ do_sample=False
37
  )[0]["generated_text"]
38
+ return (out or "").strip()
39
  except Exception:
40
  return "AI fallback had an issue. Please try a simpler question or use the topics in 'help'."
41
 
 
42
  HELP_TEXT = (
43
  "Ask me about: Bastet • cat mummies • daily life • worship\n"
44
  "Type anything else to try the AI fallback."
45
  )
46
 
 
 
47
  def reply(message, history):
48
  msg = (message or "").strip().lower()
49
  if msg in {"hi", "hello", "hey"} or "help" in msg:
50
  return "Hi! I share facts about cats in ancient Egypt.\n\n" + HELP_TEXT
51
  if "bastet" in msg or "bast" in msg:
52
+ return "Bastet (later cat-headed) … major cult center at Bubastis in the Nile Delta."
53
  if any(w in msg for w in ["mummy", "mummies", "mummified", "offering"]):
54
+ return "Millions of animal mummies (cats common), esp. Late Period (664–332 BCE)."
55
  if any(w in msg for w in ["daily", "life", "pest", "mouse", "rat", "snake"]):
56
+ return "Cats protected grain stores; art shows them under chairs/on leashes with owners."
57
  if any(w in msg for w in ["worship", "god", "goddess", "taboo"]):
58
+ return "People didn’t worship pet cats as gods; they revered cats via Bastet and votive offerings."
59
  return ai_fallback(message)
60
 
61
+ demo = gr.ChatInterface(
62
+ fn=reply,
63
+ title="Cats of Ancient Egypt — Hybrid (Rules + LaMini-Flan-T5-248M)"
64
+ )
65
+
66
  if __name__ == "__main__":
67
  demo.launch()