Update app.py
Browse filesUpdated so it should add a text game
app.py
CHANGED
|
@@ -1,87 +1,147 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
-
from datasets import load_dataset
|
| 4 |
-
from sklearn.model_selection import train_test_split
|
| 5 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
import joblib
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 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 |
]], columns=[
|
| 45 |
-
"savings",
|
| 46 |
-
"sales_skills",
|
| 47 |
-
"idea_level",
|
| 48 |
])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
with gr.Blocks() as demo:
|
| 61 |
-
gr.Markdown("#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
with gr.Row():
|
| 64 |
-
with gr.Column():
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
btn = gr.Button("๐ฎ Predict")
|
| 77 |
-
|
| 78 |
-
with gr.Column():
|
| 79 |
-
output = gr.Label(label="Prediction (Readiness Level)")
|
| 80 |
-
|
| 81 |
-
btn.click(
|
| 82 |
-
predict,
|
| 83 |
-
inputs=[savings, income, bills, entertainment, assets, sales_skills, confidence, dependents, age, idea_level, entrepreneurial_experience],
|
| 84 |
-
outputs=output
|
| 85 |
-
)
|
| 86 |
|
| 87 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import random, json, os
|
| 3 |
import pandas as pd
|
|
|
|
|
|
|
|
|
|
| 4 |
import joblib
|
| 5 |
+
from huggingface_hub import hf_hub_download
|
| 6 |
+
|
| 7 |
+
REPO_ID = "DetectiveShadow/Entrepreneurial_predictor"
|
| 8 |
+
|
| 9 |
+
# --- Try loading trained regressor + feature columns from Hub; fall back to heuristic if not available ---
|
| 10 |
+
regressor = None
|
| 11 |
+
FEATURE_COLUMNS = None
|
| 12 |
+
try:
|
| 13 |
+
reg_path = hf_hub_download(REPO_ID, "regressor.pkl")
|
| 14 |
+
feat_path = hf_hub_download(REPO_ID, "feature_columns.json")
|
| 15 |
+
regressor = joblib.load(reg_path)
|
| 16 |
+
with open(feat_path, "r") as f:
|
| 17 |
+
FEATURE_COLUMNS = json.load(f)
|
| 18 |
+
except Exception as e:
|
| 19 |
+
print("โ ๏ธ Could not load regressor from Hub; will use heuristic. Error:", e)
|
| 20 |
+
|
| 21 |
+
def confidence_to_bonus(conf):
|
| 22 |
+
return {"Low": 0, "Medium": 5, "High": 10}.get(conf, 5)
|
| 23 |
+
|
| 24 |
+
def heuristic_score(profile):
|
| 25 |
+
# Matches earlier heuristic logic
|
| 26 |
+
disposable = profile["income"] - profile["bills"] - profile["entertainment"]
|
| 27 |
+
score = (
|
| 28 |
+
(profile["savings"] + profile["assets"]) / 1000
|
| 29 |
+
+ disposable / 500
|
| 30 |
+
+ profile["sales_skills"] * 2
|
| 31 |
+
+ profile["idea_level"] * 3
|
| 32 |
+
+ profile["entrepreneurial_experience"] * 2
|
| 33 |
+
+ confidence_to_bonus(profile["confidence"])
|
| 34 |
+
- profile["dependents"]
|
| 35 |
+
+ (40 - abs(35 - profile["age"])) / 5
|
| 36 |
+
)
|
| 37 |
+
return max(0, min(100, score))
|
| 38 |
+
|
| 39 |
+
def level_from_score(score):
|
| 40 |
+
if score < 30: return "Low"
|
| 41 |
+
if score < 60: return "Moderate"
|
| 42 |
+
return "High"
|
| 43 |
+
|
| 44 |
+
def build_features_df(profile):
|
| 45 |
+
# Build a one-row DataFrame in the same format as training
|
| 46 |
+
base = pd.DataFrame([[
|
| 47 |
+
profile["savings"], profile["income"], profile["bills"], profile["entertainment"],
|
| 48 |
+
profile["assets"], profile["sales_skills"], profile["confidence"], profile["dependents"],
|
| 49 |
+
profile["age"], profile["idea_level"], profile["entrepreneurial_experience"]
|
| 50 |
]], columns=[
|
| 51 |
+
"savings","income","bills","entertainment","assets",
|
| 52 |
+
"sales_skills","confidence","dependents","age",
|
| 53 |
+
"idea_level","entrepreneurial_experience"
|
| 54 |
])
|
| 55 |
+
df = pd.get_dummies(base, columns=["confidence"])
|
| 56 |
+
if FEATURE_COLUMNS is not None:
|
| 57 |
+
# ensure all training cols present
|
| 58 |
+
for col in FEATURE_COLUMNS:
|
| 59 |
+
if col not in df.columns:
|
| 60 |
+
df[col] = 0
|
| 61 |
+
df = df[FEATURE_COLUMNS]
|
| 62 |
+
return df
|
| 63 |
|
| 64 |
+
def compute_score(profile):
|
| 65 |
+
if regressor is not None and FEATURE_COLUMNS is not None:
|
| 66 |
+
X = build_features_df(profile)
|
| 67 |
+
try:
|
| 68 |
+
pred = float(regressor.predict(X)[0])
|
| 69 |
+
return max(0, min(100, pred))
|
| 70 |
+
except Exception as e:
|
| 71 |
+
print("โ ๏ธ Regressor prediction failed, fallback to heuristic:", e)
|
| 72 |
+
return heuristic_score(profile)
|
| 73 |
|
| 74 |
+
# --- Scenario generator ---
|
| 75 |
+
def random_profile():
|
| 76 |
+
return {
|
| 77 |
+
"savings": random.randint(0, 20000),
|
| 78 |
+
"income": random.randint(1500, 10000),
|
| 79 |
+
"bills": random.randint(500, 8000),
|
| 80 |
+
"entertainment": random.randint(0, 1500),
|
| 81 |
+
"assets": random.randint(0, 60000),
|
| 82 |
+
"sales_skills": random.randint(1, 5),
|
| 83 |
+
"confidence": random.choice(["Low", "Medium", "High"]),
|
| 84 |
+
"dependents": random.randint(0, 6),
|
| 85 |
+
"age": random.randint(18, 64),
|
| 86 |
+
"idea_level": random.randint(1, 10),
|
| 87 |
+
"entrepreneurial_experience": random.randint(0, 10),
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
def profile_to_story(p):
|
| 91 |
+
# A friendly, human-readable scenario
|
| 92 |
+
parts = []
|
| 93 |
+
parts.append(f"{p['age']}-year-old aspiring founder with {p['dependents']} dependents.")
|
| 94 |
+
parts.append(f"Savings of ${p['savings']:,} and assets totaling ${p['assets']:,}.")
|
| 95 |
+
parts.append(f"Monthly income ${p['income']:,}, bills ${p['bills']:,}, entertainment ${p['entertainment']:,}.")
|
| 96 |
+
parts.append(f"Sales skills rated {p['sales_skills']}/5 and confidence **{p['confidence']}**.")
|
| 97 |
+
parts.append(f"Business idea quality {p['idea_level']}/10 and entrepreneurial experience {p['entrepreneurial_experience']} year(s).")
|
| 98 |
+
return " ".join(parts)
|
| 99 |
+
|
| 100 |
+
# --- Gradio app logic ---
|
| 101 |
+
def new_scenario():
|
| 102 |
+
p = random_profile()
|
| 103 |
+
story = profile_to_story(p)
|
| 104 |
+
true_score = round(compute_score(p), 2)
|
| 105 |
+
true_level = level_from_score(true_score)
|
| 106 |
+
# We keep true values hidden in state; return the story, reset guess, and stash state
|
| 107 |
+
return story, 50, {"profile": p, "score": true_score, "level": true_level}
|
| 108 |
+
|
| 109 |
+
def check_guess(guess, state):
|
| 110 |
+
if not state or "score" not in state:
|
| 111 |
+
return {"error": "No scenario generated yet. Click '๐ฒ New Scenario' first."}
|
| 112 |
+
true_score = state["score"]
|
| 113 |
+
true_level = state["level"]
|
| 114 |
+
delta = round(abs(true_score - float(guess)), 2)
|
| 115 |
+
verdict = "โ
Great guess!" if delta <= 5 else ("๐ก Close!" if delta <= 10 else "โ Not quite.")
|
| 116 |
+
return {
|
| 117 |
+
"Your Guess": float(guess),
|
| 118 |
+
"Actual Score": true_score,
|
| 119 |
+
"Readiness Level": true_level,
|
| 120 |
+
"Off by": delta,
|
| 121 |
+
"Verdict": verdict
|
| 122 |
+
}
|
| 123 |
|
| 124 |
with gr.Blocks() as demo:
|
| 125 |
+
gr.Markdown("# ๐ฏ Guess the Entrepreneurial Readiness")
|
| 126 |
+
gr.Markdown(
|
| 127 |
+
"Iโll generate a random founder profile. **You guess the readiness score (0โ100)**, "
|
| 128 |
+
"then weโll reveal the actual score and level."
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
state = gr.State() # holds {"profile": ..., "score": ..., "level": ...}
|
| 132 |
|
| 133 |
with gr.Row():
|
| 134 |
+
with gr.Column(scale=3):
|
| 135 |
+
scenario_md = gr.Markdown("Click **New Scenario** to start.")
|
| 136 |
+
with gr.Row():
|
| 137 |
+
new_btn = gr.Button("๐ฒ New Scenario", variant="primary")
|
| 138 |
+
guess = gr.Slider(0, 100, value=50, step=1, label="Your Guess (0โ100)")
|
| 139 |
+
reveal_btn = gr.Button("๐ Check My Guess")
|
| 140 |
+
with gr.Column(scale=2):
|
| 141 |
+
result = gr.JSON(label="Result")
|
| 142 |
+
|
| 143 |
+
# Wire events
|
| 144 |
+
new_btn.click(fn=new_scenario, inputs=None, outputs=[scenario_md, guess, state])
|
| 145 |
+
reveal_btn.click(fn=check_guess, inputs=[guess, state], outputs=result)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 146 |
|
| 147 |
demo.launch()
|