Ujeshhh commited on
Commit
4a5fcca
Β·
verified Β·
1 Parent(s): 7c2d1c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -32
app.py CHANGED
@@ -2,13 +2,15 @@ import gradio as gr
2
  import PyPDF2
3
  import google.generativeai as genai
4
  import re
5
- import io
 
6
 
7
  # πŸ” Gemini API Key
8
  GEMINI_API_KEY = "AIzaSyDnx_qUjGTFG1pv1otPUhNt_bGGv14aMDI"
9
  genai.configure(api_key=GEMINI_API_KEY)
10
 
11
- # πŸ“„ Extract text from PDFdef extract_text_from_pdf(file):
 
12
  try:
13
  reader = PyPDF2.PdfReader(file)
14
  text = ""
@@ -17,18 +19,25 @@ genai.configure(api_key=GEMINI_API_KEY)
17
  if content:
18
  text += content + "\n"
19
  return text.strip()
20
- except:
 
21
  return ""
22
 
 
23
  def extract_section(full_text, label):
24
  pattern = rf"\*\*\- {re.escape(label)}:\*\*\s*(.*?)(?=\n\*\*|\Z)"
25
  match = re.search(pattern, full_text, re.DOTALL)
26
  return match.group(1).strip() if match else "❓ Not found"
27
 
 
28
  def analyze_financial_data(file):
29
  text = extract_text_from_pdf(file)
 
30
  if not text:
31
- return ("⚠️ Failed to extract text. Ensure it’s not a scanned image PDF.", "", "", "", "", "", "", "", None)
 
 
 
32
 
33
  prompt = f"""
34
  Analyze the following Paytm transaction history and generate financial insights in the following structure:
@@ -48,64 +57,67 @@ def analyze_financial_data(file):
48
  response = model.generate_content(prompt)
49
  full_text = response.text.strip()
50
 
51
- # Generate downloadable report
52
- report_io = io.StringIO()
53
- report_io.write(full_text)
54
- report_bytes = io.BytesIO(report_io.getvalue().encode())
55
- report_bytes.name = "financial_analysis.txt"
56
 
57
  return (
58
- "βœ… Analysis Complete",
59
- text[:2000] + "..." if len(text) > 2000 else text, # Full text preview
60
  extract_section(full_text, "Monthly Income & Expenses"),
61
  extract_section(full_text, "Unnecessary Expense Categories"),
62
  extract_section(full_text, "Estimated Savings %"),
63
  extract_section(full_text, "Spending Trends"),
64
  extract_section(full_text, "Category-wise Expense Breakdown (Partial)"),
65
  extract_section(full_text, "Cost Control Suggestions"),
66
- report_bytes
67
  )
68
 
69
  except Exception as e:
70
  return (f"❌ Gemini Error: {e}", "", "", "", "", "", "", "", None)
71
 
72
- # 🎨 Gradio Interface
73
- with gr.Blocks(title="AI Finance Assistant") as demo:
74
- gr.Markdown("## πŸ’° AI-Powered Personal Finance Assistant")
75
- gr.Markdown("Upload your **Paytm transaction PDF** (text-based only) and get structured financial insights using **Gemini AI**.")
 
 
76
 
77
  with gr.Row():
78
- with gr.Column():
79
  pdf_input = gr.File(label="πŸ“‚ Upload PDF", file_types=[".pdf"])
80
- analyze_btn = gr.Button("πŸ” Analyze My Finances")
81
 
82
- with gr.Column():
83
  status = gr.Textbox(label="βœ… Status", interactive=False)
84
- download_btn = gr.File(label="πŸ“₯ Download Report", interactive=False)
85
 
86
- with gr.Accordion("πŸ“œ Extracted PDF Text (preview)", open=False):
87
- pdf_text = gr.Textbox(lines=10, label="Extracted Text", interactive=False)
88
 
89
  with gr.Row():
90
- income_box = gr.Textbox(label="πŸ’΅ Monthly Income & Expenses", lines=5, interactive=False)
91
- unnecessary_box = gr.Textbox(label="πŸ›’ Unnecessary Expenses", lines=5, interactive=False)
92
 
93
  with gr.Row():
94
- savings_box = gr.Textbox(label="πŸ’° Estimated Savings %", lines=2, interactive=False)
95
- trends_box = gr.Textbox(label="πŸ“ˆ Spending Trends", lines=5, interactive=False)
96
 
97
  with gr.Row():
98
- breakdown_box = gr.Textbox(label="πŸ“Š Category-wise Breakdown", lines=6, interactive=False)
99
- suggestion_box = gr.Textbox(label="🧠 Cost Control Suggestions", lines=6, interactive=False)
100
 
101
  analyze_btn.click(
102
  fn=analyze_financial_data,
103
  inputs=pdf_input,
104
  outputs=[
105
- status, pdf_text,
106
- income_box, unnecessary_box, savings_box,
107
- trends_box, breakdown_box, suggestion_box, download_btn
 
 
108
  ]
109
  )
110
 
111
- demo.launch()
 
2
  import PyPDF2
3
  import google.generativeai as genai
4
  import re
5
+ import tempfile
6
+ import os
7
 
8
  # πŸ” Gemini API Key
9
  GEMINI_API_KEY = "AIzaSyDnx_qUjGTFG1pv1otPUhNt_bGGv14aMDI"
10
  genai.configure(api_key=GEMINI_API_KEY)
11
 
12
+ # πŸ“„ Extract text from PDF
13
+ def extract_text_from_pdf(file):
14
  try:
15
  reader = PyPDF2.PdfReader(file)
16
  text = ""
 
19
  if content:
20
  text += content + "\n"
21
  return text.strip()
22
+ except Exception as e:
23
+ print("PDF Extraction Error:", e)
24
  return ""
25
 
26
+ # βœ‚οΈ Extract sections from full text using regex
27
  def extract_section(full_text, label):
28
  pattern = rf"\*\*\- {re.escape(label)}:\*\*\s*(.*?)(?=\n\*\*|\Z)"
29
  match = re.search(pattern, full_text, re.DOTALL)
30
  return match.group(1).strip() if match else "❓ Not found"
31
 
32
+ # 🧠 Main function to analyze financial data
33
  def analyze_financial_data(file):
34
  text = extract_text_from_pdf(file)
35
+
36
  if not text:
37
+ return (
38
+ "⚠️ Failed to extract text. Ensure it’s a text-based PDF.",
39
+ "", "", "", "", "", "", "", None
40
+ )
41
 
42
  prompt = f"""
