sci-image / FINAL_SOLUTION.md
Gaston895's picture
Add Gradio API ping endpoint
e6453d3
|
Raw
History Blame Contribute Delete
3.24 kB

Final Solution: Ping Endpoint for Hugging Face Space

Current Situation

The Hugging Face Space at https://gsstec-sci-image.hf.space is configured to run in --hfdemo mode, which provides a Gradio UI for interactive image generation.

The Ping Endpoint Challenge

Problem: You need a /api/ping endpoint for UptimeRobot monitoring, but the application architecture doesn't support running both Gradio UI and FastAPI endpoints simultaneously.

Solution Options

Option 1: Use Gradio's Built-in API (RECOMMENDED)

Gradio automatically provides API endpoints for all functions. Use Gradio's health check:

Endpoint: https://gsstec-sci-image.hf.space/gradio_api/call/predict

UptimeRobot Configuration:

  • Monitor Type: HTTP(s)
  • URL: https://gsstec-sci-image.hf.space/
  • Method: GET
  • Expected Response: 200 OK (Gradio UI loads)

Option 2: Deploy Two Separate Spaces

  1. UI Space (Current): https://gsstec-sci-image.hf.space

    • Mode: --hfdemo
    • Purpose: Interactive UI for users
  2. API Space (New): https://gsstec-sci-image-api.hf.space

    • Mode: --api
    • Purpose: REST API endpoints + monitoring
    • Ping: https://gsstec-sci-image-api.hf.space/api/ping

Option 3: Switch to API-Only Mode

Change Dockerfile to use --api mode:

CMD ["python", "src/app.py","--api","--port","7860"]

Trade-off: Loses interactive UI, gains full REST API

Recommended Approach

Keep the current --hfdemo setup and use one of these monitoring strategies:

  1. Monitor the root URL: https://gsstec-sci-image.hf.space/

    • UptimeRobot checks if the Gradio UI loads
    • Simple and effective
  2. Use Gradio's API endpoint: https://gsstec-sci-image.hf.space/gradio_api/call/predict

    • Monitors the actual generation functionality
    • More comprehensive health check
  3. Deploy a separate API-only Space for monitoring and programmatic access

    • Best of both worlds
    • Requires maintaining two deployments

Current Files Status

  • Dockerfile - Set to --hfdemo (Gradio UI)
  • hf_demo.py - Standard Gradio launch
  • web.py - FastAPI with HEAD support (not active in hfdemo mode)

Why Hybrid Mode Doesn't Work

The application uses two different web frameworks:

  • Gradio: For the interactive UI (port 7860)
  • FastAPI: For REST API endpoints (port 7860)

Both try to bind to the same port, and the architecture doesn't support running them together without significant refactoring.

Next Steps

  1. For immediate monitoring: Configure UptimeRobot to monitor https://gsstec-sci-image.hf.space/ (root URL)

  2. For API access: Deploy a second Space in --api mode for programmatic access and dedicated ping endpoint

  3. For long-term solution: Refactor the application to use FastAPI as the primary server with Gradio mounted as a sub-application (requires significant code changes)

Summary

Ping Endpoint: Not available in current --hfdemo mode
Alternative: Monitor root URL https://gsstec-sci-image.hf.space/
Best Solution: Deploy separate API-only Space for monitoring