Sanjayraju30 commited on
Commit
abbbfd5
Β·
verified Β·
1 Parent(s): fd59642

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -7
app.py CHANGED
@@ -2,7 +2,7 @@ import gradio as gr
2
  from PIL import Image
3
  from datetime import datetime
4
  import pytz
5
- from ocr_engine import extract_weight
6
  from simple_salesforce import Salesforce
7
  import base64
8
  import re
@@ -27,26 +27,28 @@ def process_image(image):
27
  if image is None:
28
  return "❌ No image provided", "", None, gr.update(visible=True)
29
  try:
30
- weight = extract_weight(image)
 
 
31
  print("🧠 Final OCR Result:", weight)
 
32
 
33
  ist = pytz.timezone('Asia/Kolkata')
34
  timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
35
 
36
- if not weight or weight.startswith("Error"):
37
  return f"❌ OCR Error: {weight}", "", image, gr.update(visible=True)
38
 
39
  match = re.search(r'(\d{1,3}\.\d{1,3})\s*(kg|g)?', weight)
40
  if match:
41
  numeric_value = float(match.group(1))
42
- unit = match.group(2) if match.group(2) else "g"
43
  else:
44
- # fallback try to fix raw number like 53255 β†’ 52.255
45
  cleaned = re.sub(r"[^\d]", "", weight)
46
  decimal_fixed = restore_decimal(cleaned)
47
  try:
48
  numeric_value = float(decimal_fixed)
49
- unit = "g"
50
  except:
51
  return f"❌ Could not extract number | OCR: {weight}", "", image, gr.update(visible=True)
52
 
@@ -85,7 +87,7 @@ def process_image(image):
85
  except Exception as e:
86
  return f"Error: {str(e)}", "", None, gr.update(visible=True)
87
 
88
- with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
89
  gr.Markdown("""
90
  <h1 style='text-align: center; color: #2e7d32;'>πŸ“· Auto Weight Logger</h1>
91
  <p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR and logs it into Salesforce.</p>
 
2
  from PIL import Image
3
  from datetime import datetime
4
  import pytz
5
+ from ocr_engine import extract_weight, extract_unit_from_text
6
  from simple_salesforce import Salesforce
7
  import base64
8
  import re
 
27
  if image is None:
28
  return "❌ No image provided", "", None, gr.update(visible=True)
29
  try:
30
+ result = extract_weight(image)
31
+ weight, raw_text = result if isinstance(result, tuple) else (result, "")
32
+
33
  print("🧠 Final OCR Result:", weight)
34
+ print("πŸ”€ OCR Raw Text:", raw_text)
35
 
36
  ist = pytz.timezone('Asia/Kolkata')
37
  timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
38
 
39
+ if not weight or (isinstance(weight, str) and weight.startswith("Error")):
40
  return f"❌ OCR Error: {weight}", "", image, gr.update(visible=True)
41
 
42
  match = re.search(r'(\d{1,3}\.\d{1,3})\s*(kg|g)?', weight)
43
  if match:
44
  numeric_value = float(match.group(1))
45
+ unit = match.group(2) if match.group(2) else extract_unit_from_text(raw_text)
46
  else:
 
47
  cleaned = re.sub(r"[^\d]", "", weight)
48
  decimal_fixed = restore_decimal(cleaned)
49
  try:
50
  numeric_value = float(decimal_fixed)
51
+ unit = extract_unit_from_text(raw_text)
52
  except:
53
  return f"❌ Could not extract number | OCR: {weight}", "", image, gr.update(visible=True)
54
 
 
87
  except Exception as e:
88
  return f"Error: {str(e)}", "", None, gr.update(visible=True)
89
 
90
+ with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}" ) as demo:
91
  gr.Markdown("""
92
  <h1 style='text-align: center; color: #2e7d32;'>πŸ“· Auto Weight Logger</h1>
93
  <p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR and logs it into Salesforce.</p>