Sanjayraju30 commited on
Commit
2e709f8
Β·
verified Β·
1 Parent(s): d754b04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -7
app.py CHANGED
@@ -6,19 +6,23 @@ from ocr_engine import extract_weight
6
 
7
  def process_image(image):
8
  if image is None:
9
- return "No image provided", "", None
10
  try:
11
  weight = extract_weight(image)
12
  ist = pytz.timezone('Asia/Kolkata')
13
  timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
14
- return weight, timestamp, image
 
 
 
 
15
  except Exception as e:
16
- return f"Error: {str(e)}", "", None
17
 
18
  with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
19
  gr.Markdown("""
20
  <h1 style='text-align: center; color: #2e7d32;'>πŸ“· Auto Weight Logger</h1>
21
- <p style='text-align: center;'>Detect weights (kg or grams) from digital balance display using AI OCR.</p>
22
  <hr style='border: 1px solid #ddd;'/>
23
  """)
24
 
@@ -32,12 +36,14 @@ with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: whi
32
  time_out = gr.Textbox(label="πŸ•’ Captured At (IST)", placeholder="e.g., 2025-06-30 14:32:10")
33
 
34
  snapshot = gr.Image(label="πŸ“Έ Snapshot Preview")
 
35
 
36
- detect_btn.click(fn=process_image, inputs=image_input, outputs=[weight_out, time_out, snapshot])
 
 
37
 
38
  gr.Markdown("""
39
- <br><p style='text-align: center; color: gray;'>Developed by Shalu β€’ Powered by Hugging Face OCR πŸš€</p>
40
  """)
41
 
42
- # 🚨 REQUIRED for Hugging Face to recognize the app
43
  demo.launch()
 
6
 
7
  def process_image(image):
8
  if image is None:
9
+ return "No image provided", "", None, gr.update(visible=True)
10
  try:
11
  weight = extract_weight(image)
12
  ist = pytz.timezone('Asia/Kolkata')
13
  timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
14
+
15
+ if not weight:
16
+ return "❌ Unable to detect. Try again with clearer image.", "", image, gr.update(visible=True)
17
+
18
+ return weight, timestamp, image, gr.update(visible=False)
19
  except Exception as e:
20
+ return f"Error: {str(e)}", "", None, gr.update(visible=True)
21
 
22
  with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
23
  gr.Markdown("""
24
  <h1 style='text-align: center; color: #2e7d32;'>πŸ“· Auto Weight Logger</h1>
25
+ <p style='text-align: center;'>Upload or capture a digital weight image. Detects weight with AI OCR.</p>
26
  <hr style='border: 1px solid #ddd;'/>
27
  """)
28
 
 
36
  time_out = gr.Textbox(label="πŸ•’ Captured At (IST)", placeholder="e.g., 2025-06-30 14:32:10")
37
 
38
  snapshot = gr.Image(label="πŸ“Έ Snapshot Preview")
39
+ retake_btn = gr.Button("πŸ” Retake / Try Again", visible=False)
40
 
41
+ detect_btn.click(fn=process_image, inputs=image_input, outputs=[weight_out, time_out, snapshot, retake_btn])
42
+ retake_btn.click(fn=lambda: ("", "", None, gr.update(visible=False)),
43
+ inputs=[], outputs=[weight_out, time_out, snapshot, retake_btn])
44
 
45
  gr.Markdown("""
46
+ <p style='text-align: center; color: gray;'>Tip: Ensure image is glare-free and focused. <br>Developed by Shalu β€’ Hugging Face OCR ⚑</p>
47
  """)
48
 
 
49
  demo.launch()