arshad1234321 commited on
Commit
0eb08e7
Β·
verified Β·
1 Parent(s): d16ebbe

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -0
app.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import PyPDF2
3
+ import google.generativeai as genai
4
+ import re
5
+
6
+ GEMINI_API_KEY = "AIzaSyDTHkwSJleaOpCVpgRlakxD1sXoCRM8K4c"
7
+ genai.configure(api_key=GEMINI_API_KEY)
8
+
9
+ def extract_text_from_pdf(file):
10
+ try:
11
+ reader = PyPDF2.PdfReader(file)
12
+ text = ""
13
+ for page in reader.pages:
14
+ content = page.extract_text()
15
+ if content:
16
+ text += content + "\n"
17
+ return text.strip()
18
+ except:
19
+ return ""
20
+
21
+ def extract_section(full_text, label):
22
+ pattern = rf"\*\*\- {re.escape(label)}:\*\*\s*(.*?)(?=\n\*\*|\Z)"
23
+ match = re.search(pattern, full_text, re.DOTALL)
24
+ return match.group(1).strip() if match else "❓ Not found"
25
+
26
+ 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
+
53
+ return (
54
+ "βœ… Analysis Complete",
55
+ extract_section(full_text, "Monthly Income & Expenses"),
56
+ extract_section(full_text, "Unnecessary Expense Categories"),
57
+ extract_section(full_text, "Estimated Savings %"),
58
+ extract_section(full_text, "Spending Trends"),
59
+ extract_section(full_text, "Category-wise Expense Breakdown (Partial)"),
60
+ extract_section(full_text, "Cost Control Suggestions"),
61
+ )
62
+
63
+ except Exception as e:
64
+ return (f"❌ Gemini Error: {e}", "", "", "", "", "", "")
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()