arshad1234321 commited on
Commit
55df5f6
Β·
verified Β·
1 Parent(s): 7f03bc8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -24
app.py CHANGED
@@ -2,8 +2,10 @@ import gradio as gr
2
  import PyPDF2
3
  import google.generativeai as genai
4
  import re
 
5
 
6
- GEMINI_API_KEY = "AIzaSyDSqhsofWeBd7FOV5YkdZGw0_OLH4UKBPo"
 
7
  genai.configure(api_key=GEMINI_API_KEY)
8
 
9
  def extract_text_from_pdf(file):
@@ -27,26 +29,27 @@ def analyze_financial_data(file):
27
  text = extract_text_from_pdf(file)
28
 
29
  if not text:
30
- return (
31
- "⚠️ Failed to extract text from the PDF. Ensure it’s not scanned.",
32
- "", "", "", "", "", ""
33
- )
34
 
35
  prompt = f"""
36
- Analyze the following Paytm transaction history and generate financial insights in the following structure:
 
37
  **Financial Insights**
38
- **- Monthly Income & Expenses:** [data]
39
- **- Unnecessary Expense Categories:** [data]
40
- **- Estimated Savings %:** [data]
41
- **- Spending Trends:** [data]
42
- **- Category-wise Expense Breakdown (Partial):** [data]
43
- **- Cost Control Suggestions:** [data]
 
44
  Transaction History:
45
  {text}
46
  """
47
 
48
  try:
49
- model = genai.GenerativeModel("gemini-1.5-flash")
50
  response = model.generate_content(prompt)
51
  full_text = response.text.strip()
52
 
@@ -65,16 +68,14 @@ def analyze_financial_data(file):
65
 
66
  gr.Interface(
67
  fn=analyze_financial_data,
68
- inputs=gr.File(label="πŸ“‚ Upload Paytm PDF", file_types=[".pdf"]),
69
  outputs=[
70
- gr.Textbox(label="βœ… Status", lines=2, interactive=False),
71
- gr.Textbox(label="πŸ’΅ Monthly Income & Expenses", lines=8, interactive=False),
72
- gr.Textbox(label="πŸ›’ Unnecessary Expense Categories", lines=8, interactive=False),
73
- gr.Textbox(label="πŸ’° Estimated Savings %", lines=4, interactive=False),
74
- gr.Textbox(label="πŸ“ˆ Spending Trends", lines=8, interactive=False),
75
- gr.Textbox(label="πŸ“Š Category-wise Breakdown", lines=10, interactive=False),
76
- gr.Textbox(label="🧠 Cost Control Suggestions", lines=8, interactive=False),
77
  ],
78
- title="πŸ’° AI-Powered Personal Finance Assistant",
79
- description="Upload your Paytm transaction PDF (text-based) and get structured financial insights using Gemini AI.",
80
- ).launch()
 
2
  import PyPDF2
3
  import google.generativeai as genai
4
  import re
5
+ import os
6
 
7
+ # Load API key safely
8
+ GEMINI_API_KEY = os.getenv("GEMINI_API_KEY") # set in environment
9
  genai.configure(api_key=GEMINI_API_KEY)
10
 
11
  def extract_text_from_pdf(file):
 
29
  text = extract_text_from_pdf(file)
30
 
31
  if not text:
32
+ return ("⚠️ Failed to extract text", "", "", "", "", "", "")
33
+
34
+ text = text[:6000] # LIMIT TOKENS (IMPORTANT)
 
35
 
36
  prompt = f"""
37
+ Analyze Paytm transactions and return structured insights.
38
+
39
  **Financial Insights**
40
+ **- Monthly Income & Expenses:** ...
41
+ **- Unnecessary Expense Categories:** ...
42
+ **- Estimated Savings %:** ...
43
+ **- Spending Trends:** ...
44
+ **- Category-wise Expense Breakdown (Partial):** ...
45
+ **- Cost Control Suggestions:** ...
46
+
47
  Transaction History:
48
  {text}
49
  """
50
 
51
  try:
52
+ model = genai.GenerativeModel("gemini-1.5-flash-latest")
53
  response = model.generate_content(prompt)
54
  full_text = response.text.strip()
55
 
 
68
 
69
  gr.Interface(
70
  fn=analyze_financial_data,
71
+ inputs=gr.File(label="Upload Paytm PDF", file_types=[".pdf"]),
72
  outputs=[
73
+ gr.Textbox(label="Status"),
74
+ gr.Textbox(label="Monthly Income & Expenses"),
75
+ gr.Textbox(label="Unnecessary Expense Categories"),
76
+ gr.Textbox(label="Estimated Savings %"),
77
+ gr.Textbox(label="Spending Trends"),
78
+ gr.Textbox(label="Category-wise Breakdown"),
79
+ gr.Textbox(label="Cost Control Suggestions"),
80
  ],
81
+ ).launch()