Spaces:
Runtime error
Runtime error
added classification route for plant-disease-detection
Browse files
main.py
CHANGED
|
@@ -1,7 +1,10 @@
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile, Request, Form
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
|
|
|
| 4 |
import uvicorn
|
|
|
|
|
|
|
| 5 |
import numpy as np
|
| 6 |
from projects.DL_CatDog.DL_CatDog import preprocess_image, read_image, model_DL_CatDog
|
| 7 |
from projects.ML_StudentPerformance.ML_StudentPerformace import predict_student_performance, create_custom_data, form1
|
|
@@ -23,7 +26,7 @@ app.add_middleware(
|
|
| 23 |
def home():
|
| 24 |
return {"message": "FastAPI server is running on Hugging Face Spaces!"}
|
| 25 |
|
| 26 |
-
#
|
| 27 |
@app.post("/api/predict1")
|
| 28 |
async def predict_DL_CatDog(file: UploadFile = File(...)):
|
| 29 |
try:
|
|
@@ -35,6 +38,22 @@ async def predict_DL_CatDog(file: UploadFile = File(...)):
|
|
| 35 |
except Exception as e:
|
| 36 |
return JSONResponse(content={"ok": -1, "message": f"Something went wrong! {str(e)}"}, status_code=500)
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# Prediction route for ML_StudentPerformance
|
| 39 |
@app.post("/api/predict2")
|
| 40 |
async def predict_student_performance_api(request: form1):
|
|
|
|
| 1 |
from fastapi import FastAPI, File, UploadFile, Request, Form
|
| 2 |
from fastapi.responses import JSONResponse
|
| 3 |
from fastapi.middleware.cors import CORSMiddleware
|
| 4 |
+
from transformers import pipeline
|
| 5 |
import uvicorn
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import io
|
| 8 |
import numpy as np
|
| 9 |
from projects.DL_CatDog.DL_CatDog import preprocess_image, read_image, model_DL_CatDog
|
| 10 |
from projects.ML_StudentPerformance.ML_StudentPerformace import predict_student_performance, create_custom_data, form1
|
|
|
|
| 26 |
def home():
|
| 27 |
return {"message": "FastAPI server is running on Hugging Face Spaces!"}
|
| 28 |
|
| 29 |
+
# Prediction route for DL_CatDog
|
| 30 |
@app.post("/api/predict1")
|
| 31 |
async def predict_DL_CatDog(file: UploadFile = File(...)):
|
| 32 |
try:
|
|
|
|
| 38 |
except Exception as e:
|
| 39 |
return JSONResponse(content={"ok": -1, "message": f"Something went wrong! {str(e)}"}, status_code=500)
|
| 40 |
|
| 41 |
+
# Classification route for DL_PlantDisease
|
| 42 |
+
pipe = pipeline("image-classification", model="wambugu71/crop_leaf_diseases_vit")
|
| 43 |
+
@app.post("/api/classify")
|
| 44 |
+
async def classify_image(file: UploadFile = File(...)):
|
| 45 |
+
try:
|
| 46 |
+
# Read the uploaded image file
|
| 47 |
+
image = Image.open(io.BytesIO(await file.read()))
|
| 48 |
+
|
| 49 |
+
# Run the image through the Hugging Face model
|
| 50 |
+
predictions = pipe(image)
|
| 51 |
+
|
| 52 |
+
return JSONResponse(content={"ok": 1, "predictions": predictions})
|
| 53 |
+
except Exception as e:
|
| 54 |
+
return JSONResponse(content={"ok": -1, "message": f"Something went wrong! {str(e)}"}, status_code=500)
|
| 55 |
+
|
| 56 |
+
|
| 57 |
# Prediction route for ML_StudentPerformance
|
| 58 |
@app.post("/api/predict2")
|
| 59 |
async def predict_student_performance_api(request: form1):
|