Spaces:
Running
Running
File size: 11,668 Bytes
391b9f9 4c53aee 391b9f9 |
1 2 3 4 5 6 7 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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 |
import numpy as np
import pandas as pd
from tensorflow.keras.models import load_model
import joblib
from openai import OpenAI
from dotenv import load_dotenv
import os
import json
import gradio as gr
import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
load_dotenv()
API = os.environ.get("OPENROUTER_API_KEY")
logger.info(f"API Key loaded: {bool(API)}")
# Baselines
BASELINE_LOWER = {
'Household': 30,
'Food': 40,
'Shopping': 7,
'Transportation': 5,
'Health & Fitness': 5,
'Entertainment': 5,
'Beauty': 4,
'Investment': 4,
}
BASELINE_UPPER = {
'Household': 11,
'Food': 10,
'Shopping': 13,
'Transportation': 11,
'Health & Fitness': 10,
'Entertainment': 18,
'Beauty': 8,
'Investment': 19,
}
# Load model and scaler
def load_financial_model(
model_path='model/fuzz_dnn_full_model.keras',
scaler_path='model/fuzzy_dnn_scaler.pkl',
):
logger.info("Starting to load model and scaler")
try:
model = load_model(model_path)
scaler = joblib.load(scaler_path)
logger.info("Model and scaler loaded successfully")
return model, scaler
except Exception as e:
print(f"Error loading model or scaler: {e}")
logger.error(f"Error loading model or scaler: {e}")
return None, None
# Prepare features
def prepare_features(df):
df['spend_deviation_ratio'] = df['Percent_Spend'] / (df['Deviation'].abs() + 1)
return df[['Percent_Spend', 'Deviation', 'spend_deviation_ratio']]
# Determine income level
def determine_income_level(total_spending):
return 'upper' if total_spending >= 5000 else 'lower'
# Predict spending pattern
def predict_spending_pattern(model, scaler, input_data):
total_spending = sum(input_data.values())
income_level = determine_income_level(total_spending)
baseline = BASELINE_UPPER if income_level == 'upper' else BASELINE_LOWER
percent_spend = {k: (v / total_spending) * 100 for k, v in input_data.items()}
rows = []
for category, spend_percent in percent_spend.items():
deviation = spend_percent - baseline.get(category, 0)
rows.append(
{
'Category': category,
'Percent_Spend': spend_percent,
'Deviation': deviation,
}
)
pred_df = pd.DataFrame(rows)
X = prepare_features(pred_df)
X_scaled = scaler.transform(X)
predictions = model.predict(X_scaled, verbose=0)
results = pd.DataFrame(
{
'Category': pred_df['Category'],
'Percent_Spend': pred_df['Percent_Spend'],
'Deviation': pred_df['Deviation'],
'Raw_Score': predictions.flatten(),
'Prediction': ['Good' if pred >= 0.6 else 'Bad' for pred in predictions],
}
)
return (
results.sort_values('Percent_Spend', ascending=False),
total_spending,
income_level,
)
# Suggest spending pattern
def suggest_spending_pattern(results, total_spending, input_data, income_level):
results = results.copy()
suggested_spending = {}
bad_categories = results[results['Prediction'] == 'Bad']
good_categories = results[results['Prediction'] == 'Good']
baseline = BASELINE_UPPER if income_level == 'upper' else BASELINE_LOWER
if not bad_categories.empty:
total_to_redistribute = sum(
input_data[row['Category']]
* min(max(abs(row['Deviation']) * 0.1, 0.25), 0.50)
for _, row in bad_categories.iterrows()
if row['Category'] not in ['Household', 'Food']
)
good_total = sum(input_data[cat] for cat in good_categories['Category'])
distribution_weights = {
cat: input_data[cat] / good_total if good_total > 0 else 0
for cat in good_categories['Category']
}
for category in input_data:
original = float(input_data[category])
baseline_dollars = total_spending * (baseline[category] / 100)
if category in bad_categories['Category'].values and category not in [
'Household',
'Food',
]:
reduction = min(
max(
abs(
results[results['Category'] == category][
'Deviation'
].values[0]
)
* 0.1,
0.25,
),
0.50,
)
suggested = original * (1 - reduction)
else:
weight = distribution_weights.get(category, 0)
increase = total_to_redistribute * weight
suggested = max(
original + increase,
baseline_dollars if category in ['Household', 'Food'] else original,
)
suggested_spending[category] = (original, round(suggested, 2))
else:
suggested_spending = {
cat: (float(val), float(val)) for cat, val in input_data.items()
}
return suggested_spending
# Format for Mistral
def format_for_mistral(
results, suggested_spending, total_spending, income_level, input_data
):
return {
"total_spending": total_spending,
"income_level": income_level,
"categories": [
{
"category": row['Category'],
"percent_spend": round(row['Percent_Spend'], 2),
"actual_dollars": round(input_data[row['Category']], 2),
"deviation": round(row['Deviation'], 2),
"prediction": row['Prediction'],
"suggested_dollars": suggested_spending[row['Category']][1],
}
for _, row in results.iterrows()
],
}
# Get spending summary (Mistral API call)
def get_spending_summary(spending_data):
client = OpenAI(base_url="https://openrouter.ai/api/v1", api_key=API)
analysis_prompt = f"""
You are a financial counselor analyzing a ${spending_data['total_spending']} monthly budget for a {spending_data['income_level']} income individual. Follow these strict rules:
### Financial Literacy Summary
#### Praise
For each 'Good' category:
⚠️ **Only show if ALL conditions met:**
- `prediction` = 'Good'
- `abs(deviation)` < 2%
✅ **{{category}} (${{actual_dollars}})** -
Explain using:
1. "% vs baseline: {{percent_spend}}% ({{deviation:+.2f}}% vs {{baseline}}%)"
2. Practical benefit
3. Savings impact ONLY if `deviation` > 0
#### Suggestions
⚠️ **Only show if ALL conditions met:**
- `prediction` = 'Bad'
- `abs(deviation)` > 2%
- `suggested_dollars` ≠ `actual_dollars`
For each 'Bad' category:
⚠️ **{{category}} (${{actual_dollars}} → ${{suggested_dollars}})** -
Structure as:
1. If suggested INCREASE: "Prioritize {{category}} by adding ${{suggested_dollars - actual_dollars}}..."
2. If suggested DECREASE: "Reduce {{category}} by ${{actual_dollars - suggested_dollars}}..."
#### Key Principle
Identify the MOST URGENT issue using largest absolute deviation...
**Baseline Reference ({spending_data['income_level'].capitalize()} Income):**
{'Food (10%), Household (11%), Shopping (13%), Transportation (11%), Health & Fitness (10%), Entertainment (18%), Beauty (8%), Investment (19%)' if spending_data['income_level'] == 'upper' else 'Food (40%), Household (30%), Shopping (7%), Transportation (5%), Health & Fitness (5%), Entertainment (5%), Beauty (4%), Investment (4%)'}
**Data:**
{json.dumps(spending_data, indent=2)}
**Begin Analysis:**
"""
try:
response = client.chat.completions.create(
model="mistralai/mistral-small-24b-instruct-2501:free",
messages=[{"role": "user", "content": analysis_prompt}],
temperature=0.5,
)
return response.choices[0].message.content
except Exception as e:
return f"Error calling Mistral API: {e}"
# Gradio interface function
def analyze_spending(
household,
food,
shopping,
transportation,
health_fitness,
entertainment,
beauty,
investment,
):
input_data = {
'Household': float(household),
'Food': float(food),
'Shopping': float(shopping),
'Transportation': float(transportation),
'Health & Fitness': float(health_fitness),
'Entertainment': float(entertainment),
'Beauty': float(beauty),
'Investment': float(investment),
}
logger.info("Before loading model")
model, scaler = load_financial_model()
logger.info("After loading model")
if model is None or scaler is None:
return "Error: Model or scaler failed to load.", None, None, None
results, total_spending, income_level = predict_spending_pattern(
model, scaler, input_data
)
suggested_spending = suggest_spending_pattern(
results, total_spending, input_data, income_level
)
spending_data = format_for_mistral(
results, suggested_spending, total_spending, income_level, input_data
)
summary = get_spending_summary(spending_data)
# Format suggested adjustments as a DataFrame
suggested_df = pd.DataFrame(
[(cat, orig, sugg) for cat, (orig, sugg) in suggested_spending.items()],
columns=['Category', 'Original ($)', 'Suggested ($)'],
)
return (
f"## Spending Analysis ({income_level.capitalize()} Income)\nTotal Spending: ${total_spending:.2f}",
results, # For DataFrame display
suggested_df, # For DataFrame display
summary, # Financial summary
)
# Gradio UI
logger.info("Setting up Gradio interface")
with gr.Blocks(
title="Personal Finance Assistant", css=".gr-button {margin-top: 10px}"
) as demo:
gr.Markdown("# Personal Finance Assistant")
gr.Markdown("Enter your monthly spending in each category ($):")
with gr.Row():
household = gr.Textbox(label="Household", value="500")
food = gr.Textbox(label="Food", value="100")
shopping = gr.Textbox(label="Shopping", value="950")
transportation = gr.Textbox(label="Transportation", value="100")
with gr.Row():
health_fitness = gr.Textbox(label="Health & Fitness", value="200")
entertainment = gr.Textbox(label="Entertainment", value="200")
beauty = gr.Textbox(label="Beauty", value="100")
investment = gr.Textbox(label="Investment", value="100")
submit_btn = gr.Button("Analyze")
# Output components
with gr.Column():
loading = gr.Markdown("### Analysis Results\n*Waiting for input...*")
title = gr.Markdown()
current_spending = gr.DataFrame(label="Current Spending")
suggested_adjustments = gr.DataFrame(label="Suggested Adjustments")
financial_summary = gr.Markdown()
# Handle click with loading state
def start_loading():
return "### Analysis Results\n*Processing your spending data...*"
submit_btn.click(fn=start_loading, inputs=None, outputs=loading).then(
fn=analyze_spending,
inputs=[
household,
food,
shopping,
transportation,
health_fitness,
entertainment,
beauty,
investment,
],
outputs=[title, current_spending, suggested_adjustments, financial_summary],
queue=True,
)
logger.info("Launching Gradio server")
demo.launch(server_name="0.0.0.0", server_port=7860)
|