File size: 3,097 Bytes
ba7e159
 
 
 
 
ca27c25
ba7e159
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
ο»Ώimport gradio as gr
import PyPDF2
import google.generativeai as genai
import re

GEMINI_API_KEY = "AIzaSyDgb-PyRXrrwuBqnjc3BuXSjbrM7dPlNJY"  # Replace with your actual key
genai.configure(api_key=GEMINI_API_KEY)

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:
        return ""

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"

def analyze_financial_data(file):
    text = extract_text_from_pdf(file)

    if not text:
        return (
            "⚠️ Failed to extract text from the PDF. Ensure it’s not scanned.",
            "", "", "", "", "", ""
        )

    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()

        return (
            "βœ… Analysis Complete",
            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"),
        )

    except Exception as e:
        return (f"❌ Gemini Error: {e}", "", "", "", "", "", "")

gr.Interface(
    fn=analyze_financial_data,
    inputs=gr.File(label="πŸ“‚ Upload Paytm PDF", file_types=[".pdf"]),
    outputs=[
        gr.Textbox(label="βœ… Status", lines=2, interactive=False),
        gr.Textbox(label="πŸ’΅ Monthly Income & Expenses", lines=8, interactive=False),
        gr.Textbox(label="πŸ›’ Unnecessary Expense Categories", lines=8, interactive=False),
        gr.Textbox(label="πŸ’° Estimated Savings %", lines=4, interactive=False),
        gr.Textbox(label="πŸ“ˆ Spending Trends", lines=8, interactive=False),
        gr.Textbox(label="πŸ“Š Category-wise Breakdown", lines=10, interactive=False),
        gr.Textbox(label="🧠 Cost Control Suggestions", lines=8, interactive=False),
    ],
    title="πŸ’° AI-Powered Personal Finance Assistant",
    description="Upload your Paytm transaction PDF (text-based) and get structured financial insights using Gemini AI.",
).launch()