Sanjayraju30 commited on
Commit
0112378
·
verified ·
1 Parent(s): 7e1096c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -22
app.py CHANGED
@@ -1,37 +1,37 @@
1
  import gradio as gr
2
- from PIL import Image, UnidentifiedImageError
3
- from datetime import datetime
4
- import pytz
5
  from ocr_engine import extract_weight_from_image
 
6
 
7
- def detect_weight(image: Image.Image):
8
  try:
9
- # Get IST time
10
- ist_time = datetime.now(pytz.timezone("Asia/Kolkata")).strftime("%Y-%m-%d %H:%M:%S")
11
 
12
- # OCR extraction
13
  weight, confidence = extract_weight_from_image(image)
 
14
 
15
- if confidence > 0:
16
- result = f" **Detected Weight:** {weight} \n📊 **Confidence:** {confidence:.2f} \n🕒 **Captured At (IST):** {ist_time}"
17
- else:
18
- result = "❌ No weight detected. Please upload a clearer image."
19
 
20
- return image, result
21
- except UnidentifiedImageError:
22
- return None, "❌ Invalid image format."
23
 
24
- demo = gr.Interface(
25
- fn=detect_weight,
26
- inputs=gr.Image(type="pil", label="Upload or Capture Image"),
 
 
 
27
  outputs=[
28
- gr.Image(type="pil", label="Snapshot"),
29
- gr.Markdown(label="Result")
 
 
30
  ],
31
  title="⚖️ Auto Weight Logger",
32
- description="Upload or capture a digital scale image to detect weight automatically.",
33
- allow_flagging="never"
34
  )
35
 
36
  if __name__ == "__main__":
37
- demo.launch()
 
1
  import gradio as gr
 
 
 
2
  from ocr_engine import extract_weight_from_image
3
+ from utils import get_ist_time
4
 
5
+ def process_image(image):
6
  try:
7
+ if image is None:
8
+ return "No image provided", "-", None, "-"
9
 
 
10
  weight, confidence = extract_weight_from_image(image)
11
+ timestamp = get_ist_time()
12
 
13
+ if not weight:
14
+ return "No weight detected", timestamp, image, "-"
 
 
15
 
16
+ log_url = f"https://your-salesforce-site.com/log-weight?weight={weight}&time={timestamp}"
17
+ return f"{weight} g (Confidence: {confidence}%)", timestamp, image, log_url
 
18
 
19
+ except Exception as e:
20
+ return f"Error: {str(e)}", "-", None, "-"
21
+
22
+ iface = gr.Interface(
23
+ fn=process_image,
24
+ inputs=gr.Image(type="pil", label="Upload or Capture Snapshot"),
25
  outputs=[
26
+ gr.Textbox(label="Detected Weight"),
27
+ gr.Textbox(label="Captured At (IST)"),
28
+ gr.Image(label="Snapshot Image"),
29
+ gr.Textbox(label="Salesforce Log URL"),
30
  ],
31
  title="⚖️ Auto Weight Logger",
32
+ description="Upload or capture a digital scale snapshot to detect weight automatically.",
33
+ live=True,
34
  )
35
 
36
  if __name__ == "__main__":
37
+ iface.launch()