sci-image / DEPLOY_API_FIX.md
Gaston895's picture
Add API endpoint for external calls - Fix Window 9 integration
39a3c42

HuggingFace Space API Fix - Deployment Guide

Problem Identified

The original hf_demo.py had no API endpoint configured. The predict function was only connected to the UI button, making it impossible to call via the Gradio API.

Solution Implemented

Updated sci-image/src/frontend/webui/hf_demo.py with:

  1. API-enabled predict function with proper parameters:

    • prompt: Text description
    • width: Image width (default: 512)
    • height: Image height (default: 512)
    • num_images: Number of images (default: 1)
    • steps: Inference steps (default: 4)
    • guidance_scale: Guidance scale (default: 1.0)
    • seed: Random seed, -1 for random (default: -1)
  2. Separate UI and API functions:

    • ui_predict(): Handles UI button clicks
    • predict(): Handles API calls with full parameters
  3. gr.Interface for API:

    • Created explicit API interface with api_name="predict"
    • This enables /api/predict endpoint

Deployment Steps

1. Push to HuggingFace Space

cd C:\Users\Dell\Desktop\TimeTravel-main\sci-image

# Add and commit changes
git add src/frontend/webui/hf_demo.py
git commit -m "Add API endpoint for external calls"

# Push to HuggingFace Space
git push origin main

2. Wait for Space to Rebuild

3. Test the API Endpoint

Test via Gradio API:

# Step 1: Call predict
curl -X POST https://gsstec-sci-image.hf.space/gradio_api/call/predict \
  -H "Content-Type: application/json" \
  -d '{
    "data": [
      "a virus under microscope, scientific visualization",
      512,
      512,
      1,
      4,
      1.0,
      -1
    ]
  }'

# Response: {"event_id": "some-id"}

# Step 2: Poll for results
curl https://gsstec-sci-image.hf.space/gradio_api/call/predict/{event_id}

4. Test from Frontend

Navigate to: http://localhost:8080/window/9/test

The test page will now:

  1. Call the Space API with proper parameters
  2. Poll for results
  3. Display the generated image
  4. Show detailed logs of the entire process

API Parameters Mapping

Our frontend sends:

{
  data: [
    prompt,           // "a virus under microscope..."
    512,              // width
    512,              // height
    1,                // num_images
    4,                // num_inference_steps
    1.0,              // guidance_scale
    -1                // seed (random)
  ]
}

The Space receives these in order as function parameters.

Expected Response Format

The Space will return via SSE stream:

data: [{"url": "https://gsstec-sci-image.hf.space/file=/tmp/gradio/xxx.webp", "path": "/tmp/gradio/xxx.webp"}]

Our frontend extracts data[0].url and displays the image.

Troubleshooting

If API still returns null:

  1. Check Space logs for errors
  2. Verify the Space rebuilt successfully
  3. Test the UI directly on the Space to ensure generation works
  4. Check if the API endpoint is accessible: https://gsstec-sci-image.hf.space/api/predict

If images don't display:

  1. The temporary URLs expire quickly
  2. Consider downloading the image immediately in the Durable Object
  3. Or convert to base64 in the Space before returning

Next Steps After Deployment

  1. Test the API: Use the test page to verify it works
  2. Update Window 9: Once confirmed working, update the main Window 9 page
  3. Add image caching: Implement immediate download in Durable Object to avoid URL expiration
  4. Add error handling: Better error messages for failed generations

Files Modified

  • sci-image/src/frontend/webui/hf_demo.py - Added API interface and proper parameter handling