atifsial123 commited on
Commit
5ee9992
·
verified ·
1 Parent(s): 40d3d4b

Update app.py

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