43
  Analyze the following Paytm transaction history and generate financial insights in the following structure:
 
57
  response = model.generate_content(prompt)
58
  full_text = response.text.strip()
59
 
60
+ # Save report to temporary .txt file
61
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as tmp:
62
+ tmp.write(full_text)
63
+ report_path = tmp.name
 
64
 
65
  return (
66
+ "βœ… Analysis Complete!",
67
+ text[:2000] + "..." if len(text) > 2000 else text,
68
  extract_section(full_text, "Monthly Income & Expenses"),
69
  extract_section(full_text, "Unnecessary Expense Categories"),
70
  extract_section(full_text, "Estimated Savings %"),
71
  extract_section(full_text, "Spending Trends"),
72
  extract_section(full_text, "Category-wise Expense Breakdown (Partial)"),
73
  extract_section(full_text, "Cost Control Suggestions"),
74
+ report_path
75
  )
76
 
77
  except Exception as e:
78
  return (f"❌ Gemini Error: {e}", "", "", "", "", "", "", "", None)
79
 
80
+ # 🎨 Gradio UI
81
+ with gr.Blocks(title="AI Financial Analyzer", theme=gr.themes.Soft()) as demo:
82
+ gr.Markdown("""
83
+ # πŸ’Έ AI-Powered Personal Finance Analyzer
84
+ Upload your **Paytm/YouTube Income PDF** and get structured financial insights using **Gemini AI**.
85
+ """)
86
 
87
  with gr.Row():
88
+ with gr.Column(scale=1):
89
  pdf_input = gr.File(label="πŸ“‚ Upload PDF", file_types=[".pdf"])
90
+ analyze_btn = gr.Button("πŸ” Analyze")
91
 
92
+ with gr.Column(scale=1):
93
  status = gr.Textbox(label="βœ… Status", interactive=False)
94
+ download_btn = gr.File(label="πŸ“₯ Download AI Report", interactive=False)
95
 
96
+ with gr.Accordion("πŸ“œ View Extracted PDF Text (Optional)", open=False):
97
+ extracted_text = gr.Textbox(label="πŸ“ Extracted Text", lines=10, interactive=False)
98
 
99
  with gr.Row():
100
+ income_expense = gr.Textbox(label="πŸ’΅ Monthly Income & Expenses", lines=4, interactive=False)
101
+ unnecessary = gr.Textbox(label="πŸ›’ Unnecessary Expenses", lines=4, interactive=False)
102
 
103
  with gr.Row():
104
+ savings = gr.Textbox(label="πŸ’° Estimated Savings %", lines=2, interactive=False)
105
+ trends = gr.Textbox(label="πŸ“ˆ Spending Trends", lines=4, interactive=False)
106
 
107
  with gr.Row():
108
+ category_breakdown = gr.Textbox(label="πŸ“Š Category-wise Breakdown", lines=6, interactive=False)
109
+ suggestions = gr.Textbox(label="🧠 Cost Control Suggestions", lines=6, interactive=False)
110
 
111
  analyze_btn.click(
112
  fn=analyze_financial_data,
113
  inputs=pdf_input,
114
  outputs=[
115
+ status, extracted_text,
116
+ income_expense, unnecessary,
117
+ savings, trends,
118
+ category_breakdown, suggestions,
119
+ download_btn
120
  ]
121
  )
122
 
123
+ demo.launch(share=True)