rlgondong commited on
Commit
c46ae56
Β·
verified Β·
1 Parent(s): 1f8d0f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -4
app.py CHANGED
@@ -4,6 +4,8 @@ from torchvision import transforms
4
  from PIL import Image
5
  import joblib
6
  import base64
 
 
7
 
8
  # Load model and metadata
9
  model = torch.jit.load("resnet_grocery_model_scripted.pt", map_location="cpu")
@@ -36,6 +38,40 @@ def classify_and_track(files, budget):
36
  total_cost = 0
37
  receipt_lines = []
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  receipt_lines.append("========================================")
40
  receipt_lines.append(" 🧾 OFFICIAL RECEIPT")
41
  receipt_lines.append(" πŸ›’ SOBRANG TINDANG INA!")
@@ -64,6 +100,9 @@ def classify_and_track(files, budget):
64
  if len(name) > 26:
65
  name = name[:23] + "..."
66
  receipt_lines.append(f" {name:<28} β‚±{price:>6.2f}")
 
 
 
67
 
68
  remaining = budget - total_cost
69
  receipt_lines.append("----------------------------------------")
@@ -72,15 +111,25 @@ def classify_and_track(files, budget):
72
  receipt_lines.append(f" REMAINING{'':<20}β‚±{remaining:>7.2f}")
73
  receipt_lines.append("========================================")
74
 
 
 
 
 
 
 
 
 
75
  percent = min(100, int((total_cost / budget) * 100)) if budget else 0
76
  status = f"**Total: β‚±{total_cost:.2f} / β‚±{budget:.2f}**"
77
 
78
  if total_cost <= budget:
79
  receipt_lines.append(" βœ… You're within budget! πŸŽ‰")
 
80
  status_msg = f"βœ… **Within Budget!** πŸŽ‰\n{status}"
81
  sound_path = "success.mp3"
82
  else:
83
  receipt_lines.append(" ❌ Over budget! Remove some snacks!")
 
84
  status_msg = f"🚨 **Over Budget!** πŸ’Έ\n{status}"
85
  sound_path = "fail.mp3"
86
 
@@ -89,11 +138,26 @@ def classify_and_track(files, budget):
89
  receipt_lines.append(" Come back for smarter buys! 🧾")
90
  receipt_lines.append("========================================")
91
 
 
 
 
 
 
 
 
 
92
  receipt_text = "```\n" + "\n".join(receipt_lines) + "\n```"
93
- return results, status_msg, receipt_text, percent, sound_path
 
 
 
 
 
 
 
94
 
95
  def reset_ui():
96
- return [], "", "", 0, None
97
 
98
  # Load and encode local background image
99
  with open("background.jpg", "rb") as img_file:
@@ -172,16 +236,17 @@ with gr.Blocks(title="Grocery Classifier") as app:
172
  progress_bar = gr.Slider(label="Budget Used (%)", minimum=0, maximum=100, interactive=False)
173
  receipt_display = gr.Markdown(label="🧾 Grocery Receipt")
174
  audio_output = gr.Audio(interactive=False, autoplay=True)
 
175
 
176
  classify_btn.click(
177
  fn=classify_and_track,
178
  inputs=[file_input, budget_input],
179
- outputs=[gallery, status_display, receipt_display, progress_bar, audio_output]
180
  )
181
 
182
  reset_btn.click(
183
  fn=reset_ui,
184
- outputs=[gallery, status_display, receipt_display, progress_bar, audio_output]
185
  )
186
 
187
  app.launch()
 
4
  from PIL import Image
5
  import joblib
6
  import base64
7
+ import io
8
+ from contextlib import redirect_stdout
9
 
10
  # Load model and metadata
11
  model = torch.jit.load("resnet_grocery_model_scripted.pt", map_location="cpu")
 
38
  total_cost = 0
39
  receipt_lines = []
40
 
41
+ # Initialize LaTeX document for PDF
42
+ latex_lines = [
43
+ r"\documentclass[a4paper,12pt]{article}",
44
+ r"\usepackage[utf8]{inputenc}",
45
+ r"\usepackage[T1]{fontenc}",
46
+ r"\usepackage{lmodern}",
47
+ r"\usepackage{geometry}",
48
+ r"\geometry{margin=2cm}",
49
+ r"\usepackage{longtable}",
50
+ r"\usepackage{array}",
51
+ r"\usepackage{booktabs}",
52
+ r"\usepackage{fancyhdr}",
53
+ r"\pagestyle{fancy}",
54
+ r"\fancyhf{}",
55
+ r"\fancyhead[C]{SOBRANG TINDANG INA!}",
56
+ r"\fancyfoot[C]{\thepage}",
57
+ r"\usepackage{lastpage}",
58
+ r"\usepackage{noto}", # Using Noto font for compatibility
59
+ r"\begin{document}",
60
+ r"\begin{center}",
61
+ r"{\Large \textbf{OFFICIAL RECEIPT}} \\[0.2cm]",
62
+ r"{\normalsize SOBRANG TINDANG INA!} \\",
63
+ r"{\normalsize Online Smart Checkout} \\",
64
+ r"\vspace{0.5cm}",
65
+ r"\hrulefill \\",
66
+ r"\end{center}",
67
+ r"\vspace{0.5cm}",
68
+ r"\begin{longtable}{p{10cm} r}",
69
+ r"\hline",
70
+ r"\textbf{Item} & \textbf{Price (\textsf{β‚±})} \\",
71
+ r"\hline",
72
+ r"\endhead",
73
+ ]
74
+
75
  receipt_lines.append("========================================")
