Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,88 +1,111 @@
|
|
1 |
-
|
2 |
-
|
|
|
3 |
import gradio as gr
|
4 |
import pandas as pd
|
|
|
5 |
from datetime import datetime
|
6 |
-
import tempfile
|
7 |
-
|
8 |
-
# Load text generation model
|
9 |
-
generator = pipeline("text-generation", model="gpt-2")
|
10 |
|
11 |
-
# Initialize an empty DataFrame to store all records of invoices
|
12 |
ledger = pd.DataFrame(columns=["Head", "Company Name", "SP/Station", "Invoice No", "Date", "Amount"])
|
13 |
|
14 |
-
# Function to add a new invoice
|
15 |
def add_invoice(head, company_name, sp_station, invoice_no, date, amount):
|
16 |
global ledger
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
def search_by_head(head):
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
-
# Function to generate a PDF report
|
37 |
def generate_ledger_pdf(head):
|
38 |
global ledger
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
# Title and generated content
|
50 |
-
pdf.set_font("Arial", 'B', 16)
|
51 |
-
pdf.cell(0, 10, f"Ledger Report for {head}", ln=True, align="C")
|
52 |
-
pdf.set_font("Arial", 'I', 12)
|
53 |
-
pdf.cell(0, 10, f"Generated Content: {generated_text}", ln=True, align="C")
|
54 |
-
pdf.ln(10)
|
55 |
-
|
56 |
-
# Table headers
|
57 |
-
pdf.set_font("Arial", 'B', 12)
|
58 |
-
pdf.cell(50, 10, "Company Name", border=1, align="C")
|
59 |
-
pdf.cell(40, 10, "SP/Station", border=1, align="C")
|
60 |
-
pdf.cell(30, 10, "Invoice No", border=1, align="C")
|
61 |
-
pdf.cell(30, 10, "Date", border=1, align="C")
|
62 |
-
pdf.cell(40, 10, "Amount", border=1, ln=True, align="C")
|
63 |
|
64 |
-
filtered_ledger = ledger[ledger["Head"] == head]
|
65 |
-
if not filtered_ledger.empty:
|
66 |
-
pdf.set_font("Arial", size=12)
|
67 |
for _, row in filtered_ledger.iterrows():
|
68 |
-
pdf.cell(
|
69 |
-
pdf.cell(40, 10, str(row['SP/Station']), border=1, align="C")
|
70 |
-
pdf.cell(30, 10, str(row['Invoice No']), border=1, align="C")
|
71 |
-
pdf.cell(30, 10, str(row['Date']), border=1, align="C")
|
72 |
-
pdf.cell(
|
73 |
|
74 |
# Total amount for the head
|
75 |
total_amount = filtered_ledger["Amount"].sum()
|
76 |
-
pdf.
|
77 |
-
pdf.
|
78 |
-
|
79 |
-
|
|
|
80 |
|
81 |
-
|
82 |
-
|
|
|
|
|
|
|
|
|
83 |
|
84 |
# Gradio interface
|
85 |
with gr.Blocks() as app:
|
|
|
86 |
with gr.Row():
|
87 |
head = gr.Textbox(label="Head")
|
88 |
company_name = gr.Textbox(label="Company Name")
|
@@ -91,19 +114,36 @@ with gr.Blocks() as app:
|
|
91 |
date = gr.Textbox(label="Date")
|
92 |
amount = gr.Number(label="Amount")
|
93 |
add_button = gr.Button("Add Invoice")
|
94 |
-
|
|
|
95 |
with gr.Row():
|
96 |
search_head = gr.Textbox(label="Search by Head")
|
97 |
search_button = gr.Button("Search Ledger")
|
98 |
ledger_records = gr.DataFrame(label="Ledger Records")
|
99 |
total_display = gr.Textbox(label="Total Amount", interactive=False)
|
100 |
-
|
|
|
101 |
with gr.Row():
|
102 |
generate_pdf_button = gr.Button("Generate PDF")
|
103 |
pdf_output = gr.File(label="Download PDF")
|
104 |
|
|
|
105 |
add_button.click(add_invoice, [head, company_name, sp_station, invoice_no, date, amount], [ledger_records, total_display])
|
106 |
-
|
107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
|
|
|
109 |
app.launch(share=True)
|
|
|
1 |
+
# Install necessary libraries in Hugging Face Space
|
2 |
+
!pip install gradio fpdf pandas
|
3 |
+
|
4 |
import gradio as gr
|
5 |
import pandas as pd
|
6 |
+
from fpdf import FPDF
|
7 |
from datetime import datetime
|
8 |
+
import tempfile # Use for temporary file handling
|
9 |
+
import os
|
|
|
|
|
10 |
|
11 |
+
# Initialize an empty DataFrame to store all records of invoices (acting as our ledger)
|
12 |
ledger = pd.DataFrame(columns=["Head", "Company Name", "SP/Station", "Invoice No", "Date", "Amount"])
|
13 |
|
14 |
+
# Function to add a new invoice under a specific head
|
15 |
def add_invoice(head, company_name, sp_station, invoice_no, date, amount):
|
16 |
global ledger
|
17 |
+
try:
|
18 |
+
# Add the new record to the ledger
|
19 |
+
new_row = {
|
20 |
+
"Head": head,
|
21 |
+
"Company Name": company_name,
|
22 |
+
"SP/Station": sp_station,
|
23 |
+
"Invoice No": invoice_no,
|
24 |
+
"Date": date,
|
25 |
+
"Amount": float(amount)
|
26 |
+
}
|
27 |
+
ledger = pd.concat([ledger, pd.DataFrame([new_row])], ignore_index=True)
|
28 |
+
|
29 |
+
# Filter the ledger for the selected head
|
30 |
+
filtered_ledger = ledger[ledger["Head"] == head]
|
31 |
+
|
32 |
+
# Calculate the total for the selected head
|
33 |
+
total_amount = filtered_ledger["Amount"].sum()
|
34 |
+
return filtered_ledger, f"Total for {head}: {total_amount}"
|
35 |
+
|
36 |
+
except Exception as e:
|
37 |
+
print(f"Error during adding invoice: {e}")
|
38 |
+
return pd.DataFrame(), "Error during adding invoice"
|
39 |
+
|
40 |
+
# Function to search and display all previous records by head
|
41 |
def search_by_head(head):
|
42 |
+
global ledger
|
43 |
+
try:
|
44 |
+
# Filter the ledger for the selected head
|
45 |
+
filtered_ledger = ledger[ledger["Head"] == head]
|
46 |
+
|
47 |
+
# Calculate the total for the selected head
|
48 |
+
total_amount = filtered_ledger["Amount"].sum()
|
49 |
+
|
50 |
+
return filtered_ledger, f"Total for {head}: {total_amount}"
|
51 |
+
|
52 |
+
except Exception as e:
|
53 |
+
print(f"Error during search by head: {e}")
|
54 |
+
return pd.DataFrame(), "Error during search by head"
|
55 |
|
56 |
+
# Function to generate a PDF report for a specific head
|
57 |
def generate_ledger_pdf(head):
|
58 |
global ledger
|
59 |
+
try:
|
60 |
+
# Use a temporary directory to save the file
|
61 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp_file:
|
62 |
+
pdf_file_path = tmp_file.name # Get the temporary file path
|
63 |
+
|
64 |
+
# Create a PDF
|
65 |
+
pdf = FPDF()
|
66 |
+
pdf.add_page()
|
67 |
+
pdf.set_font("Arial", 'B', 16)
|
68 |
+
pdf.cell(200, 10, txt=f"Ledger Report for {head}", ln=True, align="C")
|
69 |
+
pdf.set_font("Arial", size=12)
|
70 |
+
current_date = datetime.now().strftime("%d/%m/%Y")
|
71 |
+
pdf.cell(200, 10, txt=f"Report Date: {current_date}", ln=True, align="C")
|
72 |
+
pdf.ln(10)
|
73 |
|
74 |
+
# Table headers
|
75 |
+
pdf.cell(40, 10, txt="Company Name", border=1, align="C")
|
76 |
+
pdf.cell(40, 10, txt="SP/Station", border=1, align="C")
|
77 |
+
pdf.cell(30, 10, txt="Invoice No", border=1, align="C")
|
78 |
+
pdf.cell(30, 10, txt="Date", border=1, align="C")
|
79 |
+
pdf.cell(50, 10, txt="Amount", border=1, ln=True, align="C")
|
80 |
+
|
81 |
+
# Filter records by head
|
82 |
+
filtered_ledger = ledger[ledger["Head"] == head]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
|
|
|
|
|
|
84 |
for _, row in filtered_ledger.iterrows():
|
85 |
+
pdf.cell(40, 10, txt=str(row['Company Name']), border=1, align="C")
|
86 |
+
pdf.cell(40, 10, txt=str(row['SP/Station']), border=1, align="C")
|
87 |
+
pdf.cell(30, 10, txt=str(row['Invoice No']), border=1, align="C")
|
88 |
+
pdf.cell(30, 10, txt=str(row['Date']), border=1, align="C")
|
89 |
+
pdf.cell(50, 10, txt=str(row['Amount']), border=1, ln=True, align="C")
|
90 |
|
91 |
# Total amount for the head
|
92 |
total_amount = filtered_ledger["Amount"].sum()
|
93 |
+
pdf.cell(140, 10, txt="Total Amount", border=1, align="R")
|
94 |
+
pdf.cell(50, 10, txt=str(total_amount), border=1, ln=True, align="C")
|
95 |
+
|
96 |
+
# Save PDF file
|
97 |
+
pdf.output(pdf_file_path)
|
98 |
|
99 |
+
# Return the file path for download
|
100 |
+
return pdf_file_path
|
101 |
+
|
102 |
+
except Exception as e:
|
103 |
+
print(f"Error during PDF generation: {e}")
|
104 |
+
return None
|
105 |
|
106 |
# Gradio interface
|
107 |
with gr.Blocks() as app:
|
108 |
+
# Section for adding new invoices with Head and Company Name fields
|
109 |
with gr.Row():
|
110 |
head = gr.Textbox(label="Head")
|
111 |
company_name = gr.Textbox(label="Company Name")
|
|
|
114 |
date = gr.Textbox(label="Date")
|
115 |
amount = gr.Number(label="Amount")
|
116 |
add_button = gr.Button("Add Invoice")
|
117 |
+
|
118 |
+
# Section for searching and displaying ledger by head
|
119 |
with gr.Row():
|
120 |
search_head = gr.Textbox(label="Search by Head")
|
121 |
search_button = gr.Button("Search Ledger")
|
122 |
ledger_records = gr.DataFrame(label="Ledger Records")
|
123 |
total_display = gr.Textbox(label="Total Amount", interactive=False)
|
124 |
+
|
125 |
+
# Section for generating and displaying PDF link
|
126 |
with gr.Row():
|
127 |
generate_pdf_button = gr.Button("Generate PDF")
|
128 |
pdf_output = gr.File(label="Download PDF")
|
129 |
|
130 |
+
# Define button functionalities
|
131 |
add_button.click(add_invoice, [head, company_name, sp_station, invoice_no, date, amount], [ledger_records, total_display])
|
132 |
+
|
133 |
+
def search_ledger(head):
|
134 |
+
filtered_ledger, total = search_by_head(head)
|
135 |
+
return filtered_ledger, total
|
136 |
+
|
137 |
+
search_button.click(search_ledger, [search_head], [ledger_records, total_display])
|
138 |
+
|
139 |
+
def generate_pdf(head):
|
140 |
+
pdf_path = generate_ledger_pdf(head)
|
141 |
+
if pdf_path:
|
142 |
+
return pdf_path
|
143 |
+
else:
|
144 |
+
return None
|
145 |
+
|
146 |
+
generate_pdf_button.click(generate_pdf, [search_head], [pdf_output])
|
147 |
|
148 |
+
# Launch the app
|
149 |
app.launch(share=True)
|