|
|
|
|
|
|
|
|
|
import gradio as gr |
|
import requests |
|
import json |
|
|
|
MODAL_URL = "https://devsam2898--personal-investment-strategist-optimized-web.modal.run/strategy" |
|
|
|
def get_investment_strategy(age_group, income, expenses, risk_profile, goal, timeframe): |
|
""" |
|
Get investment strategy from Modal backend |
|
""" |
|
|
|
if not all([age_group, income, expenses, risk_profile, goal, timeframe]): |
|
return "β Please fill in all fields to get a personalized strategy." |
|
|
|
|
|
try: |
|
income_val = float(income.replace('$', '').replace(',', '')) if isinstance(income, str) else float(income) |
|
expenses_val = float(expenses.replace('$', '').replace(',', '')) if isinstance(expenses, str) else float(expenses) |
|
except ValueError: |
|
return "β Please enter valid numbers for income and expenses." |
|
|
|
payload = { |
|
"profile": { |
|
"age_group": age_group, |
|
"income": income_val, |
|
"expenses": expenses_val, |
|
"risk_profile": risk_profile, |
|
"goal": goal, |
|
"timeframe": timeframe |
|
} |
|
} |
|
|
|
try: |
|
print(f"Sending request to: {MODAL_URL}") |
|
print(f"Payload: {json.dumps(payload, indent=2)}") |
|
|
|
response = requests.post( |
|
MODAL_URL, |
|
json=payload, |
|
headers={ |
|
'Content-Type': 'application/json', |
|
'Accept': 'application/json' |
|
}, |
|
timeout=30 |
|
) |
|
|
|
print(f"Response status: {response.status_code}") |
|
print(f"Response headers: {dict(response.headers)}") |
|
|
|
if response.status_code == 200: |
|
result = response.json() |
|
strategy = result.get("strategy", "No strategy returned.") |
|
return f"## π Your Personalized Investment Strategy\n\n{strategy}" |
|
else: |
|
error_msg = f"β **Error {response.status_code}**\n\n" |
|
try: |
|
error_detail = response.json() |
|
error_msg += f"Details: {error_detail}" |
|
except: |
|
error_msg += f"Response: {response.text}" |
|
return error_msg |
|
|
|
except requests.exceptions.Timeout: |
|
return "β **Request Timeout**: The service is taking too long to respond. Please try again." |
|
except requests.exceptions.ConnectionError: |
|
return "β **Connection Error**: Unable to connect to the backend service. Please check if the Modal service is running." |
|
except requests.exceptions.RequestException as e: |
|
return f"β **Network Error**: {str(e)}" |
|
except Exception as e: |
|
return f"β **Unexpected Error**: {str(e)}" |
|
|
|
|
|
def test_function(): |
|
return "β
Function is working correctly!" |
|
|
|
|
|
with gr.Blocks(theme="soft", title="π Personal Finance Strategist") as interface: |
|
gr.Markdown( |
|
""" |
|
# π Personal Finance Strategist |
|
**Powered by LlamaIndex + Nebius Qwen3** |
|
|
|
Get personalized investment strategies based on your life stage and financial goals. |
|
""" |
|
) |
|
|
|
with gr.Row(): |
|
with gr.Column(scale=1): |
|
age_group = gr.Dropdown( |
|
choices=["20s", "30s", "40s", "50s+"], |
|
label="Age Group", |
|
value="30s" |
|
) |
|
income = gr.Textbox( |
|
label="Monthly Income ($)", |
|
value="6000", |
|
placeholder="e.g., 6000" |
|
) |
|
expenses = gr.Textbox( |
|
label="Monthly Expenses ($)", |
|
value="4000", |
|
placeholder="e.g., 4000" |
|
) |
|
|
|
with gr.Column(scale=1): |
|
risk_profile = gr.Radio( |
|
choices=["Conservative", "Moderate", "Aggressive"], |
|
label="Risk Tolerance", |
|
value="Moderate" |
|
) |
|
goal = gr.Textbox( |
|
label="Financial Goal", |
|
placeholder="e.g., buy a house, retirement, emergency fund" |
|
) |
|
timeframe = gr.Textbox( |
|
label="Timeframe", |
|
placeholder="e.g., 5 years, 10 years" |
|
) |
|
|
|
with gr.Row(): |
|
submit_btn = gr.Button("Get Investment Strategy", variant="primary") |
|
test_btn = gr.Button("Test Connection", variant="secondary") |
|
|
|
output = gr.Markdown(label="Investment Strategy") |
|
|
|
|
|
submit_btn.click( |
|
fn=get_investment_strategy, |
|
inputs=[age_group, income, expenses, risk_profile, goal, timeframe], |
|
outputs=output |
|
) |
|
|
|
test_btn.click( |
|
fn=test_function, |
|
outputs=output |
|
) |
|
|
|
if __name__ == "__main__": |
|
interface.launch( |
|
server_name="0.0.0.0", |
|
server_port=7860, |
|
share=False |
|
) |