SleepyTerr commited on
Commit
7818ebd
Β·
verified Β·
1 Parent(s): 08b4916

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +40 -14
app.py CHANGED
@@ -20,19 +20,19 @@ except Exception as e:
20
  model_load_error = traceback.format_exc()
21
  print("⚠️ Model load error:\n", model_load_error)
22
 
23
- # ---- Improved parsing ----
24
  def parse_input_for_fields(text):
25
  text = text.lower()
26
  extracted = {}
27
 
28
- def grab(patterns, key):
29
  if key in extracted:
30
  return
31
  for pat in patterns:
32
  m = re.search(pat, text)
33
  if m:
34
  try:
35
- extracted[key] = float(m.group(1))
36
  except:
37
  extracted[key] = m.group(1)
38
  return
@@ -50,7 +50,7 @@ def parse_input_for_fields(text):
50
  # Expenses
51
  grab([r"(?:expense|bill|spend).{0,15}\$?(\d{3,6})"], "monthly_expenses")
52
 
53
- # Entertainment (expanded)
54
  grab([
55
  r"(?:entertainment|fun|leisure).{0,15}\$?(\d{2,5})",
56
  r"spend.{0,10}\$?(\d{2,5}).{0,10}(?:entertainment|fun|leisure)"
@@ -68,8 +68,11 @@ def parse_input_for_fields(text):
68
  # Sales skills
69
  grab([r"(?:sales|selling).{0,15}(\d{1,2})"], "sales_skills_1_5")
70
 
71
- # Dependents
72
- grab([r"(?:dependents?|kids|children).{0,15}(\d{1,2})"], "dependence_1_5")
 
 
 
73
 
74
  return extracted
75
 
@@ -110,30 +113,54 @@ def chatbot(user_input, history):
110
  try:
111
  collected = conversation_state["collected"]
112
 
113
- # Parse any new info
114
  new_data = parse_input_for_fields(user_input)
115
  for k, v in new_data.items():
116
  if k not in collected:
117
  collected[k] = v
118
 
119
- # Missing fields?
120
  missing = [f for f in REQUIRED_FIELDS if f not in collected]
121
  if missing:
122
  field_map = {
123
- "age": "your age",
124
- "monthly_income": "your monthly income ($)",
125
  "monthly_expenses": "your monthly expenses ($)",
126
  "entertainment_spending": "your monthly entertainment spending ($)",
127
  "savings_amount": "your savings ($)",
128
  "assets": "your assets/net worth ($)",
129
  "risk_tolerance_1_10": "your risk tolerance (1–10)",
130
  "sales_skills_1_5": "your sales skills (1–5)",
131
- "dependence_1_5": "how many dependents you support"
132
  }
133
  next_field = missing[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134
  return f"Hmm... I think I need a bit more info. Could you tell me {field_map[next_field]}?"
135
 
136
- # All fields collected
137
  df = pd.DataFrame([collected])
138
  label = None
139
  try:
@@ -161,7 +188,7 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="purple", secondary_hue="purple"
161
  """
162
  <h1 style='text-align:center; color:#D8B4FE;'>πŸ’¬ Entrepreneurial Readiness Chatbot</h1>
163
  <p style='text-align:center; color:#E9D5FF; font-size:16px;'>
164
- Hello! I'm an <b>entrepreneurial readiness chatbot</b>.<br>
165
  Tell me a little about yourself (e.g., age, income, expenses, savings, etc.)<br>
166
  and I'll give you a readiness score between 0 and 100!
167
  </p>
@@ -187,4 +214,3 @@ if model_load_error:
187
  print("=== MODEL LOAD ERROR ===\n", model_load_error)
188
 
189
  demo.launch()
190
-
 
20
  model_load_error = traceback.format_exc()
21
  print("⚠️ Model load error:\n", model_load_error)
22
 
23
+ # ---- Parsing ----
24
  def parse_input_for_fields(text):
25
  text = text.lower()
26
  extracted = {}
27
 
28
+ def grab(patterns, key, cast_float=True):
29
  if key in extracted:
30
  return
31
  for pat in patterns:
32
  m = re.search(pat, text)
33
  if m:
34
  try:
35
+ extracted[key] = float(m.group(1)) if cast_float else m.group(1)
36
  except:
37
  extracted[key] = m.group(1)
38
  return
 
50
  # Expenses
51
  grab([r"(?:expense|bill|spend).{0,15}\$?(\d{3,6})"], "monthly_expenses")
52
 
53
+ # Entertainment
54
  grab([
55
  r"(?:entertainment|fun|leisure).{0,15}\$?(\d{2,5})",
56
  r"spend.{0,10}\$?(\d{2,5}).{0,10}(?:entertainment|fun|leisure)"
 
68
  # Sales skills
69
  grab([r"(?:sales|selling).{0,15}(\d{1,2})"], "sales_skills_1_5")
70
 
71
+ # Dependents β†’ support "no kids"/"none"
72
+ if any(phrase in text for phrase in ["no kids", "none", "0 dependents"]):
73
+ extracted["dependence_1_5"] = 0
74
+ else:
75
+ grab([r"(?:dependents?|kids|children).{0,15}(\d{1,2})"], "dependence_1_5")
76
 
77
  return extracted
78
 
 
113
  try:
114
  collected = conversation_state["collected"]
115
 
116
+ # Parse free-text
117
  new_data = parse_input_for_fields(user_input)
118
  for k, v in new_data.items():
119
  if k not in collected:
120
  collected[k] = v
121
 
122
+ # Check missing
123
  missing = [f for f in REQUIRED_FIELDS if f not in collected]
124
  if missing:
125
  field_map = {
126
+ "age": "your age (just the number)",
127
+ "monthly_income": "your monthly income ($, just numbers)",
128
  "monthly_expenses": "your monthly expenses ($)",
129
  "entertainment_spending": "your monthly entertainment spending ($)",
130
  "savings_amount": "your savings ($)",
131
  "assets": "your assets/net worth ($)",
132
  "risk_tolerance_1_10": "your risk tolerance (1–10)",
133
  "sales_skills_1_5": "your sales skills (1–5)",
134
+ "dependence_1_5": "how many dependents you support (number or 'none')"
135
  }
136
  next_field = missing[0]
137
+
138
+ # If user typed only a number in response
139
+ if re.fullmatch(r"\d+", user_input.strip()):
140
+ collected[next_field] = float(user_input.strip())
141
+ missing = [f for f in REQUIRED_FIELDS if f not in collected]
142
+ if not missing:
143
+ # All done
144
+ df = pd.DataFrame([collected])
145
+ label = None
146
+ try:
147
+ if model:
148
+ raw_pred = model.predict(df)
149
+ label = raw_pred[0] if hasattr(raw_pred, "__len__") else raw_pred
150
+ except Exception:
151
+ label = None
152
+
153
+ score = compute_readiness(collected)
154
+ conversation_state["collected"] = {}
155
+
156
+ response = f"πŸ“Š Your entrepreneurial readiness score is **{score:.1f}/100**."
157
+ if label:
158
+ response += f" The model also classified you as: **{label}**."
159
+ return response
160
+
161
  return f"Hmm... I think I need a bit more info. Could you tell me {field_map[next_field]}?"
162
 
163
+ # All info collected
164
  df = pd.DataFrame([collected])
165
  label = None
166
  try:
 
188
  """
189
  <h1 style='text-align:center; color:#D8B4FE;'>πŸ’¬ Entrepreneurial Readiness Chatbot</h1>
190
  <p style='text-align:center; color:#E9D5FF; font-size:16px;'>
191
+ πŸ‘‹ Hello! I'm an <b>entrepreneurial readiness chatbot</b>.<br>
192
  Tell me a little about yourself (e.g., age, income, expenses, savings, etc.)<br>
193
  and I'll give you a readiness score between 0 and 100!
194
  </p>
 
214
  print("=== MODEL LOAD ERROR ===\n", model_load_error)
215
 
216
  demo.launch()