varun321's picture
Switch to saving images as files instead of base64, optimize LLM parameters
0004b69
import gradio as gr
from utils.invoice_parser import parse_invoice
from utils.data_processor import process_data
from utils.llm_analyzer import LLMAnalyzer
from utils.report_generator import generate_report
import os
# Initialize the LLM analyzer
analyzer = LLMAnalyzer()
def process_invoice(pdf_file):
try:
if not pdf_file:
return "No file uploaded.", None, None
transactions = parse_invoice(pdf_file)
if not transactions:
return "No transactions found in the invoice.", None, None
df = process_data(transactions)
if df.empty:
return "No valid transactions after processing.", None, None
df_categorized = analyzer.categorize_transactions(df)
spending_analysis = analyzer.analyze_spending_patterns(df_categorized)
recommendations = analyzer.generate_budget_recommendations(spending_analysis)
report = generate_report(df_categorized, spending_analysis, recommendations)
# Get paths to generated images
category_image_path = os.path.join("images", "category_spending.png") if os.path.exists(os.path.join("images", "category_spending.png")) else None
monthly_image_path = os.path.join("images", "monthly_spending.png") if os.path.exists(os.path.join("images", "monthly_spending.png")) else None
return report, category_image_path, monthly_image_path
except Exception as e:
return f"An error occurred: {str(e)}", None, None
interface = gr.Interface(
fn=process_invoice,
inputs=gr.File(label="Upload Invoice (PDF)", type="filepath"),
outputs=[
gr.Markdown(label="Report"),
gr.Image(label="Category-wise Spending", type="filepath"),
gr.Image(label="Monthly Spending Trend", type="filepath")
],
title="Invoice Reader & Budget Categorizer",
description="Upload your invoice PDF to categorize transactions, analyze spending patterns, and get budget optimization recommendations."
)
if __name__ == "__main__":
interface.launch() # Removed share=True for Spaces compatibility