Add base64 API endpoint for direct backend integration
Browse files- Added classify_base64 function to accept base64 images directly
- Exposed as API-only endpoint using gr.Interface with hidden UI
- Added stable API names to existing endpoints
- Enables vision backend to call Space without file uploads
- Based on GPT recommendation in codexmis.md
app.py
CHANGED
|
@@ -268,14 +268,16 @@ with gr.Blocks(title="MobileCLIP Image Classifier") as demo:
|
|
| 268 |
classify_btn.click(
|
| 269 |
fn=classify_image,
|
| 270 |
inputs=[input_image, top_k_slider],
|
| 271 |
-
outputs=[output_text, output_chart]
|
|
|
|
| 272 |
)
|
| 273 |
|
| 274 |
# Also trigger on image upload
|
| 275 |
input_image.change(
|
| 276 |
fn=classify_image,
|
| 277 |
inputs=[input_image, top_k_slider],
|
| 278 |
-
outputs=[output_text, output_chart]
|
|
|
|
| 279 |
)
|
| 280 |
|
| 281 |
with gr.Tab("🔧 Admin Panel"):
|
|
@@ -322,7 +324,8 @@ with gr.Blocks(title="MobileCLIP Image Classifier") as demo:
|
|
| 322 |
upsert_btn.click(
|
| 323 |
fn=upsert_labels_admin,
|
| 324 |
inputs=[admin_token_input, new_items_input],
|
| 325 |
-
outputs=upsert_output
|
|
|
|
| 326 |
)
|
| 327 |
|
| 328 |
with gr.Accordion("🔄 Reload Label Version", open=False):
|
|
@@ -411,6 +414,40 @@ with gr.Blocks(title="MobileCLIP Image Classifier") as demo:
|
|
| 411 |
|
| 412 |
print("Gradio interface created successfully!")
|
| 413 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
if __name__ == "__main__":
|
| 415 |
print("Launching Gradio app...")
|
| 416 |
demo.launch()
|
|
|
|
| 268 |
classify_btn.click(
|
| 269 |
fn=classify_image,
|
| 270 |
inputs=[input_image, top_k_slider],
|
| 271 |
+
outputs=[output_text, output_chart],
|
| 272 |
+
api_name="classify_image"
|
| 273 |
)
|
| 274 |
|
| 275 |
# Also trigger on image upload
|
| 276 |
input_image.change(
|
| 277 |
fn=classify_image,
|
| 278 |
inputs=[input_image, top_k_slider],
|
| 279 |
+
outputs=[output_text, output_chart],
|
| 280 |
+
api_name="classify_image_1"
|
| 281 |
)
|
| 282 |
|
| 283 |
with gr.Tab("🔧 Admin Panel"):
|
|
|
|
| 324 |
upsert_btn.click(
|
| 325 |
fn=upsert_labels_admin,
|
| 326 |
inputs=[admin_token_input, new_items_input],
|
| 327 |
+
outputs=upsert_output,
|
| 328 |
+
api_name="upsert_labels_admin"
|
| 329 |
)
|
| 330 |
|
| 331 |
with gr.Accordion("🔄 Reload Label Version", open=False):
|
|
|
|
| 414 |
|
| 415 |
print("Gradio interface created successfully!")
|
| 416 |
|
| 417 |
+
# Add pure API endpoint for base64 classification (as suggested by GPT)
|
| 418 |
+
def classify_base64(image_b64: str, top_k: int = 10):
|
| 419 |
+
"""
|
| 420 |
+
API-only endpoint that accepts base64 images directly.
|
| 421 |
+
This enables direct API calls from backends without file uploads.
|
| 422 |
+
"""
|
| 423 |
+
if handler is None:
|
| 424 |
+
return {"error": "handler not initialized"}
|
| 425 |
+
|
| 426 |
+
try:
|
| 427 |
+
# Call handler directly with base64
|
| 428 |
+
result = handler({
|
| 429 |
+
"inputs": {
|
| 430 |
+
"image": image_b64,
|
| 431 |
+
"top_k": int(top_k)
|
| 432 |
+
}
|
| 433 |
+
})
|
| 434 |
+
return result
|
| 435 |
+
except Exception as e:
|
| 436 |
+
return {"error": str(e)}
|
| 437 |
+
|
| 438 |
+
# Register the API endpoint (no UI)
|
| 439 |
+
with demo:
|
| 440 |
+
gr.Interface(
|
| 441 |
+
fn=classify_base64,
|
| 442 |
+
inputs=[
|
| 443 |
+
gr.Textbox(label="image_b64", visible=False),
|
| 444 |
+
gr.Number(label="top_k", visible=False)
|
| 445 |
+
],
|
| 446 |
+
outputs=gr.JSON(visible=False),
|
| 447 |
+
api_name="classify_base64",
|
| 448 |
+
visible=False
|
| 449 |
+
)
|
| 450 |
+
|
| 451 |
if __name__ == "__main__":
|
| 452 |
print("Launching Gradio app...")
|
| 453 |
demo.launch()
|