Sanjayraju30 commited on
Commit
c8c908b
·
verified ·
1 Parent(s): 325b9a0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -8
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # app.py
2
  import gradio as gr
3
  from PIL import Image
4
  from datetime import datetime
@@ -17,6 +16,13 @@ SF_TOKEN = "UgiHKWT0aoZRX9gvTYDjAiRY"
17
  # Connect to Salesforce
18
  sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_TOKEN)
19
 
 
 
 
 
 
 
 
20
  def process_image(image):
21
  if image is None:
22
  return "❌ No image provided", "", None, gr.update(visible=True)
@@ -30,12 +36,19 @@ def process_image(image):
30
  if not weight or weight.startswith("Error"):
31
  return f"❌ OCR Error: {weight}", "", image, gr.update(visible=True)
32
 
33
- numeric_match = re.search(r'\d{1,5}(\.\d{1,3})?', weight)
34
- if not numeric_match:
35
- return f"❌ Could not extract number | OCR: {weight}", "", image, gr.update(visible=True)
36
-
37
- numeric_value = float(numeric_match.group())
38
- unit = "kg" if "kg" in weight.lower() else "g"
 
 
 
 
 
 
 
39
 
40
  image_path = "snapshot.jpg"
41
  image.save(image_path)
@@ -68,7 +81,7 @@ def process_image(image):
68
  "Visibility": "AllUsers"
69
  })
70
 
71
- return weight, timestamp, image, gr.update(visible=False)
72
  except Exception as e:
73
  return f"Error: {str(e)}", "", None, gr.update(visible=True)
74
 
 
 
1
  import gradio as gr
2
  from PIL import Image
3
  from datetime import datetime
 
16
  # Connect to Salesforce
17
  sf = Salesforce(username=SF_USERNAME, password=SF_PASSWORD, security_token=SF_TOKEN)
18
 
19
+ def restore_decimal(text):
20
+ if re.fullmatch(r"\d{5}", text):
21
+ return f"{text[:2]}.{text[2:]}"
22
+ elif re.fullmatch(r"\d{4}", text):
23
+ return f"{text[:2]}.{text[2:]}"
24
+ return text
25
+
26
  def process_image(image):
27
  if image is None:
28
  return "❌ No image provided", "", None, gr.update(visible=True)
 
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
 
53
  image_path = "snapshot.jpg"
54
  image.save(image_path)
 
81
  "Visibility": "AllUsers"
82
  })
83
 
84
+ return f"{numeric_value} {unit}", timestamp, image, gr.update(visible=False)
85
  except Exception as e:
86
  return f"Error: {str(e)}", "", None, gr.update(visible=True)
87