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 )