76
  receipt_lines.append(" 🧾 OFFICIAL RECEIPT")
77
  receipt_lines.append(" πŸ›’ SOBRANG TINDANG INA!")
 
100
  if len(name) > 26:
101
  name = name[:23] + "..."
102
  receipt_lines.append(f" {name:<28} β‚±{price:>6.2f}")
103
+ # Sanitize name for LaTeX
104
+ latex_name = name.replace("_", r"\_").replace("&", r"\&").replace("%", r"\%")
105
+ latex_lines.append(f"{latex_name} & {price:.2f} \\\\")
106
 
107
  remaining = budget - total_cost
108
  receipt_lines.append("----------------------------------------")
 
111
  receipt_lines.append(f" REMAINING{'':<20}β‚±{remaining:>7.2f}")
112
  receipt_lines.append("========================================")
113
 
114
+ latex_lines.append(r"\hline")
115
+ latex_lines.append(f"\\textbf{{Total}} & {total_cost:.2f} \\\\")
116
+ latex_lines.append(f"\\textbf{{Budget}} & {budget:.2f} \\\\")
117
+ latex_lines.append(f"\\textbf{{Remaining}} & {remaining:.2f} \\\\")
118
+ latex_lines.append(r"\hline")
119
+ latex_lines.append(r"\end{longtable}")
120
+ latex_lines.append(r"\vspace{0.5cm}")
121
+
122
  percent = min(100, int((total_cost / budget) * 100)) if budget else 0
123
  status = f"**Total: β‚±{total_cost:.2f} / β‚±{budget:.2f}**"
124
 
125
  if total_cost <= budget:
126
  receipt_lines.append(" βœ… You're within budget! πŸŽ‰")
127
+ latex_lines.append(r"{\color{green} \textbf{You're within budget!}} \\")
128
  status_msg = f"βœ… **Within Budget!** πŸŽ‰\n{status}"
129
  sound_path = "success.mp3"
130
  else:
131
  receipt_lines.append(" ❌ Over budget! Remove some snacks!")
132
+ latex_lines.append(r"{\color{red} \textbf{Over budget! Remove some snacks!}} \\")
133
  status_msg = f"🚨 **Over Budget!** πŸ’Έ\n{status}"
134
  sound_path = "fail.mp3"
135
 
 
138
  receipt_lines.append(" Come back for smarter buys! 🧾")
139
  receipt_lines.append("========================================")
140
 
141
+ latex_lines.append(r"\vspace{0.5cm}")
142
+ latex_lines.append(r"\begin{center}")
143
+ latex_lines.append(r"THANK YOU FOR SHOPPING WITH US! \\")
144
+ latex_lines.append(r"Come back for smarter buys! \\")
145
+ latex_lines.append(r"\hrulefill")
146
+ latex_lines.append(r"\end{center}")
147
+ latex_lines.append(r"\end{document}")
148
+
149
  receipt_text = "```\n" + "\n".join(receipt_lines) + "\n```"
150
+ latex_content = "\n".join(latex_lines)
151
+
152
+ # Create a file-like object for the LaTeX content
153
+ latex_file = io.StringIO()
154
+ with redirect_stdout(latex_file):
155
+ print(latex_content)
156
+
157
+ return results, status_msg, receipt_text, percent, sound_path, latex_file
158
 
159
  def reset_ui():
160
+ return [], "", "", 0, None, None
161
 
162
  # Load and encode local background image
163
  with open("background.jpg", "rb") as img_file:
 
236
  progress_bar = gr.Slider(label="Budget Used (%)", minimum=0, maximum=100, interactive=False)
237
  receipt_display = gr.Markdown(label="🧾 Grocery Receipt")
238
  audio_output = gr.Audio(interactive=False, autoplay=True)
239
+ pdf_download = gr.File(label="πŸ“„ Download Receipt as PDF", interactive=False)
240
 
241
  classify_btn.click(
242
  fn=classify_and_track,
243
  inputs=[file_input, budget_input],
244
+ outputs=[gallery, status_display, receipt_display, progress_bar, audio_output, pdf_download]
245
  )
246
 
247
  reset_btn.click(
248
  fn=reset_ui,
249
+ outputs=[gallery, status_display, receipt_display, progress_bar, audio_output, pdf_download]
250
  )
251
 
252
  app.launch()