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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -9
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  import gradio as gr
3
 
4
  # --------- CPU hygiene (nice-to-have) ----------
@@ -53,10 +54,10 @@ def get_vader():
53
  print("[startup] Loaded VADER sentiment analyzer")
54
  return _vader
55
 
56
- # -------- Helpers --------
57
  def detect_sentiment_bucket(text: str):
58
  """
59
- Return ('neg'|'neu'|'pos', compound_score).
60
  Thresholds chosen for clear buckets in chat settings.
61
  """
62
  scores = get_vader().polarity_scores(text or "")
@@ -67,17 +68,59 @@ def detect_sentiment_bucket(text: str):
67
  return "pos", c
68
  return "neu", c
69
 
70
- def apply_tone_prefix(reply_text: str, bucket: str) -> str:
71
- """Prepend a tiny tone wrapper without changing factual content."""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  if bucket == "pos":
73
- prefix = "Great question! "
74
  elif bucket == "neg":
75
- prefix = "Calm down. You're being a little too negative! "
76
  else:
77
  prefix = ""
78
  return (prefix + (reply_text or "")).strip()
79
 
80
- # ---- Use the LLM if the generator doesn't understand user prompt ----
81
  def ai_fallback(prompt: str) -> str:
82
  try:
83
  gen = get_t2t()
@@ -117,12 +160,12 @@ def reply(message, history):
117
  base = ai_fallback(message)
118
 
119
  # 3) tone wrapper (content unchanged)
120
- return apply_tone_prefix(base, bucket)
121
 
122
  # -------- UI --------
123
  demo = gr.ChatInterface(
124
  fn=reply,
125
- title="Cats of Ancient Egypt Chatbot"
126
  )
127
 
128
  if __name__ == "__main__":
 
1
  import os
2
+ import re
3
  import gradio as gr
4
 
5
  # --------- CPU hygiene (nice-to-have) ----------
 
54
  print("[startup] Loaded VADER sentiment analyzer")
55
  return _vader
56
 
57
+ # -------- Sentiment helpers --------
58
  def detect_sentiment_bucket(text: str):
59
  """
60
+ Return ('neg'|'neu'|'pos', compound_score) using VADER.
61
  Thresholds chosen for clear buckets in chat settings.
62
  """
63
  scores = get_vader().polarity_scores(text or "")
 
68
  return "pos", c
69
  return "neu", c
70
 
71
+ def is_question(text: str) -> bool:
72
+ t = (text or "").strip()
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()
80
+ return any(k in t for k in [
81
+ "thanks", "thank you", "appreciate", "appreciated",
82
+ "great answer", "nice", "awesome", "love", "helpful",
83
+ "that helped", "that was good", "i like your response"
84
+ ])
85
+
86
+ POS_QUESTION_PREFIXES = [
87
+ "Good question! ",
88
+ "Nice one—here’s the gist: ",
89
+ "Let’s dig in. ",
90
+ ]
91
+
92
+ POS_PRAISE_PREFIXES = [
93
+ "You’re welcome—glad that helped. ",
94
+ "Appreciate the kind words! ",
95
+ "Happy it was useful. ",
96
+ ]
97
+
98
+ POS_STATEMENT_PREFIXES = [
99
+ "Sounds good. ",
100
+ "Got it. ",
101
+ "All right—here’s the short version. ",
102
+ ]
103
+
104
+ # Your custom negative message:
105
+ NEG_PREFIX = "Calm down. You're being a little too negative! "
106
+
107
+ def choose_positive_prefix(message: str) -> str:
108
+ if is_thanks_or_praise(message):
109
+ return POS_PRAISE_PREFIXES[0]
110
+ if is_question(message):
111
+ return POS_QUESTION_PREFIXES[0]
112
+ return POS_STATEMENT_PREFIXES[0]
113
+
114
+ def apply_tone_prefix(reply_text: str, bucket: str, message: str = "") -> str:
115
  if bucket == "pos":
116
+ prefix = choose_positive_prefix(message)
117
  elif bucket == "neg":
118
+ prefix = NEG_PREFIX
119
  else:
120
  prefix = ""
121
  return (prefix + (reply_text or "")).strip()
122
 
123
+ # ---- LLM fallback (used when rules don't match) ----
124
  def ai_fallback(prompt: str) -> str:
125
  try:
126
  gen = get_t2t()
 
160
  base = ai_fallback(message)
161
 
162
  # 3) tone wrapper (content unchanged)
163
+ return apply_tone_prefix(base, bucket, message)
164
 
165
  # -------- UI --------
166
  demo = gr.ChatInterface(
167
  fn=reply,
168
+ title="😺 Cats of Ancient Egypt Chatbot 😺"
169
  )
170
 
171
  if __name__ == "__main__":