File size: 2,659 Bytes
18ecf97
 
d44fae5
bc25884
 
8992627
 
 
 
bc25884
8992627
2684e23
 
 
bc25884
 
8992627
18ecf97
aeb566a
 
 
cd6842a
bc25884
 
 
 
 
18ecf97
8992627
 
 
bc25884
 
 
 
 
8992627
bc25884
8992627
bc25884
 
 
8992627
bc25884
 
8992627
bc25884
 
 
8992627
bc25884
 
8992627
cd6842a
bc25884
 
 
 
 
 
8992627
cd6842a
 
bc25884
 
 
 
 
aeb566a
bc25884
cd6842a
aeb566a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18ecf97
aeb566a
 
 
bc25884
aeb566a
bc25884
aeb566a
 
bc25884
aeb566a
 
 
bc25884
 
 
 
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
import os
import gradio as gr
import spaces
from google import genai

# Connect to Gemini
client = genai.Client(
    api_key=os.getenv("GEMINI_API_KEY")
)

# Required for ZeroGPU Spaces
@spaces.GPU(duration=5)
def gpu_placeholder():
    return "ready"


def finance_helper(income, expenses, cash, investments, debt, goal):

    # Show this immediately after Submit is clicked
    yield "## ⏳ Analysing your finances..."

    try:
        income = float(income or 0)
        expenses = float(expenses or 0)
        cash = float(cash or 0)
        investments = float(investments or 0)
        debt = float(debt or 0)

        surplus = income - expenses
        savings_rate = surplus / income * 100 if income > 0 else 0
        emergency_months = cash / expenses if expenses > 0 else 0

        prompt = f"""
You are a friendly personal finance education assistant.

Provide general educational guidance only.
Do not recommend specific financial products or investments.

Financial information:

Monthly income: ${income:,.0f}
Monthly expenses: ${expenses:,.0f}
Monthly surplus: ${surplus:,.0f}
Savings rate: {savings_rate:.1f}%
Cash savings: ${cash:,.0f}
Emergency fund: {emergency_months:.1f} months
Investments: ${investments:,.0f}
Debt: ${debt:,.0f}

Main financial goal:
{goal}

Please provide:

## Financial Snapshot
## What Looks Good
## Three Priorities
## One Thing to Watch
## Questions to Think About

Keep the response practical and concise.
"""

        response = client.models.generate_content(
            model="gemini-3.6-flash",
            contents=prompt
        )

        yield response.text

    except Exception as e:
        yield f"## Error\n\n{str(e)}"


demo = gr.Interface(
    fn=finance_helper,

    inputs=[
        gr.Number(
            label="Monthly Income",
            value=10000
        ),
        gr.Number(
            label="Monthly Expenses",
            value=6000
        ),
        gr.Number(
            label="Cash Savings",
            value=30000
        ),
        gr.Number(
            label="Investments",
            value=50000
        ),
        gr.Number(
            label="Debt",
            value=10000
        ),
        gr.Textbox(
            label="Main Financial Goal",
            value="Build enough savings for a home purchase in five years"
        )
    ],

    outputs=gr.Markdown(
        label="AI Financial Review"
    ),

    title="💰 Personal Finance Helper",

    description="""
Enter a few details about your finances and receive an AI generated financial health check.

For educational purposes only. This is not financial advice.
"""
)

demo.launch(
    ssr_mode=False
)