File size: 2,091 Bytes
1d54def
 
 
 
 
0004b69
1d54def
 
 
0004b69
1d54def
 
 
0004b69
1d54def
0004b69
1d54def
0004b69
1d54def
 
 
0004b69
1d54def
 
 
 
 
 
0004b69
 
 
 
 
1d54def
 
0004b69
1d54def
 
 
0004b69
 
 
 
 
 
1d54def
 
 
 
 
0004b69
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
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