Spaces:
Runtime error
Runtime error
Update main.py
Browse files
main.py
CHANGED
@@ -1,63 +1,60 @@
|
|
1 |
-
from fastapi import FastAPI, File, UploadFile
|
2 |
from fastapi.responses import JSONResponse
|
3 |
from fastapi.middleware.cors import CORSMiddleware
|
4 |
-
import
|
5 |
-
import
|
6 |
-
from
|
7 |
-
from
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
# Add CORS middleware
|
12 |
app.add_middleware(
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
)
|
19 |
|
20 |
-
|
21 |
-
# Load your pre-trained model
|
22 |
-
MODEL_PATH = "./models/model_catdog1.h5"
|
23 |
-
model = tf.keras.models.load_model(MODEL_PATH)
|
24 |
-
|
25 |
-
@app.get("/")
|
26 |
-
def home():
|
27 |
-
return {"message": "FastAPI server is running on Hugging Face Spaces!"}
|
28 |
-
|
29 |
@app.get("/api/working")
|
30 |
def home():
|
31 |
-
|
32 |
-
|
33 |
-
# Helper function to read and convert the uploaded image
|
34 |
-
def read_image(file: UploadFile) -> Image.Image:
|
35 |
-
image = Image.open(BytesIO(file.file.read())).convert('RGB')
|
36 |
-
return image
|
37 |
|
38 |
-
#
|
39 |
-
def preprocess_image(image: Image.Image):
|
40 |
-
image = image.resize((128, 128)) # Adjust to the size expected by your model
|
41 |
-
image = np.array(image) / 255.0 # Normalize the image
|
42 |
-
image = np.expand_dims(image, axis=0) # Add batch dimension
|
43 |
-
return image
|
44 |
-
|
45 |
-
# Route for classifying image
|
46 |
@app.post("/api/predict1")
|
47 |
-
async def
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
if __name__ == "__main__":
|
62 |
-
|
63 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
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 pandas as pd
|
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
|
8 |
|
9 |
app = FastAPI()
|
10 |
|
11 |
# Add CORS middleware
|
12 |
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_origins=["*"], # You can restrict this to specific origins if needed
|
15 |
+
allow_credentials=True,
|
16 |
+
allow_methods=["*"],
|
17 |
+
allow_headers=["*"],
|
18 |
)
|
19 |
|
20 |
+
# Health check route
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
@app.get("/api/working")
|
22 |
def home():
|
23 |
+
return {"message": "FastAPI server is running on Hugging Face Spaces!"}
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
# # Prediction route for DL_CatDog
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
@app.post("/api/predict1")
|
27 |
+
async def predict_DL_CatDog(file: UploadFile = File(...)):
|
28 |
+
try:
|
29 |
+
image = read_image(file)
|
30 |
+
preprocessed_image = preprocess_image(image)
|
31 |
+
prediction = model_DL_CatDog.predict(preprocessed_image)
|
32 |
+
predicted_class = "Dog" if np.round(prediction[0][0]) == 1 else "Cat"
|
33 |
+
return JSONResponse(content={"ok": 1, "prediction": predicted_class})
|
34 |
+
except Exception as e:
|
35 |
+
return JSONResponse(content={"ok": -1, "message": f"Something went wrong! {str(e)}"}, status_code=500)
|
36 |
+
|
37 |
+
# New Prediction route for ML_StudentPerformance
|
38 |
+
@app.post("/api/predict2")
|
39 |
+
async def predict_student_performance_api(request: form1):
|
40 |
+
print(request, end='\n\n\n\n')
|
41 |
+
try:
|
42 |
+
# Create the CustomData object
|
43 |
+
custom_data = create_custom_data(
|
44 |
+
gender= request.gender,
|
45 |
+
ethnicity= request.ethnicity,
|
46 |
+
parental_level_of_education= request.parental_level_of_education,
|
47 |
+
lunch= request.lunch,
|
48 |
+
test_preparation_course= request.test_preparation_course,
|
49 |
+
reading_score= request.reading_score,
|
50 |
+
writing_score= request.writing_score
|
51 |
+
)
|
52 |
+
# Perform the prediction
|
53 |
+
result = predict_student_performance(custom_data)
|
54 |
+
return JSONResponse(content={"ok": 1, "prediction": result})
|
55 |
+
except Exception as e:
|
56 |
+
return JSONResponse(content={"ok": -1, "message": f"Something went wrong! {str(e)}"}, status_code=500)
|
57 |
+
|
58 |
+
# Main function to run the FastAPI server
|
59 |
if __name__ == "__main__":
|
60 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|