Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
|
|
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
|
|
|
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 (
|
|
|
|
|
|
|
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 |
-
#
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
report_bytes.name = "financial_analysis.txt"
|
56 |
|
57 |
return (
|
58 |
-
"β
Analysis Complete",
|
59 |
-
text[:2000] + "..." if len(text) > 2000 else text,
|
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 |
-
|
67 |
)
|
68 |
|
69 |
except Exception as e:
|
70 |
return (f"β Gemini Error: {e}", "", "", "", "", "", "", "", None)
|
71 |
|
72 |
-
# π¨ Gradio
|
73 |
-
with gr.Blocks(title="AI
|
74 |
-
gr.Markdown("
|
75 |
-
|
|
|
|
|
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
|
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 (
|
87 |
-
|
88 |
|
89 |
with gr.Row():
|
90 |
-
|
91 |
-
|
92 |
|
93 |
with gr.Row():
|
94 |
-
|
95 |
-
|
96 |
|
97 |
with gr.Row():
|
98 |
-
|
99 |
-
|
100 |
|
101 |
analyze_btn.click(
|
102 |
fn=analyze_financial_data,
|
103 |
inputs=pdf_input,
|
104 |
outputs=[
|
105 |
-
status,
|
106 |
-
|
107 |
-
|
|
|
|
|
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)
|