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:
API-enabled predict function with proper parameters:
prompt: Text descriptionwidth: 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)
Separate UI and API functions:
ui_predict(): Handles UI button clickspredict(): Handles API calls with full parameters
gr.Interface for API:
- Created explicit API interface with
api_name="predict" - This enables
/api/predictendpoint
- Created explicit API interface with
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
- Go to: https://huggingface.co/spaces/gsstec/sci-image
- Wait for the Space to rebuild (usually 2-5 minutes)
- Check the logs for any errors
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:
- Call the Space API with proper parameters
- Poll for results
- Display the generated image
- 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:
- Check Space logs for errors
- Verify the Space rebuilt successfully
- Test the UI directly on the Space to ensure generation works
- Check if the API endpoint is accessible:
https://gsstec-sci-image.hf.space/api/predict
If images don't display:
- The temporary URLs expire quickly
- Consider downloading the image immediately in the Durable Object
- Or convert to base64 in the Space before returning
Next Steps After Deployment
- Test the API: Use the test page to verify it works
- Update Window 9: Once confirmed working, update the main Window 9 page
- Add image caching: Implement immediate download in Durable Object to avoid URL expiration
- 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