Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -259,42 +259,59 @@ def ocr_process_np_image(np_img, detail=1):
|
|
| 259 |
return joined_text, ocr_blocks
|
| 260 |
|
| 261 |
# ---------------------
|
| 262 |
-
# Programmatic functions
|
| 263 |
# ---------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
def run_by_image_base64(image_base64: str, detail=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 265 |
try:
|
| 266 |
np_img = _b64_to_rgb_np(image_base64)
|
| 267 |
joined_text, _ = ocr_process_np_image(np_img, detail=int(detail))
|
| 268 |
ingredients = extract_ingredients_from_text(joined_text)
|
| 269 |
-
return
|
| 270 |
except Exception as e:
|
| 271 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
|
| 273 |
def run_by_image_url(image_url: str, detail=1):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
try:
|
| 275 |
if not image_url or not isinstance(image_url, str):
|
| 276 |
-
return
|
| 277 |
resp = requests.get(image_url, timeout=15)
|
| 278 |
resp.raise_for_status()
|
| 279 |
pil = Image.open(io.BytesIO(resp.content)).convert("RGB")
|
| 280 |
np_img = np.array(pil)
|
| 281 |
joined_text, _ = ocr_process_np_image(np_img, detail=int(detail))
|
| 282 |
ingredients = extract_ingredients_from_text(joined_text)
|
| 283 |
-
return
|
| 284 |
-
except Exception as e:
|
| 285 |
-
return json.dumps({"error": str(e), "traceback": traceback.format_exc()}, indent=2)
|
| 286 |
-
|
| 287 |
-
def run_by_image_file(img, detail=1):
|
| 288 |
-
try:
|
| 289 |
-
if isinstance(img, np.ndarray):
|
| 290 |
-
np_img = img
|
| 291 |
-
else:
|
| 292 |
-
np_img = np.array(img.convert("RGB"))
|
| 293 |
-
joined_text, _ = ocr_process_np_image(np_img, detail=int(detail))
|
| 294 |
-
ingredients = extract_ingredients_from_text(joined_text)
|
| 295 |
-
return json.dumps(ingredients, indent=2)
|
| 296 |
except Exception as e:
|
| 297 |
-
return
|
| 298 |
|
| 299 |
# ---------------------
|
| 300 |
# Your existing wrappers (kept)
|
|
|
|
| 259 |
return joined_text, ocr_blocks
|
| 260 |
|
| 261 |
# ---------------------
|
| 262 |
+
# Programmatic functions (modified to return a single comma-separated string)
|
| 263 |
# ---------------------
|
| 264 |
+
def _ingredients_to_string(ingredients_list):
|
| 265 |
+
"""Return a clean comma-separated string from the ingredients list."""
|
| 266 |
+
if not ingredients_list:
|
| 267 |
+
return ""
|
| 268 |
+
return ", ".join(ingredients_list)
|
| 269 |
+
|
| 270 |
def run_by_image_base64(image_base64: str, detail=1):
|
| 271 |
+
"""
|
| 272 |
+
Accepts a base64 image string and RETURNS ONLY a comma-separated ingredient string.
|
| 273 |
+
Exposed at /run/run_by_image_base64 when the Space runs.
|
| 274 |
+
"""
|
| 275 |
try:
|
| 276 |
np_img = _b64_to_rgb_np(image_base64)
|
| 277 |
joined_text, _ = ocr_process_np_image(np_img, detail=int(detail))
|
| 278 |
ingredients = extract_ingredients_from_text(joined_text)
|
| 279 |
+
return _ingredients_to_string(ingredients)
|
| 280 |
except Exception as e:
|
| 281 |
+
return f"ERROR: {str(e)}"
|
| 282 |
+
|
| 283 |
+
def run_by_image_file(img, detail=1):
|
| 284 |
+
"""
|
| 285 |
+
Gradio UI function: accepts an uploaded image (PIL or numpy) and RETURNS a comma-separated ingredient string.
|
| 286 |
+
"""
|
| 287 |
+
try:
|
| 288 |
+
if isinstance(img, np.ndarray):
|
| 289 |
+
np_img = img
|
| 290 |
+
else:
|
| 291 |
+
np_img = np.array(img.convert("RGB"))
|
| 292 |
+
joined_text, _ = ocr_process_np_image(np_img, detail=int(detail))
|
| 293 |
+
ingredients = extract_ingredients_from_text(joined_text)
|
| 294 |
+
return _ingredients_to_string(ingredients)
|
| 295 |
+
except Exception as e:
|
| 296 |
+
return f"ERROR: {str(e)}"
|
| 297 |
|
| 298 |
def run_by_image_url(image_url: str, detail=1):
|
| 299 |
+
"""
|
| 300 |
+
Accepts a public image URL, fetches the image server-side, runs OCR, and RETURNS a comma-separated ingredient string.
|
| 301 |
+
Exposed at /run/run_by_image_url when the Space runs.
|
| 302 |
+
"""
|
| 303 |
try:
|
| 304 |
if not image_url or not isinstance(image_url, str):
|
| 305 |
+
return "ERROR: image_url required"
|
| 306 |
resp = requests.get(image_url, timeout=15)
|
| 307 |
resp.raise_for_status()
|
| 308 |
pil = Image.open(io.BytesIO(resp.content)).convert("RGB")
|
| 309 |
np_img = np.array(pil)
|
| 310 |
joined_text, _ = ocr_process_np_image(np_img, detail=int(detail))
|
| 311 |
ingredients = extract_ingredients_from_text(joined_text)
|
| 312 |
+
return _ingredients_to_string(ingredients)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 313 |
except Exception as e:
|
| 314 |
+
return f"ERROR: {str(e)}"
|
| 315 |
|
| 316 |
# ---------------------
|
| 317 |
# Your existing wrappers (kept)
|