Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -2,296 +2,143 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
-
# --- Configuration
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
DEFAULT_API_KEY = ""
|
| 9 |
GEMINI_API_KEY_ENV_VAR = os.environ.get("GEMINI_API_KEY", DEFAULT_API_KEY)
|
| 10 |
|
| 11 |
# Model configuration
|
| 12 |
GEMINI_MODEL_NAME = "gemini-2.5-flash"
|
| 13 |
GEMINI_API_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 14 |
|
| 15 |
-
|
| 16 |
-
GENERIC_FINANCIAL_DATA = {
|
| 17 |
-
"salary": 100000,
|
| 18 |
-
"sip": 30000,
|
| 19 |
-
"bills": 25000,
|
| 20 |
-
"shopping": 5000,
|
| 21 |
-
"dining": 5000,
|
| 22 |
-
"entertainment": 5000,
|
| 23 |
-
"travel": 5000,
|
| 24 |
-
"other_expenses": 5000,
|
| 25 |
-
"target_surplus_percentage": 10,
|
| 26 |
-
}
|
| 27 |
-
|
| 28 |
-
def submit_feedback(feedback_text):
|
| 29 |
-
"""
|
| 30 |
-
Handles user suggestions/feedback. Prints to the console and provides UI confirmation.
|
| 31 |
-
"""
|
| 32 |
-
if feedback_text:
|
| 33 |
-
# Print to application logs (simulating saving to a common file/log)
|
| 34 |
-
print(f"--- USER FEEDBACK RECEIVED ---\n{feedback_text}\n------------------------------")
|
| 35 |
-
gr.Info("Thank you! Your suggestion has been logged.")
|
| 36 |
-
return "" # Clear the input box
|
| 37 |
-
|
| 38 |
-
def generate_portfolio_download(
|
| 39 |
-
salary, sip, bills, shopping, dining, entertainment, travel, other_expenses,
|
| 40 |
-
target_surplus_percentage, ai_response
|
| 41 |
-
):
|
| 42 |
-
"""
|
| 43 |
-
Generates the content for the download button.
|
| 44 |
-
"""
|
| 45 |
-
total_income = salary
|
| 46 |
-
total_fixed_expenses = sip + bills
|
| 47 |
-
total_discretionary_expenses = shopping + dining + entertainment + travel + other_expenses
|
| 48 |
-
total_expenses = total_fixed_expenses + total_discretionary_expenses
|
| 49 |
-
net_cashflow = total_income - total_expenses
|
| 50 |
-
target_surplus_amount = (target_surplus_percentage / 100) * total_income
|
| 51 |
-
|
| 52 |
-
portfolio_content = f"""
|
| 53 |
-
# Personal Financial Health Report
|
| 54 |
-
|
| 55 |
-
## 1. Input Data
|
| 56 |
-
| Category | Monthly Amount (₹) |
|
| 57 |
-
| :--- | :--- |
|
| 58 |
-
| **Gross Monthly Salary** | ₹{salary:,.0f} |
|
| 59 |
-
| **Fixed/Commitment Expenses** | |
|
| 60 |
-
| Mandatory SIP/Investment | ₹{sip:,.0f} |
|
| 61 |
-
| Bills/EMIs/Rent | ₹{bills:,.0f} |
|
| 62 |
-
| **Discretionary Expenses** | |
|
| 63 |
-
| Shopping | ₹{shopping:,.0f} |
|
| 64 |
-
| Dining & Food Delivery | ₹{dining:,.0f} |
|
| 65 |
-
| Entertainment/Subscriptions | ₹{entertainment:,.0f} |
|
| 66 |
-
| Travel/Fuel | ₹{travel:,.0f} |
|
| 67 |
-
| Other Expenses | ₹{other_expenses:,.0f} |
|
| 68 |
-
| **Target Savings Goal** | {target_surplus_percentage}% of Salary |
|
| 69 |
-
|
| 70 |
-
## 2. Calculated Summary
|
| 71 |
-
- **Total Income:** ₹{total_income:,.0f}
|
| 72 |
-
- **Total Expenses:** ₹{total_expenses:,.0f}
|
| 73 |
-
- **Net Cashflow (Target Surplus):** ₹{net_cashflow:,.0f}
|
| 74 |
-
- **Target Surplus Amount:** ₹{target_surplus_amount:,.0f}
|
| 75 |
-
|
| 76 |
-
## 3. AI Financial Coach Analysis
|
| 77 |
-
{ai_response}
|
| 78 |
-
|
| 79 |
-
---
|
| 80 |
-
Generated by the AI Personal Finance Agent.
|
| 81 |
-
"""
|
| 82 |
-
return portfolio_content
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
def personal_finance_agent(
|
| 86 |
-
salary, sip, bills, shopping, dining, entertainment, travel, other_expenses,
|
| 87 |
-
target_surplus_percentage,
|
| 88 |
-
prompt
|
| 89 |
-
):
|
| 90 |
"""
|
| 91 |
-
|
| 92 |
"""
|
| 93 |
|
| 94 |
-
# 1. API Key Check
|
| 95 |
final_api_key = GEMINI_API_KEY_ENV_VAR
|
| 96 |
-
|
| 97 |
-
|
| 98 |
if not final_api_key:
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
try:
|
| 103 |
-
client = OpenAI(
|
| 104 |
-
base_url=GEMINI_API_BASE_URL,
|
| 105 |
-
api_key=final_api_key,
|
| 106 |
-
timeout=60.0
|
| 107 |
-
)
|
| 108 |
-
except Exception as e:
|
| 109 |
-
ai_response = f"ERROR: Could not initialize API client. Detail: {e}"
|
| 110 |
-
else:
|
| 111 |
-
# 3. Calculations
|
| 112 |
-
total_income = salary
|
| 113 |
-
total_fixed_expenses = sip + bills
|
| 114 |
-
total_discretionary_expenses = shopping + dining + entertainment + travel + other_expenses
|
| 115 |
-
total_expenses = total_fixed_expenses + total_discretionary_expenses
|
| 116 |
-
net_cashflow = total_income - total_expenses
|
| 117 |
-
target_surplus_amount = (target_surplus_percentage / 100) * total_income
|
| 118 |
-
status = "Surplus" if net_cashflow > 0 else "Deficit"
|
| 119 |
-
|
| 120 |
-
# 4. Dynamic System Prompt Generation
|
| 121 |
-
system_prompt = f"""
|
| 122 |
-
You are a world-class, empathetic personal financial analyst and coach.
|
| 123 |
-
The user's current monthly financial summary: Income: ₹{total_income:,.0f},
|
| 124 |
-
Total Expenses: ₹{total_expenses:,.0f}, Net Cashflow: ₹{net_cashflow:,.0f},
|
| 125 |
-
Target Savings: ₹{target_surplus_amount:,.0f}.
|
| 126 |
-
Provide a summary, then give specific, positive, and practical recommendations
|
| 127 |
-
to help them achieve their savings goal. Focus on optimizing discretionary spending
|
| 128 |
-
(Shopping, Dining, Entertainment, Travel, Other). Use Indian Rupee (₹) formatting.
|
| 129 |
-
"""
|
| 130 |
-
|
| 131 |
-
# 5. User Query
|
| 132 |
-
user_query = f"""
|
| 133 |
-
Discretionary spending: Shopping: ₹{shopping:,.0f}, Dining: ₹{dining:,.0f},
|
| 134 |
-
Entertainment: ₹{entertainment:,.0f}, Travel: ₹{travel:,.0f}, Other: ₹{other_expenses:,.0f}.
|
| 135 |
-
User prompt: "{prompt}"
|
| 136 |
-
"""
|
| 137 |
-
|
| 138 |
-
try:
|
| 139 |
-
# 6. API Call
|
| 140 |
-
response = client.chat.completions.create(
|
| 141 |
-
model=GEMINI_MODEL_NAME,
|
| 142 |
-
messages=[
|
| 143 |
-
{"role": "system", "content": system_prompt},
|
| 144 |
-
{"role": "user", "content": user_query}
|
| 145 |
-
],
|
| 146 |
-
temperature=0.7,
|
| 147 |
-
)
|
| 148 |
-
ai_response = response.choices[0].message.content
|
| 149 |
-
|
| 150 |
-
except Exception as e:
|
| 151 |
-
ai_response = f"An API error occurred during content generation: {e}"
|
| 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 |
# --- Gradio UI Definition ---
|
| 182 |
|
| 183 |
-
with gr.Blocks(title="
|
| 184 |
-
gr.Markdown("#
|
| 185 |
gr.Markdown(
|
| 186 |
"""
|
| 187 |
-
|
| 188 |
-
|
| 189 |
"""
|
| 190 |
)
|
| 191 |
-
|
| 192 |
-
with gr.Row():
|
| 193 |
-
# Configuration Column (Download & Feedback)
|
| 194 |
-
with gr.Column(scale=1):
|
| 195 |
-
gr.Markdown("## ⚙️ Tools")
|
| 196 |
-
|
| 197 |
-
# Download Button - now an output, content generated by the main function
|
| 198 |
-
download_btn = gr.DownloadButton(
|
| 199 |
-
label="Download Portfolio Report (.txt)",
|
| 200 |
-
scale=0
|
| 201 |
-
)
|
| 202 |
-
|
| 203 |
-
gr.Markdown("---")
|
| 204 |
-
gr.Markdown("## 💬 Add Suggestion/Feedback")
|
| 205 |
-
|
| 206 |
-
# Feedback Input
|
| 207 |
-
feedback_input = gr.Textbox(
|
| 208 |
-
label="Your Suggestion",
|
| 209 |
-
placeholder="e.g., 'Add a feature to track daily expenses.'",
|
| 210 |
-
lines=3
|
| 211 |
-
)
|
| 212 |
-
feedback_button = gr.Button("Submit Feedback")
|
| 213 |
-
|
| 214 |
-
# Feedback action
|
| 215 |
-
feedback_button.click(
|
| 216 |
-
fn=submit_feedback,
|
| 217 |
-
inputs=[feedback_input],
|
| 218 |
-
outputs=[feedback_input]
|
| 219 |
-
)
|
| 220 |
-
|
| 221 |
-
# Financial Inputs Column
|
| 222 |
-
with gr.Column(scale=2):
|
| 223 |
-
gr.Markdown("## 💸 Your Monthly Finances (in ₹)")
|
| 224 |
-
|
| 225 |
-
# Input Sliders (simplified layout)
|
| 226 |
-
with gr.Row():
|
| 227 |
-
salary_input = gr.Slider(label="1. Gross Monthly Salary (Income)", minimum=10000, maximum=500000, value=GENERIC_FINANCIAL_DATA["salary"], step=1000, interactive=True)
|
| 228 |
-
sip_input = gr.Slider(label="2. Mandatory SIP/Investment", minimum=0, maximum=100000, value=GENERIC_FINANCIAL_DATA["sip"], step=500, interactive=True)
|
| 229 |
-
|
| 230 |
-
with gr.Row():
|
| 231 |
-
bills_input = gr.Slider(label="3. Bills/EMIs/Rent", minimum=0, maximum=200000, value=GENERIC_FINANCIAL_DATA["bills"], step=500, interactive=True)
|
| 232 |
-
shopping_input = gr.Slider(label="4. Shopping", minimum=0, maximum=50000, value=GENERIC_FINANCIAL_DATA["shopping"], step=100, interactive=True)
|
| 233 |
-
|
| 234 |
-
with gr.Row():
|
| 235 |
-
dining_input = gr.Slider(label="5. Dining & Food Delivery", minimum=0, maximum=50000, value=GENERIC_FINANCIAL_DATA["dining"], step=100, interactive=True)
|
| 236 |
-
entertainment_input = gr.Slider(label="6. Entertainment/Subscriptions", minimum=0, maximum=20000, value=GENERIC_FINANCIAL_DATA["entertainment"], step=50, interactive=True)
|
| 237 |
-
|
| 238 |
-
with gr.Row():
|
| 239 |
-
travel_input = gr.Slider(label="7. Travel/Fuel", minimum=0, maximum=50000, value=GENERIC_FINANCIAL_DATA["travel"], step=100, interactive=True)
|
| 240 |
-
other_expenses_input = gr.Slider(label="8. Other Expenses", minimum=0, maximum=30000, value=GENERIC_FINANCIAL_DATA["other_expenses"], step=100, interactive=True)
|
| 241 |
-
|
| 242 |
-
target_surplus_input = gr.Slider(
|
| 243 |
-
label="9. Target Savings Percentage (%)",
|
| 244 |
-
minimum=0,
|
| 245 |
-
maximum=50,
|
| 246 |
-
value=GENERIC_FINANCIAL_DATA["target_surplus_percentage"],
|
| 247 |
-
step=1,
|
| 248 |
-
interactive=True
|
| 249 |
-
)
|
| 250 |
-
|
| 251 |
-
gr.Markdown("---")
|
| 252 |
-
|
| 253 |
-
# --- Prompt and Action ---
|
| 254 |
-
prompt_input = gr.Textbox(
|
| 255 |
-
label="Your Specific Question (Optional)",
|
| 256 |
-
placeholder="e.g., How can I reduce my spending on food delivery?",
|
| 257 |
-
value="Analyze my current spending and suggest two actionable ways to increase my net cashflow."
|
| 258 |
-
)
|
| 259 |
-
|
| 260 |
-
run_button = gr.Button("💰 Analyze My Finances & Get Coaching")
|
| 261 |
-
|
| 262 |
-
# --- Output Section ---
|
| 263 |
-
gr.Markdown("## 📈 AI Financial Analysis & Coaching")
|
| 264 |
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
| 268 |
)
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
| 273 |
)
|
| 274 |
|
| 275 |
-
#
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
salary_input, sip_input, bills_input, shopping_input, dining_input,
|
| 280 |
-
entertainment_input, travel_input, other_expenses_input, target_surplus_input,
|
| 281 |
-
prompt_input
|
| 282 |
-
]
|
| 283 |
-
|
| 284 |
-
# List of output components (HTML, Markdown, Download Content)
|
| 285 |
-
outputs = [
|
| 286 |
-
financial_summary_output,
|
| 287 |
-
ai_response_output,
|
| 288 |
-
download_btn
|
| 289 |
-
]
|
| 290 |
-
|
| 291 |
-
# The main analysis flow: one click, one function, three distinct outputs
|
| 292 |
-
run_button.click(
|
| 293 |
-
fn=personal_finance_agent,
|
| 294 |
-
inputs=inputs,
|
| 295 |
-
outputs=outputs,
|
| 296 |
-
queue=False,
|
| 297 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
from openai import OpenAI
|
| 4 |
|
| 5 |
+
# --- Configuration ---
|
| 6 |
|
| 7 |
+
# API key MUST be provided via an environment variable (e.g., GEMINI_API_KEY).
|
| 8 |
+
DEFAULT_API_KEY = "AIzaSyAR235waLIezibSn72t90EeLFsSrOlMj5o"
|
| 9 |
GEMINI_API_KEY_ENV_VAR = os.environ.get("GEMINI_API_KEY", DEFAULT_API_KEY)
|
| 10 |
|
| 11 |
# Model configuration
|
| 12 |
GEMINI_MODEL_NAME = "gemini-2.5-flash"
|
| 13 |
GEMINI_API_BASE_URL = "https://generativelanguage.googleapis.com/v1beta/openai/"
|
| 14 |
|
| 15 |
+
def chatbot_query(message, history, rag_context):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
+
Handles the chat query using the provided context (rag_context).
|
| 18 |
"""
|
| 19 |
|
|
|
|
| 20 |
final_api_key = GEMINI_API_KEY_ENV_VAR
|
| 21 |
+
|
|
|
|
| 22 |
if not final_api_key:
|
| 23 |
+
response = "ERROR: API Key is missing. Please set the 'GEMINI_API_KEY' environment variable."
|
| 24 |
+
history.append([message, response])
|
| 25 |
+
return history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
try:
|
| 28 |
+
# Initialize the client using the Gemini base URL and the environment key
|
| 29 |
+
client = OpenAI(
|
| 30 |
+
base_url=GEMINI_API_BASE_URL,
|
| 31 |
+
api_key=final_api_key,
|
| 32 |
+
timeout=60.0
|
| 33 |
+
)
|
| 34 |
+
except Exception as e:
|
| 35 |
+
response = f"ERROR: Could not initialize API client. Detail: {e}"
|
| 36 |
+
history.append([message, response])
|
| 37 |
+
return history
|
| 38 |
+
|
| 39 |
+
# Construct System Prompt using the RAG Context
|
| 40 |
+
system_prompt = f"""
|
| 41 |
+
You are an expert personal financial assistant. Your goal is to analyze the user's expenses
|
| 42 |
+
and provide advice based on the context provided in the data box below.
|
| 43 |
+
Be helpful, empathetic, and focus on practical recommendations.
|
| 44 |
+
|
| 45 |
+
--- RAG/CONTEXT DATA ---
|
| 46 |
+
{rag_context}
|
| 47 |
+
------------------------
|
| 48 |
+
|
| 49 |
+
Address the user's question based on this data. If the data is empty or irrelevant,
|
| 50 |
+
provide general financial advice.
|
| 51 |
"""
|
| 52 |
|
| 53 |
+
# Prepare chat history for API call
|
| 54 |
+
messages = [{"role": "system", "content": system_prompt}]
|
| 55 |
+
|
| 56 |
+
# Append past conversation turns
|
| 57 |
+
for user_msg, ai_msg in history:
|
| 58 |
+
messages.append({"role": "user", "content": user_msg})
|
| 59 |
+
messages.append({"role": "assistant", "content": ai_msg})
|
| 60 |
+
|
| 61 |
+
# Append current user message
|
| 62 |
+
messages.append({"role": "user", "content": message})
|
| 63 |
+
|
| 64 |
+
try:
|
| 65 |
+
# API Call
|
| 66 |
+
response_stream = client.chat.completions.create(
|
| 67 |
+
model=GEMINI_MODEL_NAME,
|
| 68 |
+
messages=messages,
|
| 69 |
+
temperature=0.7,
|
| 70 |
+
stream=True # Use streaming for better user experience
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
full_response = ""
|
| 74 |
+
# Stream the response back to Gradio
|
| 75 |
+
for chunk in response_stream:
|
| 76 |
+
content = chunk.choices[0].delta.content
|
| 77 |
+
if content is not None:
|
| 78 |
+
full_response += content
|
| 79 |
+
history.append([message, full_response])
|
| 80 |
+
yield history
|
| 81 |
+
|
| 82 |
+
except Exception as e:
|
| 83 |
+
full_response = f"An API error occurred: {e}"
|
| 84 |
+
history.append([message, full_response])
|
| 85 |
+
yield history
|
| 86 |
+
|
| 87 |
+
# Final return (although streaming already handled the updates)
|
| 88 |
+
return history
|
| 89 |
|
| 90 |
|
| 91 |
# --- Gradio UI Definition ---
|
| 92 |
|
| 93 |
+
with gr.Blocks(title="Simple Personal Finance Chatbot") as demo:
|
| 94 |
+
gr.Markdown("# 💬 Simple Personal Finance Chatbot (Gemini Powered)")
|
| 95 |
gr.Markdown(
|
| 96 |
"""
|
| 97 |
+
Type your personal finance data (income, expenses, goals) into the **Context Box** below,
|
| 98 |
+
then ask the chatbot a question based on that data.
|
| 99 |
"""
|
| 100 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
|
| 102 |
+
# RAG/Context Data Input
|
| 103 |
+
rag_context = gr.Textbox(
|
| 104 |
+
label="Context Box: Manually Add Your Financial Data (e.g., 'My salary is 1,00,000. My rent is 30,000 and my food spending is 15,000.')",
|
| 105 |
+
lines=5,
|
| 106 |
+
placeholder="Enter your financial data here. The AI will use this as context for its advice.",
|
| 107 |
+
value="Monthly income: 85,000. Fixed expenses (rent, bills, SIP): 40,000. Discretionary spending: Food delivery: 10,000, Shopping: 8,000, Entertainment: 4,000. I want to save for a vacation next year."
|
| 108 |
)
|
| 109 |
+
|
| 110 |
+
# Chatbot Interface
|
| 111 |
+
chatbot = gr.Chatbot(
|
| 112 |
+
label="Financial Coach Chat",
|
| 113 |
+
height=350,
|
| 114 |
+
avatar_images=[None, "https://storage.googleapis.com/huggingface-org/spaces/gradio-pr-bot/chatbot-avatar.png"]
|
| 115 |
)
|
| 116 |
|
| 117 |
+
# Chat Input
|
| 118 |
+
msg = gr.Textbox(
|
| 119 |
+
label="Your Question",
|
| 120 |
+
placeholder="e.g., Based on my data, where can I cut costs? Or, How much should I allocate to my vacation fund?"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
)
|
| 122 |
+
|
| 123 |
+
# Action Buttons
|
| 124 |
+
with gr.Row():
|
| 125 |
+
clear_btn = gr.Button("Clear Chat")
|
| 126 |
+
submit_btn = gr.Button("Ask the Coach", variant="primary")
|
| 127 |
+
|
| 128 |
+
# Connect components
|
| 129 |
+
submit_btn.click(
|
| 130 |
+
fn=chatbot_query,
|
| 131 |
+
inputs=[msg, chatbot, rag_context],
|
| 132 |
+
outputs=[chatbot],
|
| 133 |
+
# Clears the input message box after submitting
|
| 134 |
+
queue=False
|
| 135 |
+
).then(lambda: "", None, msg)
|
| 136 |
+
|
| 137 |
+
msg.submit(
|
| 138 |
+
fn=chatbot_query,
|
| 139 |
+
inputs=[msg, chatbot, rag_context],
|
| 140 |
+
outputs=[chatbot],
|
| 141 |
+
queue=False
|
| 142 |
+
).then(lambda: "", None, msg)
|
| 143 |
+
|
| 144 |
+
clear_btn.click(lambda: ([], ""), None, [chatbot, msg], queue=False)
|