File size: 3,760 Bytes
39a3c42 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | # 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
```bash
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:
```bash
# 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:
```javascript
{
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 |