Spaces:
Sleeping
Sleeping
import gradio as gr | |
import PyPDF2 | |
import google.generativeai as genai | |
import re | |
import tempfile | |
import os | |
# π Gemini API Key | |
GEMINI_API_KEY = "AIzaSyDnx_qUjGTFG1pv1otPUhNt_bGGv14aMDI" | |
genai.configure(api_key=GEMINI_API_KEY) | |
# π Extract text from PDF | |
def extract_text_from_pdf(file): | |
try: | |
reader = PyPDF2.PdfReader(file) | |
text = "" | |
for page in reader.pages: | |
content = page.extract_text() | |
if content: | |
text += content + "\n" | |
return text.strip() | |
except Exception as e: | |
print("PDF Extraction Error:", e) | |
return "" | |
# βοΈ Extract sections from full text using regex | |
def extract_section(full_text, label): | |
pattern = rf"\*\*\- {re.escape(label)}:\*\*\s*(.*?)(?=\n\*\*|\Z)" | |
match = re.search(pattern, full_text, re.DOTALL) | |
return match.group(1).strip() if match else "β Not found" | |
# π§ Main function to analyze financial data | |
def analyze_financial_data(file): | |
text = extract_text_from_pdf(file) | |
if not text: | |
return ( | |
"β οΈ Failed to extract text. Ensure itβs a text-based PDF.", | |
"", "", "", "", "", "", "", None | |
) | |
prompt = f""" | |
Analyze the following Paytm transaction history and generate financial insights in the following structure: | |
**Financial Insights** | |
**- Monthly Income & Expenses:** [data] | |
**- Unnecessary Expense Categories:** [data] | |
**- Estimated Savings %:** [data] | |
**- Spending Trends:** [data] | |
**- Category-wise Expense Breakdown (Partial):** [data] | |
**- Cost Control Suggestions:** [data] | |
Transaction History: | |
{text} | |
""" | |
try: | |
model = genai.GenerativeModel("gemini-1.5-flash") | |
response = model.generate_content(prompt) | |
full_text = response.text.strip() | |
# Save report to temporary .txt file | |
with tempfile.NamedTemporaryFile(delete=False, suffix=".txt", mode="w", encoding="utf-8") as tmp: | |
tmp.write(full_text) | |
report_path = tmp.name | |
return ( | |
"β Analysis Complete!", | |
text[:2000] + "..." if len(text) > 2000 else text, | |
extract_section(full_text, "Monthly Income & Expenses"), | |
extract_section(full_text, "Unnecessary Expense Categories"), | |
extract_section(full_text, "Estimated Savings %"), | |
extract_section(full_text, "Spending Trends"), | |
extract_section(full_text, "Category-wise Expense Breakdown (Partial)"), | |
extract_section(full_text, "Cost Control Suggestions"), | |
report_path | |
) | |
except Exception as e: | |
return (f"β Gemini Error: {e}", "", "", "", "", "", "", "", None) | |
# π¨ Gradio UI | |
with gr.Blocks(title="AI Financial Analyzer", theme=gr.themes.Soft()) as demo: | |
gr.Markdown(""" | |
# πΈ AI-Powered Personal Finance Analyzer | |
Upload your **UPI Expenses PDF** and get structured financial insights using **Gemini AI**. | |
""") | |
with gr.Row(): | |
with gr.Column(scale=1): | |
pdf_input = gr.File(label="π Upload PDF", file_types=[".pdf"]) | |
analyze_btn = gr.Button("π Analyze") | |
with gr.Column(scale=1): | |
status = gr.Textbox(label="β Status", interactive=False) | |
download_btn = gr.File(label="π₯ Download AI Report", interactive=False) | |
with gr.Accordion("π View Extracted PDF Text (Optional)", open=False): | |
extracted_text = gr.Textbox(label="π Extracted Text", lines=10, interactive=False) | |
with gr.Row(): | |
income_expense = gr.Textbox(label="π΅ Monthly Income & Expenses", lines=4, interactive=False) | |
unnecessary = gr.Textbox(label="π Unnecessary Expenses", lines=4, interactive=False) | |
with gr.Row(): | |
savings = gr.Textbox(label="π° Estimated Savings %", lines=2, interactive=False) | |
trends = gr.Textbox(label="π Spending Trends", lines=4, interactive=False) | |
with gr.Row(): | |
category_breakdown = gr.Textbox(label="π Category-wise Breakdown", lines=6, interactive=False) | |
suggestions = gr.Textbox(label="π§ Cost Control Suggestions", lines=6, interactive=False) | |
analyze_btn.click( | |
fn=analyze_financial_data, | |
inputs=pdf_input, | |
outputs=[ | |
status, extracted_text, | |
income_expense, unnecessary, | |
savings, trends, | |
category_breakdown, suggestions, | |
download_btn | |
] | |
) | |
demo.launch(share=True) |