Spaces:
Sleeping
Sleeping
Fix port conflicts and API issues - use Gradio built-in API
Browse files
app.py
CHANGED
|
@@ -9,21 +9,12 @@ from ultralytics import YOLO
|
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image
|
| 11 |
import json
|
| 12 |
-
import base64
|
| 13 |
-
from io import BytesIO
|
| 14 |
from typing import Dict, Tuple, Any
|
| 15 |
import logging
|
| 16 |
-
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 17 |
-
from fastapi.responses import JSONResponse
|
| 18 |
-
import uvicorn
|
| 19 |
-
from threading import Thread
|
| 20 |
|
| 21 |
logging.basicConfig(level=logging.INFO)
|
| 22 |
logger = logging.getLogger(__name__)
|
| 23 |
|
| 24 |
-
# Initialize FastAPI app for API endpoints
|
| 25 |
-
app = FastAPI(title="Hand Detection API")
|
| 26 |
-
|
| 27 |
# Load the model
|
| 28 |
MODEL_PATH = "https://huggingface.co/EtanHey/hand-sign-detection/resolve/main/model.pt"
|
| 29 |
model = None
|
|
@@ -149,52 +140,13 @@ def gradio_predict(image: Image.Image) -> Tuple[str, Dict, str]:
|
|
| 149 |
|
| 150 |
return output_text, confidence_scores, json_output
|
| 151 |
|
| 152 |
-
#
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
"status": "online",
|
| 158 |
-
"model": "hand-sign-detection",
|
| 159 |
-
"classes": CLASS_NAMES,
|
| 160 |
-
"api_endpoints": {
|
| 161 |
-
"health": "/",
|
| 162 |
-
"predict": "/api/predict",
|
| 163 |
-
"predict_base64": "/api/predict/base64"
|
| 164 |
-
}
|
| 165 |
-
}
|
| 166 |
-
|
| 167 |
-
@app.post("/api/predict")
|
| 168 |
-
async def predict_api(file: UploadFile = File(...)):
|
| 169 |
-
"""API endpoint for file upload prediction"""
|
| 170 |
-
try:
|
| 171 |
-
# Read image
|
| 172 |
-
contents = await file.read()
|
| 173 |
-
image = Image.open(BytesIO(contents))
|
| 174 |
-
|
| 175 |
-
# Process
|
| 176 |
-
result = process_image(image)
|
| 177 |
-
|
| 178 |
-
return JSONResponse(content=result)
|
| 179 |
-
|
| 180 |
-
except Exception as e:
|
| 181 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 182 |
-
|
| 183 |
-
@app.post("/api/predict/base64")
|
| 184 |
-
async def predict_base64_api(data: Dict[str, str]):
|
| 185 |
-
"""API endpoint for base64 image prediction"""
|
| 186 |
-
try:
|
| 187 |
-
# Decode base64 image
|
| 188 |
-
image_data = base64.b64decode(data["image"])
|
| 189 |
-
image = Image.open(BytesIO(image_data))
|
| 190 |
-
|
| 191 |
-
# Process
|
| 192 |
-
result = process_image(image)
|
| 193 |
-
|
| 194 |
-
return JSONResponse(content=result)
|
| 195 |
|
| 196 |
-
|
| 197 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 198 |
|
| 199 |
# Gradio Interface
|
| 200 |
def create_gradio_interface():
|
|
@@ -239,7 +191,7 @@ def create_gradio_interface():
|
|
| 239 |
|
| 240 |
**Model:** YOLOv8 trained on 1,740 images | **Accuracy:** 96.3%
|
| 241 |
|
| 242 |
-
**API Access:** Use
|
| 243 |
""",
|
| 244 |
article="""
|
| 245 |
### About
|
|
@@ -250,14 +202,17 @@ def create_gradio_interface():
|
|
| 250 |
|
| 251 |
### API Usage
|
| 252 |
```python
|
| 253 |
-
import
|
| 254 |
|
| 255 |
-
#
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
|
|
|
|
|
|
|
|
|
| 259 |
)
|
| 260 |
-
print(
|
| 261 |
```
|
| 262 |
|
| 263 |
### Model Card
|
|
@@ -271,21 +226,29 @@ def create_gradio_interface():
|
|
| 271 |
|
| 272 |
return interface
|
| 273 |
|
| 274 |
-
# Run FastAPI in background thread
|
| 275 |
-
def run_api():
|
| 276 |
-
"""Run FastAPI server in background"""
|
| 277 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
| 278 |
-
|
| 279 |
-
# Start API server in background
|
| 280 |
-
api_thread = Thread(target=run_api, daemon=True)
|
| 281 |
-
api_thread.start()
|
| 282 |
-
|
| 283 |
# Create and launch Gradio interface
|
| 284 |
if __name__ == "__main__":
|
|
|
|
| 285 |
interface = create_gradio_interface()
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
server_name="0.0.0.0",
|
| 288 |
-
server_port=
|
| 289 |
-
share=False
|
| 290 |
-
debug=True
|
| 291 |
)
|
|
|
|
| 9 |
import numpy as np
|
| 10 |
from PIL import Image
|
| 11 |
import json
|
|
|
|
|
|
|
| 12 |
from typing import Dict, Tuple, Any
|
| 13 |
import logging
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
logging.basicConfig(level=logging.INFO)
|
| 16 |
logger = logging.getLogger(__name__)
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
# Load the model
|
| 19 |
MODEL_PATH = "https://huggingface.co/EtanHey/hand-sign-detection/resolve/main/model.pt"
|
| 20 |
model = None
|
|
|
|
| 140 |
|
| 141 |
return output_text, confidence_scores, json_output
|
| 142 |
|
| 143 |
+
# API prediction function for Gradio's built-in API
|
| 144 |
+
def api_predict(image: Image.Image) -> Dict[str, Any]:
|
| 145 |
+
"""API function that returns raw results for API access"""
|
| 146 |
+
if image is None:
|
| 147 |
+
return {"error": "No image provided"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 148 |
|
| 149 |
+
return process_image(image)
|
|
|
|
| 150 |
|
| 151 |
# Gradio Interface
|
| 152 |
def create_gradio_interface():
|
|
|
|
| 191 |
|
| 192 |
**Model:** YOLOv8 trained on 1,740 images | **Accuracy:** 96.3%
|
| 193 |
|
| 194 |
+
**API Access:** Use Gradio's built-in API endpoints for programmatic access.
|
| 195 |
""",
|
| 196 |
article="""
|
| 197 |
### About
|
|
|
|
| 202 |
|
| 203 |
### API Usage
|
| 204 |
```python
|
| 205 |
+
from gradio_client import Client
|
| 206 |
|
| 207 |
+
# Connect to the API
|
| 208 |
+
client = Client("https://huggingface.co/spaces/EtanHey/hand-detection-api")
|
| 209 |
+
|
| 210 |
+
# Make prediction
|
| 211 |
+
result = client.predict(
|
| 212 |
+
image="path/to/your/image.jpg",
|
| 213 |
+
api_name="/predict"
|
| 214 |
)
|
| 215 |
+
print(result)
|
| 216 |
```
|
| 217 |
|
| 218 |
### Model Card
|
|
|
|
| 226 |
|
| 227 |
return interface
|
| 228 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 229 |
# Create and launch Gradio interface
|
| 230 |
if __name__ == "__main__":
|
| 231 |
+
# Create the main interface
|
| 232 |
interface = create_gradio_interface()
|
| 233 |
+
|
| 234 |
+
# Create API interface for programmatic access
|
| 235 |
+
api_interface = gr.Interface(
|
| 236 |
+
fn=api_predict,
|
| 237 |
+
inputs=gr.Image(type="pil"),
|
| 238 |
+
outputs=gr.JSON(),
|
| 239 |
+
title="Hand Detection API"
|
| 240 |
+
)
|
| 241 |
+
|
| 242 |
+
# Combine both interfaces in a tabbed interface
|
| 243 |
+
demo = gr.TabbedInterface(
|
| 244 |
+
[interface, api_interface],
|
| 245 |
+
["Web Interface", "API"],
|
| 246 |
+
title="🤚 Hand/Arm Detection AI"
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
# Launch on default HuggingFace Spaces port (7860)
|
| 250 |
+
demo.launch(
|
| 251 |
server_name="0.0.0.0",
|
| 252 |
+
server_port=7860,
|
| 253 |
+
share=False
|
|
|
|
| 254 |
)
|