File size: 1,609 Bytes
0590b95
d55b56b
 
9bbac2d
d55b56b
0590b95
d55b56b
a4391a6
 
91bcc4e
 
9bbac2d
77bb0dd
 
91bcc4e
a4391a6
d55b56b
77bb0dd
 
 
 
 
 
 
 
 
 
 
136c114
77bb0dd
 
 
9bbac2d
77bb0dd
f32159a
 
0590b95
77bb0dd
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import gradio as gr
from PIL import Image
from datetime import datetime
import pytz
from ocr_engine import extract_weight

def process_image(image):
    if image is None:
        return "No image provided", "", None
    try:
        weight = extract_weight(image)
        ist = pytz.timezone('Asia/Kolkata')
        timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
        return weight, timestamp, image
    except Exception as e:
        return f"Error: {str(e)}", "", None

with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
    gr.Markdown("""
    <h1 style='text-align: center; color: #2e7d32;'>πŸ“· Auto Weight Logger</h1>
    <p style='text-align: center;'>AI-powered OCR tool to detect weights (kg or grams) from digital balance display images.</p>
    <hr style='border: 1px solid #ddd;'/>
    """)

    with gr.Row():
        image_input = gr.Image(type="pil", label="πŸ“ Upload or Capture Image", elem_id="img_upload")

    detect_btn = gr.Button("πŸš€ Detect Weight", elem_id="detect_btn")

    with gr.Row():
        weight_out = gr.Textbox(label="πŸ“¦ Detected Weight", placeholder="e.g., 72.4 kg", show_copy_button=True)
        time_out = gr.Textbox(label="πŸ•’ Captured At (IST)", placeholder="e.g., 2025-06-30 14:32:10")

    snapshot = gr.Image(label="πŸ“Έ Snapshot Preview")

    detect_btn.click(fn=process_image, inputs=image_input, outputs=[weight_out, time_out, snapshot])

    gr.Markdown("""
    <br><p style='text-align: center; color: gray;'>Developed by Shalu | Powered by Hugging Face OCR πŸš€</p>
    """)