Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,67 @@
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
def get_t2t():
|
| 9 |
-
global
|
| 10 |
-
if
|
| 11 |
-
|
| 12 |
"text2text-generation",
|
| 13 |
-
model=
|
|
|
|
|
|
|
| 14 |
)
|
| 15 |
-
return
|
| 16 |
|
| 17 |
-
def ai_fallback(prompt):
|
| 18 |
try:
|
| 19 |
gen = get_t2t()
|
| 20 |
-
#
|
| 21 |
out = gen(
|
| 22 |
-
f"Answer briefly: {prompt}",
|
| 23 |
max_new_tokens=48,
|
| 24 |
-
do_sample=False
|
| 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
|
| 44 |
if any(w in msg for w in ["mummy", "mummies", "mummified", "offering"]):
|
| 45 |
-
return
|
| 46 |
if any(w in msg for w in ["daily", "life", "pest", "mouse", "rat", "snake"]):
|
| 47 |
-
return
|
| 48 |
if any(w in msg for w in ["worship", "god", "goddess", "taboo"]):
|
| 49 |
-
return
|
| 50 |
return ai_fallback(message)
|
| 51 |
|
| 52 |
-
demo = gr.ChatInterface(
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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()
|