import gradio as gr from PIL import Image from datetime import datetime from ocr_engine import extract_weight def process_image(image): if image is None: return "No image provided", None, None weight = extract_weight(image) now = datetime.now().strftime("%Y-%m-%d %H:%M:%S") return f"{weight} grams", now, image with gr.Blocks() as demo: gr.Markdown("## 📷 Auto Weight Logger — OCR-Based Smart Scale Reader") gr.Markdown("Upload a snapshot or use webcam. The app extracts weight using OCR.") with gr.Row(): camera_input = gr.Camera(label="📸 Capture via Webcam", type="pil") upload_input = gr.Image(label="📁 Upload Image", type="pil") selected_image = gr.State() def choose_latest_image(cam_img, upload_img): return cam_img if cam_img is not None else upload_img camera_input.change(fn=choose_latest_image, inputs=[camera_input, upload_input], outputs=selected_image) upload_input.change(fn=choose_latest_image, inputs=[camera_input, upload_input], outputs=selected_image) btn = gr.Button("🚀 Detect Weight") weight_out = gr.Textbox(label="Detected Weight") time_out = gr.Textbox(label="Captured At (IST)") preview = gr.Image(label="Snapshot") btn.click(fn=process_image, inputs=selected_image, outputs=[weight_out, time_out, preview]) demo.launch()