Spaces:
Sleeping
Sleeping
File size: 4,486 Bytes
dab9ea3 bea8665 4a5fcca dab9ea3 bea8665 4a5fcca 9813569 bea8665 4a5fcca bea8665 4a5fcca bea8665 9813569 4a5fcca bea8665 4a5fcca bea8665 4a5fcca 9813569 bea8665 9813569 bea8665 9813569 4a5fcca 9813569 bea8665 4a5fcca bea8665 4a5fcca bea8665 9813569 bea8665 4a5fcca 96a893c 4a5fcca bea8665 4a5fcca bea8665 4a5fcca bea8665 4a5fcca bea8665 4a5fcca bea8665 4a5fcca bea8665 4a5fcca bea8665 4a5fcca bea8665 4a5fcca bea8665 4a5fcca bea8665 4a5fcca |
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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
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) |