|
from fastapi import FastAPI, UploadFile, HTTPException, Body |
|
from fastapi.responses import JSONResponse |
|
from fastapi.middleware.cors import CORSMiddleware |
|
from transformers import pipeline |
|
from PIL import Image |
|
import base64 |
|
from io import BytesIO |
|
|
|
app = FastAPI() |
|
|
|
|
|
classifier = pipeline(model="Diginsa/Plant-Disease-Detection-Project") |
|
|
|
|
|
origins = [""] |
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=origins, |
|
allow_credentials=True, |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
|
|
@app.post("/classify") |
|
async def classify_image(encoded_image: str= Body(..., embed=True)): |
|
try: |
|
|
|
decoded_image = base64.b64decode(encoded_image) |
|
|
|
|
|
image = Image.open(BytesIO(decoded_image)) |
|
|
|
|
|
result = classifier(images=image) |
|
|
|
|
|
return JSONResponse(content=result, status_code=200) |
|
|
|
except Exception as e: |
|
raise HTTPException(status_code=500, detail=str(e)) |