Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,27 +2,54 @@ import gradio as gr
|
|
| 2 |
from PIL import Image
|
| 3 |
from datetime import datetime
|
| 4 |
import pytz
|
|
|
|
|
|
|
| 5 |
from ocr_engine import extract_weight
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
def process_image(image):
|
| 8 |
if image is None:
|
| 9 |
return "β No image provided", "", None, gr.update(visible=True)
|
|
|
|
| 10 |
try:
|
| 11 |
weight = extract_weight(image)
|
| 12 |
ist = pytz.timezone('Asia/Kolkata')
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
|
| 15 |
if not weight or "No valid" in weight:
|
| 16 |
return "β Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
except Exception as e:
|
| 20 |
return f"Error: {str(e)}", "", None, gr.update(visible=True)
|
| 21 |
|
|
|
|
| 22 |
with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
|
| 23 |
gr.Markdown("""
|
| 24 |
<h1 style='text-align: center; color: #2e7d32;'>π· Auto Weight Logger</h1>
|
| 25 |
-
<p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR.</p>
|
| 26 |
<hr style='border: 1px solid #ddd;'/>
|
| 27 |
""")
|
| 28 |
|
|
@@ -42,5 +69,4 @@ with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: whi
|
|
| 42 |
retake_btn.click(fn=lambda: ("", "", None, gr.update(visible=False)),
|
| 43 |
inputs=[], outputs=[weight_out, time_out, snapshot, retake_btn])
|
| 44 |
|
| 45 |
-
# β
Required for Hugging Face to launch the app properly
|
| 46 |
demo.launch()
|
|
|
|
| 2 |
from PIL import Image
|
| 3 |
from datetime import datetime
|
| 4 |
import pytz
|
| 5 |
+
import os
|
| 6 |
+
from simple_salesforce import Salesforce
|
| 7 |
from ocr_engine import extract_weight
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
|
| 10 |
+
# Load environment variables
|
| 11 |
+
load_dotenv()
|
| 12 |
+
|
| 13 |
+
# Salesforce connection
|
| 14 |
+
sf = Salesforce(
|
| 15 |
+
username=os.getenv("SF_USERNAME"),
|
| 16 |
+
password=os.getenv("SF_PASSWORD"),
|
| 17 |
+
security_token=os.getenv("SF_TOKEN")
|
| 18 |
+
)
|
| 19 |
|
| 20 |
def process_image(image):
|
| 21 |
if image is None:
|
| 22 |
return "β No image provided", "", None, gr.update(visible=True)
|
| 23 |
+
|
| 24 |
try:
|
| 25 |
weight = extract_weight(image)
|
| 26 |
ist = pytz.timezone('Asia/Kolkata')
|
| 27 |
+
now = datetime.now(ist)
|
| 28 |
+
timestamp_display = now.strftime("%Y-%m-%d %H:%M:%S IST")
|
| 29 |
+
timestamp_iso = now.isoformat()
|
| 30 |
|
| 31 |
if not weight or "No valid" in weight:
|
| 32 |
return "β Unable to detect. Try again with a clearer image.", "", image, gr.update(visible=True)
|
| 33 |
|
| 34 |
+
# Create Salesforce record
|
| 35 |
+
sf.Weight_Log__c.create({
|
| 36 |
+
'Captured_Weight__c': float(weight.replace("kg", "").strip()),
|
| 37 |
+
'Captured_At__c': timestamp_iso,
|
| 38 |
+
'Device_ID__c': 'Device-01', # Static or dynamic device ID
|
| 39 |
+
'Snapshot_Image__c': 'Manual Entry',
|
| 40 |
+
'Status__c': 'Captured'
|
| 41 |
+
})
|
| 42 |
+
|
| 43 |
+
return weight, timestamp_display, image, gr.update(visible=False)
|
| 44 |
+
|
| 45 |
except Exception as e:
|
| 46 |
return f"Error: {str(e)}", "", None, gr.update(visible=True)
|
| 47 |
|
| 48 |
+
# Gradio UI
|
| 49 |
with gr.Blocks(css=".gr-button {background-color: #2e7d32 !important; color: white !important;}") as demo:
|
| 50 |
gr.Markdown("""
|
| 51 |
<h1 style='text-align: center; color: #2e7d32;'>π· Auto Weight Logger</h1>
|
| 52 |
+
<p style='text-align: center;'>Upload or capture a digital weight image. Detects weight using AI OCR and stores the result in Salesforce.</p>
|
| 53 |
<hr style='border: 1px solid #ddd;'/>
|
| 54 |
""")
|
| 55 |
|
|
|
|
| 69 |
retake_btn.click(fn=lambda: ("", "", None, gr.update(visible=False)),
|
| 70 |
inputs=[], outputs=[weight_out, time_out, snapshot, retake_btn])
|
| 71 |
|
|
|
|
| 72 |
demo.launch()
|