Spaces:
Runtime error
Runtime error
Updated app.py
Browse files
app.py
CHANGED
|
@@ -11,24 +11,19 @@ MODAL_URL = "https://devsam2898--personal-investment-strategist-optimized-web.mo
|
|
| 11 |
HEALTH_URL = "https://devsam2898--personal-investment-strategist-optimized-web.modal.run/health"
|
| 12 |
TEST_URL = "https://devsam2898--personal-investment-strategist-optimized-web.modal.run/test"
|
| 13 |
|
| 14 |
-
async def get_investment_strategy_async(age_group, income, expenses, risk_profile, goal, timeframe, country):
|
| 15 |
-
"""Async version with better timeout handling"""
|
| 16 |
|
| 17 |
# Input validation
|
| 18 |
if not all([age_group, income, expenses, risk_profile, goal, timeframe, country]):
|
| 19 |
return "❌ Please fill in all fields to get a personalized strategy."
|
| 20 |
|
| 21 |
-
# Convert income and
|
| 22 |
-
# app.py - inside get_investment_strategy_async function
|
| 23 |
-
|
| 24 |
try:
|
| 25 |
income_val = float(str(income).replace('$', '').replace(',', '')) if income else 0
|
| 26 |
expenses_val = float(str(expenses).replace('$', '').replace(',', '')) if expenses else 0
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
assets_val = float(current_assets) if current_assets is not None and str(current_assets).strip() != "" else 0.0
|
| 30 |
-
liabilities_val = float(current_liabilities) if current_liabilities is not None and str(current_liabilities).strip() != "" else 0.0
|
| 31 |
-
|
| 32 |
except ValueError:
|
| 33 |
return "❌ Please enter valid numbers for income, expenses, assets, and liabilities."
|
| 34 |
|
|
@@ -37,6 +32,10 @@ async def get_investment_strategy_async(age_group, income, expenses, risk_profil
|
|
| 37 |
return "❌ Income must be greater than 0."
|
| 38 |
if expenses_val < 0:
|
| 39 |
return "❌ Expenses cannot be negative."
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if expenses_val >= income_val:
|
| 41 |
return "⚠️ **Warning**: Your expenses are equal to or exceed your income. Consider budgeting advice before investing."
|
| 42 |
|
|
@@ -45,17 +44,17 @@ async def get_investment_strategy_async(age_group, income, expenses, risk_profil
|
|
| 45 |
"age_group": age_group,
|
| 46 |
"income": income_val,
|
| 47 |
"expenses": expenses_val,
|
|
|
|
|
|
|
| 48 |
"risk_profile": risk_profile,
|
| 49 |
"goal": goal,
|
| 50 |
"timeframe": timeframe,
|
| 51 |
-
"country": country
|
| 52 |
-
"current_assets": assets_val,
|
| 53 |
-
"current_liabilities": liabilities_val # Placeholder, can be extended later
|
| 54 |
}
|
| 55 |
}
|
| 56 |
|
| 57 |
try:
|
| 58 |
-
print(f"Sending request to: {MODAL_URL}")
|
| 59 |
|
| 60 |
# Use aiohttp for better async handling
|
| 61 |
timeout = aiohttp.ClientTimeout(total=150) # 2.5 minute timeout
|
|
@@ -125,13 +124,13 @@ An unexpected error occurred: {str(e)}
|
|
| 125 |
|
| 126 |
Please try again or contact support if the issue persists."""
|
| 127 |
|
| 128 |
-
def get_investment_strategy(age_group, income, expenses, risk_profile, goal, timeframe, country):
|
| 129 |
"""Sync wrapper for async function"""
|
| 130 |
try:
|
| 131 |
loop = asyncio.new_event_loop()
|
| 132 |
asyncio.set_event_loop(loop)
|
| 133 |
result = loop.run_until_complete(
|
| 134 |
-
get_investment_strategy_async(age_group, income, expenses, risk_profile, goal, timeframe, country)
|
| 135 |
)
|
| 136 |
loop.close()
|
| 137 |
return result
|
|
@@ -397,7 +396,7 @@ custom_css = """
|
|
| 397 |
}
|
| 398 |
|
| 399 |
.tips-section h4 {
|
| 400 |
-
color: #
|
| 401 |
font-weight: 700 !important;
|
| 402 |
margin-bottom: 1rem !important;
|
| 403 |
}
|
|
@@ -511,17 +510,18 @@ with gr.Blocks(
|
|
| 511 |
|
| 512 |
country = gr.Dropdown(
|
| 513 |
choices=[
|
| 514 |
-
"
|
| 515 |
-
"
|
| 516 |
],
|
| 517 |
label="🌍 Country of Residence",
|
| 518 |
-
value="
|
| 519 |
info="Tax jurisdiction for investment recommendations"
|
| 520 |
)
|
| 521 |
|
| 522 |
# Financial Information Section
|
| 523 |
with gr.Group(elem_classes="form-section"):
|
| 524 |
gr.HTML("<h3>💰 Financial Overview</h3>")
|
|
|
|
| 525 |
with gr.Row():
|
| 526 |
income = gr.Number(
|
| 527 |
label="💵 Monthly Income ($)",
|
|
@@ -535,25 +535,26 @@ with gr.Blocks(
|
|
| 535 |
minimum=0,
|
| 536 |
info="Total monthly living expenses and obligations"
|
| 537 |
)
|
|
|
|
| 538 |
with gr.Row():
|
| 539 |
current_assets = gr.Number(
|
| 540 |
-
label="
|
| 541 |
-
value=
|
| 542 |
minimum=0,
|
| 543 |
-
info="
|
| 544 |
)
|
| 545 |
current_liabilities = gr.Number(
|
| 546 |
-
label="
|
| 547 |
-
value=
|
| 548 |
minimum=0,
|
| 549 |
-
info="
|
| 550 |
)
|
| 551 |
|
| 552 |
# Financial health indicator
|
| 553 |
gr.HTML("""
|
| 554 |
<div style="background: linear-gradient(90deg, #10b981, #059669); color: white;
|
| 555 |
padding: 1rem; border-radius: 8px; margin-top: 1rem; font-size: 0.9rem;">
|
| 556 |
-
💡 <strong>Quick Tip:</strong> A healthy savings rate is typically 20% or more of your income
|
| 557 |
</div>
|
| 558 |
""")
|
| 559 |
|
|
@@ -562,9 +563,9 @@ with gr.Blocks(
|
|
| 562 |
gr.HTML("<h3>🎯 Investment Strategy</h3>")
|
| 563 |
|
| 564 |
risk_profile = gr.Radio(
|
| 565 |
-
choices=["
|
| 566 |
label="📊 Risk Tolerance",
|
| 567 |
-
value="
|
| 568 |
info="How comfortable are you with potential investment losses?"
|
| 569 |
)
|
| 570 |
|
|
@@ -618,7 +619,7 @@ with gr.Blocks(
|
|
| 618 |
</div>
|
| 619 |
<div style="display: flex; align-items: start; gap: 0.5rem;">
|
| 620 |
<span style="color: #10b981; font-size: 1.2rem;">✓</span>
|
| 621 |
-
<span><strong>
|
| 622 |
</div>
|
| 623 |
</div>
|
| 624 |
""")
|
|
@@ -636,6 +637,7 @@ with gr.Blocks(
|
|
| 636 |
🏦 **Account Type Suggestions**
|
| 637 |
💡 **Tax Optimization Strategies**
|
| 638 |
📈 **Risk Management Advice**
|
|
|
|
| 639 |
|
| 640 |
*Your data is processed securely and never stored permanently.*
|
| 641 |
""",
|
|
@@ -645,7 +647,7 @@ with gr.Blocks(
|
|
| 645 |
# Event handlers
|
| 646 |
submit_btn.click(
|
| 647 |
fn=get_investment_strategy,
|
| 648 |
-
inputs=[age_group, income, expenses, risk_profile, goal, timeframe, country],
|
| 649 |
outputs=output,
|
| 650 |
show_progress=True
|
| 651 |
)
|
|
|
|
| 11 |
HEALTH_URL = "https://devsam2898--personal-investment-strategist-optimized-web.modal.run/health"
|
| 12 |
TEST_URL = "https://devsam2898--personal-investment-strategist-optimized-web.modal.run/test"
|
| 13 |
|
| 14 |
+
async def get_investment_strategy_async(age_group, income, expenses, current_assets, current_liabilities, risk_profile, goal, timeframe, country):
|
| 15 |
+
"""Async version with better timeout handling and assets/liabilities support"""
|
| 16 |
|
| 17 |
# Input validation
|
| 18 |
if not all([age_group, income, expenses, risk_profile, goal, timeframe, country]):
|
| 19 |
return "❌ Please fill in all fields to get a personalized strategy."
|
| 20 |
|
| 21 |
+
# Convert income, expenses, assets, and liabilities to numbers
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
income_val = float(str(income).replace('$', '').replace(',', '')) if income else 0
|
| 24 |
expenses_val = float(str(expenses).replace('$', '').replace(',', '')) if expenses else 0
|
| 25 |
+
assets_val = float(str(current_assets).replace('$', '').replace(',', '')) if current_assets else 0
|
| 26 |
+
liabilities_val = float(str(current_liabilities).replace('$', '').replace(',', '')) if current_liabilities else 0
|
|
|
|
|
|
|
|
|
|
| 27 |
except ValueError:
|
| 28 |
return "❌ Please enter valid numbers for income, expenses, assets, and liabilities."
|
| 29 |
|
|
|
|
| 32 |
return "❌ Income must be greater than 0."
|
| 33 |
if expenses_val < 0:
|
| 34 |
return "❌ Expenses cannot be negative."
|
| 35 |
+
if assets_val < 0:
|
| 36 |
+
return "❌ Assets cannot be negative."
|
| 37 |
+
if liabilities_val < 0:
|
| 38 |
+
return "❌ Liabilities cannot be negative."
|
| 39 |
if expenses_val >= income_val:
|
| 40 |
return "⚠️ **Warning**: Your expenses are equal to or exceed your income. Consider budgeting advice before investing."
|
| 41 |
|
|
|
|
| 44 |
"age_group": age_group,
|
| 45 |
"income": income_val,
|
| 46 |
"expenses": expenses_val,
|
| 47 |
+
"current_assets": assets_val,
|
| 48 |
+
"current_liabilities": liabilities_val,
|
| 49 |
"risk_profile": risk_profile,
|
| 50 |
"goal": goal,
|
| 51 |
"timeframe": timeframe,
|
| 52 |
+
"country": country
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
}
|
| 55 |
|
| 56 |
try:
|
| 57 |
+
print(f"🚀 Sending request to: {MODAL_URL}")
|
| 58 |
|
| 59 |
# Use aiohttp for better async handling
|
| 60 |
timeout = aiohttp.ClientTimeout(total=150) # 2.5 minute timeout
|
|
|
|
| 124 |
|
| 125 |
Please try again or contact support if the issue persists."""
|
| 126 |
|
| 127 |
+
def get_investment_strategy(age_group, income, expenses, current_assets, current_liabilities, risk_profile, goal, timeframe, country):
|
| 128 |
"""Sync wrapper for async function"""
|
| 129 |
try:
|
| 130 |
loop = asyncio.new_event_loop()
|
| 131 |
asyncio.set_event_loop(loop)
|
| 132 |
result = loop.run_until_complete(
|
| 133 |
+
get_investment_strategy_async(age_group, income, expenses, current_assets, current_liabilities, risk_profile, goal, timeframe, country)
|
| 134 |
)
|
| 135 |
loop.close()
|
| 136 |
return result
|
|
|
|
| 396 |
}
|
| 397 |
|
| 398 |
.tips-section h4 {
|
| 399 |
+
color: #ffffff !important;
|
| 400 |
font-weight: 700 !important;
|
| 401 |
margin-bottom: 1rem !important;
|
| 402 |
}
|
|
|
|
| 510 |
|
| 511 |
country = gr.Dropdown(
|
| 512 |
choices=[
|
| 513 |
+
"United States", "Canada", "United Kingdom",
|
| 514 |
+
"Germany", "France", "Italy", "Japan", "India"
|
| 515 |
],
|
| 516 |
label="🌍 Country of Residence",
|
| 517 |
+
value="United States",
|
| 518 |
info="Tax jurisdiction for investment recommendations"
|
| 519 |
)
|
| 520 |
|
| 521 |
# Financial Information Section
|
| 522 |
with gr.Group(elem_classes="form-section"):
|
| 523 |
gr.HTML("<h3>💰 Financial Overview</h3>")
|
| 524 |
+
|
| 525 |
with gr.Row():
|
| 526 |
income = gr.Number(
|
| 527 |
label="💵 Monthly Income ($)",
|
|
|
|
| 535 |
minimum=0,
|
| 536 |
info="Total monthly living expenses and obligations"
|
| 537 |
)
|
| 538 |
+
|
| 539 |
with gr.Row():
|
| 540 |
current_assets = gr.Number(
|
| 541 |
+
label="💎 Current Assets ($)",
|
| 542 |
+
value=25000,
|
| 543 |
minimum=0,
|
| 544 |
+
info="Total value of current investments, savings, property, etc."
|
| 545 |
)
|
| 546 |
current_liabilities = gr.Number(
|
| 547 |
+
label="💳 Current Liabilities ($)",
|
| 548 |
+
value=10000,
|
| 549 |
minimum=0,
|
| 550 |
+
info="Total debt including credit cards, loans, mortgages, etc."
|
| 551 |
)
|
| 552 |
|
| 553 |
# Financial health indicator
|
| 554 |
gr.HTML("""
|
| 555 |
<div style="background: linear-gradient(90deg, #10b981, #059669); color: white;
|
| 556 |
padding: 1rem; border-radius: 8px; margin-top: 1rem; font-size: 0.9rem;">
|
| 557 |
+
💡 <strong>Quick Tip:</strong> A healthy savings rate is typically 20% or more of your income. Consider your net worth (assets - liabilities) for better strategy planning.
|
| 558 |
</div>
|
| 559 |
""")
|
| 560 |
|
|
|
|
| 563 |
gr.HTML("<h3>🎯 Investment Strategy</h3>")
|
| 564 |
|
| 565 |
risk_profile = gr.Radio(
|
| 566 |
+
choices=["Conservative", "Moderate", "Aggressive"],
|
| 567 |
label="📊 Risk Tolerance",
|
| 568 |
+
value="Moderate",
|
| 569 |
info="How comfortable are you with potential investment losses?"
|
| 570 |
)
|
| 571 |
|
|
|
|
| 619 |
</div>
|
| 620 |
<div style="display: flex; align-items: start; gap: 0.5rem;">
|
| 621 |
<span style="color: #10b981; font-size: 1.2rem;">✓</span>
|
| 622 |
+
<span><strong>Include Assets & Debts:</strong> Complete financial picture enables better advice</span>
|
| 623 |
</div>
|
| 624 |
</div>
|
| 625 |
""")
|
|
|
|
| 637 |
🏦 **Account Type Suggestions**
|
| 638 |
💡 **Tax Optimization Strategies**
|
| 639 |
📈 **Risk Management Advice**
|
| 640 |
+
💳 **Debt Management Guidance**
|
| 641 |
|
| 642 |
*Your data is processed securely and never stored permanently.*
|
| 643 |
""",
|
|
|
|
| 647 |
# Event handlers
|
| 648 |
submit_btn.click(
|
| 649 |
fn=get_investment_strategy,
|
| 650 |
+
inputs=[age_group, income, expenses, current_assets, current_liabilities, risk_profile, goal, timeframe, country],
|
| 651 |
outputs=output,
|
| 652 |
show_progress=True
|
| 653 |
)
|