Spaces:
Sleeping
Sleeping
Update app.py
Browse files延伸實作 predict_image 版本
app.py
CHANGED
|
@@ -47,8 +47,29 @@ def predict_text_api(payload: dict):
|
|
| 47 |
except Exception as e:
|
| 48 |
return {"error": str(e)}
|
| 49 |
|
| 50 |
-
# ✅ API 路由:圖片分析(POST
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
# ✅ Gradio UI 功能
|
| 53 |
def predict_text(text, mode):
|
| 54 |
result = analyze_text(text=text, explain_mode=mode)
|
|
|
|
| 47 |
except Exception as e:
|
| 48 |
return {"error": str(e)}
|
| 49 |
|
| 50 |
+
# ✅ API 路由:圖片分析(POST)
|
| 51 |
+
@api.post("/run/predict_image")
|
| 52 |
+
async def predict_image_api(file: UploadFile = File(...), explain_mode: str = Form("cnn")):
|
| 53 |
+
try:
|
| 54 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as temp_file:
|
| 55 |
+
temp_file.write(await file.read())
|
| 56 |
+
temp_file_path = temp_file.name
|
| 57 |
+
|
| 58 |
+
with open(temp_file_path, "rb") as f:
|
| 59 |
+
result = analyze_image(f.read(), explain_mode=explain_mode)
|
| 60 |
|
| 61 |
+
os.remove(temp_file_path)
|
| 62 |
+
|
| 63 |
+
return {
|
| 64 |
+
"data": [
|
| 65 |
+
result["status"],
|
| 66 |
+
f'{result["confidence"]}%',
|
| 67 |
+
", ".join(result["suspicious_keywords"])
|
| 68 |
+
]
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
except Exception as e:
|
| 72 |
+
return {"error": str(e)}
|
| 73 |
# ✅ Gradio UI 功能
|
| 74 |
def predict_text(text, mode):
|
| 75 |
result = analyze_text(text=text, explain_mode=mode)
|