Skip to content

Commit 2e55995

Browse files
Create prediction.py
1 parent 010b006 commit 2e55995

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

app/routers/prediction.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import base64
2+
import tempfile
3+
from pathlib import Path
4+
from fastapi import APIRouter, HTTPException
5+
from app.schemas.prediction import PredictionRequest, PredictionResponse, DiseaseType
6+
from app.services.langchain_agents import DiseaseAgent
7+
8+
router = APIRouter()
9+
10+
@router.post("/", response_model=PredictionResponse)
11+
async def predict_disease(request: PredictionRequest) -> PredictionResponse:
12+
"""
13+
Predict disease from an uploaded image.
14+
15+
Parameters:
16+
- disease_type: Type of disease to predict
17+
- image_base64: Base64 encoded image data
18+
19+
Returns:
20+
- Prediction results including confidence score and analysis
21+
"""
22+
try:
23+
# Decode base64 image and save temporarily
24+
image_data = base64.b64decode(request.image_base64)
25+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_file:
26+
temp_file.write(image_data)
27+
temp_path = temp_file.name
28+
29+
# Initialize disease agent and get prediction
30+
agent = DiseaseAgent(img_path=temp_path, task=request.disease_type.value)
31+
result = agent.response()
32+
33+
# Clean up temporary file
34+
Path(temp_path).unlink()
35+
36+
if "error" in result:
37+
return PredictionResponse(
38+
disease_type=request.disease_type,
39+
prediction="",
40+
confidence=0.0,
41+
analysis="",
42+
status="error",
43+
error_message=str(result["error"])
44+
)
45+
46+
return PredictionResponse(
47+
disease_type=request.disease_type,
48+
prediction=result["prediction"],
49+
confidence=result["confidence"],
50+
analysis=result["analysis"],
51+
status="success"
52+
)
53+
54+
except Exception as e:
55+
raise HTTPException(
56+
status_code=500,
57+
detail=f"Error processing prediction: {str(e)}"
58+
)

0 commit comments

Comments
 (0)