Update api.py
Browse files
api.py
CHANGED
|
@@ -45,9 +45,6 @@ async def root():
|
|
| 45 |
|
| 46 |
@app.post("/predict/")
|
| 47 |
async def predict(file: UploadFile = File(...)):
|
| 48 |
-
"""
|
| 49 |
-
Returns JSON with detections (no image).
|
| 50 |
-
"""
|
| 51 |
try:
|
| 52 |
contents = await file.read()
|
| 53 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
|
@@ -88,9 +85,6 @@ async def predict(file: UploadFile = File(...)):
|
|
| 88 |
|
| 89 |
@app.post("/predict_image/")
|
| 90 |
async def predict_image(file: UploadFile = File(...)):
|
| 91 |
-
"""
|
| 92 |
-
Returns the annotated image only (PNG).
|
| 93 |
-
"""
|
| 94 |
try:
|
| 95 |
contents = await file.read()
|
| 96 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
|
@@ -108,3 +102,37 @@ async def predict_image(file: UploadFile = File(...)):
|
|
| 108 |
|
| 109 |
except Exception as e:
|
| 110 |
return JSONResponse({"error": str(e)}, status_code=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
@app.post("/predict/")
|
| 47 |
async def predict(file: UploadFile = File(...)):
|
|
|
|
|
|
|
|
|
|
| 48 |
try:
|
| 49 |
contents = await file.read()
|
| 50 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
|
|
|
| 85 |
|
| 86 |
@app.post("/predict_image/")
|
| 87 |
async def predict_image(file: UploadFile = File(...)):
|
|
|
|
|
|
|
|
|
|
| 88 |
try:
|
| 89 |
contents = await file.read()
|
| 90 |
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
|
|
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
return JSONResponse({"error": str(e)}, status_code=500)
|
| 105 |
+
|
| 106 |
+
# ✅ Nouvelle route de test sur Hugging Face
|
| 107 |
+
@app.get("/test_request/")
|
| 108 |
+
async def test_request():
|
| 109 |
+
"""
|
| 110 |
+
Teste l'API déployée sur Hugging Face : envoie une image vers /predict et /predict_image,
|
| 111 |
+
puis sauvegarde les résultats.
|
| 112 |
+
"""
|
| 113 |
+
try:
|
| 114 |
+
file_path = "test.jpg" # mets ton image dans ton Space
|
| 115 |
+
base_url = "https://stroke-ia-api.hf.space" # ⚠️ adapte au nom exact de ton Space
|
| 116 |
+
|
| 117 |
+
# 1) Test JSON (détection)
|
| 118 |
+
url_predict = f"{base_url}/predict/"
|
| 119 |
+
files = {"file": open(file_path, "rb")}
|
| 120 |
+
response = requests.post(url_predict, files=files)
|
| 121 |
+
json_result = response.json()
|
| 122 |
+
|
| 123 |
+
# 2) Test image annotée
|
| 124 |
+
url_img = f"{base_url}/predict_image/"
|
| 125 |
+
files = {"file": open(file_path, "rb")}
|
| 126 |
+
response_img = requests.post(url_img, files=files)
|
| 127 |
+
|
| 128 |
+
with open("result.png", "wb") as f:
|
| 129 |
+
f.write(response_img.content)
|
| 130 |
+
|
| 131 |
+
return {
|
| 132 |
+
"message": "✅ Test request executed on Hugging Face API. Results saved.",
|
| 133 |
+
"json_result": json_result,
|
| 134 |
+
"saved_image": "result.png"
|
| 135 |
+
}
|
| 136 |
+
|
| 137 |
+
except Exception as e:
|
| 138 |
+
return {"error": str(e)}
|