Spaces:
Runtime error
Runtime error
File size: 1,737 Bytes
951a669 0e0c8cb |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import JSONResponse
# from tensorflow.keras.models import load_model
# from tensorflow.keras.preprocessing import image
import numpy as np
from fastapi.middleware.cors import CORSMiddleware
import io
import os
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Add your frontend URL
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Load your trained model
# model = load_model('flower_species_model.h5')
# def preprocess_image(img_file):
# img = image.load_img(img_file, target_size=(64, 64))
# img_array = image.img_to_array(img)
# img_array = np.expand_dims(img_array, axis=0)
# img_array /= 255.0
# return img_array
@app.post("/predict")
async def predict(files: list[UploadFile] = File(...)):
if not files:
return JSONResponse(content={"error": "No files uploaded"}, status_code=400)
predictions = []
for file in files:
contents = await file.read()
img = io.BytesIO(contents)
# preprocessed_img = preprocess_image(img)
# prediction = model.predict(preprocessed_img)
# predictions.append(prediction[0][0])
print("File uploaded")
threshold = 0.5
# predicted_classes = [1 if p > threshold else 0 for p in predictions]
# percentage_class_1 = (predicted_classes.count(1) / len(predicted_classes)) * 100
# return {"percentage_class_1": round(percentage_class_1, 2)}
return {"message": "Files uploaded", "percentage": 100}
@app.get("/")
async def main():
return {"message": "Hello World"}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8080) |