logger1 / app.py
Sanjayraju30's picture
Update app.py
9965439 verified
raw
history blame
2.07 kB
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, gr.update(visible=True)
try:
weight = extract_weight(image)
ist = pytz.timezone('Asia/Kolkata')
timestamp = datetime.now(ist).strftime("%Y-%m-%d %H:%M:%S IST")
if not weight or "No valid" in weight:
return "❌ Unable to detect. Try again with clearer image.", "", image, gr.update(visible=True)
return weight, timestamp, image, gr.update(visible=False)
except Exception as e:
return f"Error: {str(e)}", "", None, gr.update(visible=True)
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;'>Upload or capture a digital weight image. Detects weight using OCR.</p>
<hr style='border: 1px solid #ddd;'/>
""")
with gr.Row():
image_input = gr.Image(type="pil", label="πŸ“ Upload or Capture Image")
detect_btn = gr.Button("πŸš€ Detect Weight")
with gr.Row():
weight_out = gr.Textbox(label="πŸ“¦ Detected Weight", placeholder="e.g., 97.9 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")
retake_btn = gr.Button("πŸ” Retake / Try Again", visible=False)
detect_btn.click(fn=process_image, inputs=image_input, outputs=[weight_out, time_out, snapshot, retake_btn])
retake_btn.click(fn=lambda: ("", "", None, gr.update(visible=False)),
inputs=[], outputs=[weight_out, time_out, snapshot, retake_btn])
gr.Markdown("""
<p style='text-align: center; color: gray;'>Tip: Ensure the image is clear and not blurry.<br>Developed by Shalu β€’ Hugging Face OCR ⚑</p>
""")
demo.launch()