Update app.py
Browse files
app.py
CHANGED
|
@@ -1,216 +1,135 @@
|
|
| 1 |
-
import traceback
|
| 2 |
import gradio as gr
|
| 3 |
-
import pandas as pd
|
| 4 |
-
import joblib
|
| 5 |
import re
|
|
|
|
|
|
|
|
|
|
| 6 |
from huggingface_hub import hf_hub_download
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
model =
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
], "entertainment_spending")
|
| 58 |
-
|
| 59 |
-
# Savings
|
| 60 |
-
grab([r"(?:savings|saved).{0,15}\$?(\d{3,7})"], "savings_amount")
|
| 61 |
-
|
| 62 |
-
# Assets
|
| 63 |
-
grab([r"(?:asset|net worth|property).{0,15}\$?(\d{3,8})"], "assets")
|
| 64 |
-
|
| 65 |
-
# Risk tolerance
|
| 66 |
-
grab([r"(?:risk).{0,15}(\d{1,2})"], "risk_tolerance_1_10")
|
| 67 |
-
|
| 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 |
-
|
| 79 |
-
# ---- Compute numeric score ----
|
| 80 |
-
def compute_readiness(row):
|
| 81 |
-
score = 0
|
| 82 |
-
score += row.get("risk_tolerance_1_10", 0) * 2
|
| 83 |
-
score += row.get("sales_skills_1_5", 0) * 4
|
| 84 |
-
|
| 85 |
-
disposable = row.get("monthly_income", 0) - row.get("monthly_expenses", 0)
|
| 86 |
-
if disposable > 0:
|
| 87 |
-
score += disposable / 200
|
| 88 |
-
else:
|
| 89 |
-
score += disposable / 500
|
| 90 |
-
|
| 91 |
-
score += row.get("savings_amount", 0) / 2000
|
| 92 |
-
score += row.get("assets", 0) / 20000
|
| 93 |
-
score -= row.get("dependence_1_5", 0) * 3
|
| 94 |
-
|
| 95 |
-
return min(max(score, 0), 100)
|
| 96 |
-
|
| 97 |
-
# ---- Conversation state ----
|
| 98 |
-
conversation_state = {"collected": {}}
|
| 99 |
-
|
| 100 |
-
REQUIRED_FIELDS = [
|
| 101 |
-
"age",
|
| 102 |
-
"monthly_income",
|
| 103 |
-
"monthly_expenses",
|
| 104 |
-
"entertainment_spending",
|
| 105 |
-
"savings_amount",
|
| 106 |
-
"assets",
|
| 107 |
-
"risk_tolerance_1_10",
|
| 108 |
-
"sales_skills_1_5",
|
| 109 |
-
"dependence_1_5"
|
| 110 |
-
]
|
| 111 |
-
|
| 112 |
-
def chatbot(user_input, history):
|
| 113 |
try:
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
gr.Markdown(
|
| 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>
|
| 195 |
-
""",
|
| 196 |
-
)
|
| 197 |
-
|
| 198 |
-
chatbot_ui = gr.ChatInterface(
|
| 199 |
-
fn=chatbot,
|
| 200 |
-
chatbot=gr.Chatbot(
|
| 201 |
-
height=450,
|
| 202 |
-
bubble_full_width=False,
|
| 203 |
-
show_copy_button=True,
|
| 204 |
-
),
|
| 205 |
-
textbox=gr.Textbox(
|
| 206 |
-
placeholder="Type your scenario here...",
|
| 207 |
-
container=True,
|
| 208 |
-
),
|
| 209 |
-
title="Entrepreneurial Readiness Chatbot",
|
| 210 |
-
description="I’ll guide you step by step if I need more info."
|
| 211 |
-
)
|
| 212 |
-
|
| 213 |
-
if model_load_error:
|
| 214 |
-
print("=== MODEL LOAD ERROR ===\n", model_load_error)
|
| 215 |
|
| 216 |
demo.launch()
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
| 2 |
import re
|
| 3 |
+
import joblib
|
| 4 |
+
import csv
|
| 5 |
+
import os
|
| 6 |
from huggingface_hub import hf_hub_download
|
| 7 |
+
from datasets import load_dataset, Dataset, concatenate_datasets
|
| 8 |
+
from huggingface_hub import HfApi, HfFolder
|
| 9 |
+
import pandas as pd
|
| 10 |
|
| 11 |
+
DATASET_REPO = "SleepyTerr/entrepreneurial_readiness_v2"
|
| 12 |
+
|
| 13 |
+
# Load model
|
| 14 |
+
model_repo_id = "SleepyTerr/entrepreneurial_readiness"
|
| 15 |
+
model_file = hf_hub_download(repo_id=model_repo_id, filename="readiness_model.joblib", repo_type="model")
|
| 16 |
+
model = joblib.load(model_file)
|
| 17 |
+
|
| 18 |
+
# Conversation state
|
| 19 |
+
expected_fields = ["age", "risk_tolerance", "sales_skills", "dependents",
|
| 20 |
+
"monthly_income", "monthly_expenses", "entertainment",
|
| 21 |
+
"savings", "assets"]
|
| 22 |
+
|
| 23 |
+
field_questions = {
|
| 24 |
+
"age": "How old are you?",
|
| 25 |
+
"risk_tolerance": "On a scale of 1-10, how much risk are you willing to take?",
|
| 26 |
+
"sales_skills": "On a scale of 1-10, how would you rate your sales skills?",
|
| 27 |
+
"dependents": "How many dependents do you take care of?",
|
| 28 |
+
"monthly_income": "What is your monthly income?",
|
| 29 |
+
"monthly_expenses": "What are your monthly expenses?",
|
| 30 |
+
"entertainment": "How much do you spend on entertainment each month?",
|
| 31 |
+
"savings": "How much do you currently have saved?",
|
| 32 |
+
"assets": "What is the total value of your assets?"
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
def parse_info(message):
|
| 36 |
+
"""Extract possible numbers from free text"""
|
| 37 |
+
numbers = re.findall(r"\d+", message)
|
| 38 |
+
return [int(n) for n in numbers]
|
| 39 |
+
|
| 40 |
+
def generate_tips(user_data, score):
|
| 41 |
+
"""Give personalized tips depending on weaknesses"""
|
| 42 |
+
tips = []
|
| 43 |
+
if user_data.get("monthly_expenses", 0) > user_data.get("monthly_income", 0) * 0.8:
|
| 44 |
+
tips.append("Try to lower your expenses so they don’t eat up most of your income.")
|
| 45 |
+
if user_data.get("savings", 0) < user_data.get("monthly_income", 0) * 2:
|
| 46 |
+
tips.append("Build an emergency fund with at least 2–3 months of income saved.")
|
| 47 |
+
if user_data.get("assets", 0) < user_data.get("monthly_income", 0) * 6:
|
| 48 |
+
tips.append("Work on building long-term assets to increase stability.")
|
| 49 |
+
if user_data.get("sales_skills", 0) < 5:
|
| 50 |
+
tips.append("Consider improving your sales or communication skills—they’re key for entrepreneurship.")
|
| 51 |
+
if user_data.get("risk_tolerance", 0) < 4:
|
| 52 |
+
tips.append("Think about whether you’re comfortable taking risks—entrepreneurship often involves uncertainty.")
|
| 53 |
+
|
| 54 |
+
if not tips:
|
| 55 |
+
tips.append("Great job! You’re on a strong path to entrepreneurship.")
|
| 56 |
+
|
| 57 |
+
return tips
|
| 58 |
+
|
| 59 |
+
def save_to_db(user_data, score):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
try:
|
| 61 |
+
# Convert to pandas row
|
| 62 |
+
row = {**user_data, "score": score}
|
| 63 |
+
df = pd.DataFrame([row])
|
| 64 |
+
|
| 65 |
+
# Load existing dataset
|
| 66 |
+
ds = load_dataset(DATASET_REPO, split="train")
|
| 67 |
+
|
| 68 |
+
# Convert new row into a Dataset
|
| 69 |
+
new_ds = Dataset.from_pandas(df)
|
| 70 |
+
|
| 71 |
+
# Concatenate
|
| 72 |
+
updated = concatenate_datasets([ds, new_ds])
|
| 73 |
+
|
| 74 |
+
# Push back to Hugging Face
|
| 75 |
+
updated.push_to_hub(DATASET_REPO, split="train")
|
| 76 |
+
|
| 77 |
+
print("✅ Response saved to Hugging Face dataset")
|
| 78 |
+
except Exception as e:
|
| 79 |
+
print("⚠️ Failed to save:", e)
|
| 80 |
+
|
| 81 |
+
def chatbot(message, history, user_data, current_field):
|
| 82 |
+
history = history or []
|
| 83 |
+
user_data = user_data or {}
|
| 84 |
+
current_field = current_field or expected_fields[0]
|
| 85 |
+
|
| 86 |
+
# If current field not filled, try to extract it
|
| 87 |
+
if current_field in expected_fields:
|
| 88 |
+
nums = parse_info(message)
|
| 89 |
+
if nums:
|
| 90 |
+
user_data[current_field] = nums[0]
|
| 91 |
+
# Move to next field
|
| 92 |
+
next_index = expected_fields.index(current_field) + 1
|
| 93 |
+
if next_index < len(expected_fields):
|
| 94 |
+
current_field = expected_fields[next_index]
|
| 95 |
+
history.append((message, field_questions[current_field]))
|
| 96 |
+
else:
|
| 97 |
+
# All info collected → make prediction
|
| 98 |
+
features = [user_data.get(f, 0) for f in expected_fields]
|
| 99 |
+
score = model.predict([features])[0]
|
| 100 |
+
tips = generate_tips(user_data, score)
|
| 101 |
+
tips_text = "\n- " + "\n- ".join(tips)
|
| 102 |
+
|
| 103 |
+
# Save to database (CSV here, but can be swapped)
|
| 104 |
+
save_to_db(user_data, score)
|
| 105 |
+
|
| 106 |
+
history.append((message, f"✅ Your entrepreneurial readiness score is **{score:.1f}/100**!\n\nHere are some tips to improve:\n{tips_text}"))
|
| 107 |
+
return history, user_data, None
|
| 108 |
+
else:
|
| 109 |
+
# Ask again in case of missing number
|
| 110 |
+
history.append((message, f"Please provide a number for {current_field}."))
|
| 111 |
+
else:
|
| 112 |
+
history.append((message, "Hmm... I need a bit more info. Can you clarify?"))
|
| 113 |
+
|
| 114 |
+
return history, user_data, current_field
|
| 115 |
+
|
| 116 |
+
with gr.Blocks(css="""
|
| 117 |
+
body {background-color: #1a002b;}
|
| 118 |
+
.chatbot {background-color: #2d0a45; border-radius: 12px; padding: 10px;}
|
| 119 |
+
.message.user {background-color: #5a2d82; color: white; border-radius: 8px; padding: 8px; margin: 5px;}
|
| 120 |
+
.message.bot {background-color: #3b0f58; color: #eee; border-radius: 8px; padding: 8px; margin: 5px;}
|
| 121 |
+
""") as demo:
|
| 122 |
+
gr.Markdown("<h1 style='color:white; text-align:center;'>💡 Entrepreneurial Readiness Chatbot</h1>")
|
| 123 |
+
gr.Markdown("<p style='color:#ddd; text-align:center;'>Hello! I'm an entrepreneurial readiness chatbot! 🧑💻<br>Tell me a little about yourself and I'll give you a score between 0 and 100!</p>")
|
| 124 |
+
|
| 125 |
+
chatbot_ui = gr.Chatbot(label="Chat", elem_classes="chatbot", height=400)
|
| 126 |
+
msg = gr.Textbox(placeholder="Type your scenario here...", label="Your Message")
|
| 127 |
+
state = gr.State({})
|
| 128 |
+
current_field = gr.State(expected_fields[0])
|
| 129 |
+
|
| 130 |
+
def respond(message, history, user_data, current_field):
|
| 131 |
+
return chatbot(message, history, user_data, current_field)
|
| 132 |
+
|
| 133 |
+
msg.submit(respond, [msg, chatbot_ui, state, current_field], [chatbot_ui, state, current_field])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
demo.launch()
|