Spaces:
Sleeping
Sleeping
Matan Kriel commited on
Commit Β·
7803d6a
1
Parent(s): e2dc689
updated app age input handeling
Browse files
app.py
CHANGED
|
@@ -40,7 +40,8 @@ def initialize_app():
|
|
| 40 |
|
| 41 |
# 2. FIT ENCODERS (For Feature Consistency)
|
| 42 |
print("π€ Fitting Label Encoders...")
|
| 43 |
-
|
|
|
|
| 44 |
for c in cat_cols:
|
| 45 |
if c in knowledge_df.columns:
|
| 46 |
le = LabelEncoder()
|
|
@@ -101,27 +102,36 @@ def predict_and_optimize(user_input, duration, hour, day_of_week, category, foll
|
|
| 101 |
def safe_encode(col, val):
|
| 102 |
le = ENCODERS.get(col)
|
| 103 |
if le:
|
| 104 |
-
# If value not seen, default to first class
|
| 105 |
if val in le.classes_:
|
| 106 |
return le.transform([val])[0]
|
| 107 |
else:
|
| 108 |
-
return 0
|
| 109 |
return 0
|
| 110 |
|
| 111 |
cat_encoded = safe_encode('category', category)
|
| 112 |
gender_encoded = safe_encode('gender', gender)
|
| 113 |
day_encoded = safe_encode('day_of_week', day_of_week)
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
except Exception as e:
|
| 117 |
return f"Encoding Error: {str(e)}", "", "", "", ""
|
| 118 |
|
| 119 |
# --- 2. INITIAL PREDICTION ---
|
| 120 |
# Feature Order MUST match model-prep.py:
|
| 121 |
-
# Embeddings + [duration, hour, followers,
|
| 122 |
text_vec = ST_MODEL.encode([user_input], convert_to_numpy=True)
|
| 123 |
|
| 124 |
-
|
|
|
|
| 125 |
|
| 126 |
feat_vec = np.hstack((text_vec, meta_vec))
|
| 127 |
|
|
@@ -176,14 +186,13 @@ def predict_and_optimize(user_input, duration, hour, day_of_week, category, foll
|
|
| 176 |
[Upload duration]
|
| 177 |
"""
|
| 178 |
|
| 179 |
-
|
| 180 |
try:
|
| 181 |
response = llm.generate_content(prompt)
|
| 182 |
improved_idea = response.text.strip()
|
| 183 |
|
| 184 |
# --- 5. RE-SCORING ---
|
| 185 |
new_text_vec = ST_MODEL.encode([improved_idea], convert_to_numpy=True)
|
| 186 |
-
#
|
| 187 |
new_feat_vec = np.hstack((new_text_vec, meta_vec))
|
| 188 |
|
| 189 |
new_log = MODEL.predict(new_feat_vec)[0]
|
|
@@ -201,13 +210,13 @@ def predict_and_optimize(user_input, duration, hour, day_of_week, category, foll
|
|
| 201 |
# --- GRADIO UI ---
|
| 202 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 203 |
gr.Markdown("# π Viral Content Optimizer")
|
| 204 |
-
gr.Markdown("Enter your video idea and stats to predict views and get AI-powered optimizations.")
|
| 205 |
|
| 206 |
with gr.Row():
|
| 207 |
with gr.Column(scale=1):
|
| 208 |
input_text = gr.Textbox(
|
| 209 |
-
label="Video Description",
|
| 210 |
-
placeholder="POV:
|
| 211 |
lines=3
|
| 212 |
)
|
| 213 |
|
|
|
|
| 40 |
|
| 41 |
# 2. FIT ENCODERS (For Feature Consistency)
|
| 42 |
print("π€ Fitting Label Encoders...")
|
| 43 |
+
# UPDATED: 'age' removed from here, treated as numeric
|
| 44 |
+
cat_cols = ['category', 'gender', 'day_of_week']
|
| 45 |
for c in cat_cols:
|
| 46 |
if c in knowledge_df.columns:
|
| 47 |
le = LabelEncoder()
|
|
|
|
| 102 |
def safe_encode(col, val):
|
| 103 |
le = ENCODERS.get(col)
|
| 104 |
if le:
|
| 105 |
+
# If value not seen, default to first class
|
| 106 |
if val in le.classes_:
|
| 107 |
return le.transform([val])[0]
|
| 108 |
else:
|
| 109 |
+
return 0
|
| 110 |
return 0
|
| 111 |
|
| 112 |
cat_encoded = safe_encode('category', category)
|
| 113 |
gender_encoded = safe_encode('gender', gender)
|
| 114 |
day_encoded = safe_encode('day_of_week', day_of_week)
|
| 115 |
+
|
| 116 |
+
# FIX: Map Age String to Numeric
|
| 117 |
+
age_map = {
|
| 118 |
+
"18-24": 21.0,
|
| 119 |
+
"25-34": 30.0,
|
| 120 |
+
"35-44": 40.0,
|
| 121 |
+
"45+": 50.0
|
| 122 |
+
}
|
| 123 |
+
age_numeric = age_map.get(str(age), 25.0) # Default to 25 if unknown
|
| 124 |
|
| 125 |
except Exception as e:
|
| 126 |
return f"Encoding Error: {str(e)}", "", "", "", ""
|
| 127 |
|
| 128 |
# --- 2. INITIAL PREDICTION ---
|
| 129 |
# Feature Order MUST match model-prep.py:
|
| 130 |
+
# Embeddings + [duration, hour, followers, age_numeric, category_enc, gender_enc, day_enc]
|
| 131 |
text_vec = ST_MODEL.encode([user_input], convert_to_numpy=True)
|
| 132 |
|
| 133 |
+
# Construct metadata vector
|
| 134 |
+
meta_vec = np.array([[duration, hour, followers, age_numeric, cat_encoded, gender_encoded, day_encoded]])
|
| 135 |
|
| 136 |
feat_vec = np.hstack((text_vec, meta_vec))
|
| 137 |
|
|
|
|
| 186 |
[Upload duration]
|
| 187 |
"""
|
| 188 |
|
|
|
|
| 189 |
try:
|
| 190 |
response = llm.generate_content(prompt)
|
| 191 |
improved_idea = response.text.strip()
|
| 192 |
|
| 193 |
# --- 5. RE-SCORING ---
|
| 194 |
new_text_vec = ST_MODEL.encode([improved_idea], convert_to_numpy=True)
|
| 195 |
+
# Using same metadata for the new prediction
|
| 196 |
new_feat_vec = np.hstack((new_text_vec, meta_vec))
|
| 197 |
|
| 198 |
new_log = MODEL.predict(new_feat_vec)[0]
|
|
|
|
| 210 |
# --- GRADIO UI ---
|
| 211 |
with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
| 212 |
gr.Markdown("# π Viral Content Optimizer")
|
| 213 |
+
gr.Markdown("Enter your video idea and stats to predict views and get AI-powered optimizations based on 2025 trends.")
|
| 214 |
|
| 215 |
with gr.Row():
|
| 216 |
with gr.Column(scale=1):
|
| 217 |
input_text = gr.Textbox(
|
| 218 |
+
label="Your Video Description",
|
| 219 |
+
placeholder="e.g., POV: trying the new grimace shake #viral",
|
| 220 |
lines=3
|
| 221 |
)
|
| 222 |
|