File size: 4,393 Bytes
5ee9992
 
201dcc1
 
cf9d036
5ee9992
 
 
 
201dcc1
5ee9992
25ef9d4
201dcc1
5ee9992
25ef9d4
 
5ee9992
 
 
 
 
 
 
 
 
 
 
 
 
 
25ef9d4
5ee9992
 
 
25ef9d4
5ee9992
25ef9d4
 
5ee9992
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10eb027
d6dc6f3
5ee9992
 
 
 
 
201dcc1
 
 
 
25ef9d4
cf9d036
25ef9d4
201dcc1
 
 
 
5ee9992
201dcc1
25ef9d4
 
 
 
5ee9992
201dcc1
 
a18fe7e
201dcc1
25ef9d4
5ee9992
 
201dcc1
 
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
from transformers import pipeline
from fpdf import FPDF
import gradio as gr
import pandas as pd
from datetime import datetime
import tempfile

# Load text generation model
generator = pipeline("text-generation", model="gpt-2")

# Initialize an empty DataFrame to store all records of invoices
ledger = pd.DataFrame(columns=["Head", "Company Name", "SP/Station", "Invoice No", "Date", "Amount"])

# Function to add a new invoice
def add_invoice(head, company_name, sp_station, invoice_no, date, amount):
    global ledger
    new_row = {
        "Head": head,
        "Company Name": company_name,
        "SP/Station": sp_station,
        "Invoice No": invoice_no,
        "Date": date,
        "Amount": float(amount)
    }
    ledger = pd.concat([ledger, pd.DataFrame([new_row])], ignore_index=True)
    filtered_ledger = ledger[ledger["Head"] == head]
    total_amount = filtered_ledger["Amount"].sum()
    return filtered_ledger, f"Total for {head}: {total_amount:.2f}"

# Function to search and display all records by head
def search_by_head(head):
    filtered_ledger = ledger[ledger["Head"] == head]
    total_amount = filtered_ledger["Amount"].sum()
    return filtered_ledger, f"Total for {head}: {total_amount:.2f}"

# Function to generate a PDF report
def generate_ledger_pdf(head):
    global ledger

    # Generate some content for the PDF using the Hugging Face model
    generated_text = generator(f"Generate a report summary for {head}.", max_length=50)[0]['generated_text']

    with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
        pdf_file_path = tmp_file.name

        pdf = FPDF()
        pdf.add_page()
        
        # Title and generated content
        pdf.set_font("Arial", 'B', 16)
        pdf.cell(0, 10, f"Ledger Report for {head}", ln=True, align="C")
        pdf.set_font("Arial", 'I', 12)
        pdf.cell(0, 10, f"Generated Content: {generated_text}", ln=True, align="C")
        pdf.ln(10)

        # Table headers
        pdf.set_font("Arial", 'B', 12)
        pdf.cell(50, 10, "Company Name", border=1, align="C")
        pdf.cell(40, 10, "SP/Station", border=1, align="C")
        pdf.cell(30, 10, "Invoice No", border=1, align="C")
        pdf.cell(30, 10, "Date", border=1, align="C")
        pdf.cell(40, 10, "Amount", border=1, ln=True, align="C")

        filtered_ledger = ledger[ledger["Head"] == head]
        if not filtered_ledger.empty:
            pdf.set_font("Arial", size=12)
            for _, row in filtered_ledger.iterrows():
                pdf.cell(50, 10, str(row['Company Name']), border=1, align="C")
                pdf.cell(40, 10, str(row['SP/Station']), border=1, align="C")
                pdf.cell(30, 10, str(row['Invoice No']), border=1, align="C")
                pdf.cell(30, 10, str(row['Date']), border=1, align="C")
                pdf.cell(40, 10, f"{row['Amount']:.2f}", border=1, ln=True, align="C")

            # Total amount for the head
            total_amount = filtered_ledger["Amount"].sum()
            pdf.ln(10)
            pdf.set_font("Arial", 'B', 12)
            pdf.cell(150, 10, "Total Amount", border=1, align="R")
            pdf.cell(40, 10, f"{total_amount:.2f}", border=1, ln=True, align="C")

        pdf.output(pdf_file_path)
        return pdf_file_path

# Gradio interface
with gr.Blocks() as app:
    with gr.Row():
        head = gr.Textbox(label="Head")
        company_name = gr.Textbox(label="Company Name")
        sp_station = gr.Textbox(label="SP/Station")
        invoice_no = gr.Textbox(label="Invoice No")
        date = gr.Textbox(label="Date")
        amount = gr.Number(label="Amount")
        add_button = gr.Button("Add Invoice")

    with gr.Row():
        search_head = gr.Textbox(label="Search by Head")
        search_button = gr.Button("Search Ledger")
        ledger_records = gr.DataFrame(label="Ledger Records")
        total_display = gr.Textbox(label="Total Amount", interactive=False)

    with gr.Row():
        generate_pdf_button = gr.Button("Generate PDF")
        pdf_output = gr.File(label="Download PDF")

    add_button.click(add_invoice, [head, company_name, sp_station, invoice_no, date, amount], [ledger_records, total_display])
    search_button.click(search_by_head, [search_head], [ledger_records, total_display])
    generate_pdf_button.click(generate_ledger_pdf, [search_head], [pdf_output])

app.launch(share=True)