File size: 2,392 Bytes
670a3e0
2becaf9
 
670a3e0
739ec50
670a3e0
 
2becaf9
 
 
 
 
670a3e0
 
 
 
 
2becaf9
 
670a3e0
2becaf9
 
670a3e0
2becaf9
670a3e0
2becaf9
670a3e0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2becaf9
670a3e0
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
59
60
61
from fastapi import FastAPI, File, UploadFile, Request, Form
from fastapi.responses import JSONResponse
from fastapi.middleware.cors import CORSMiddleware
import uvicorn
import numpy as np
from projects.DL_CatDog.DL_CatDog import preprocess_image, read_image, model_DL_CatDog
from projects.ML_StudentPerformance.ML_StudentPerformace import predict_student_performance, create_custom_data, form1

app = FastAPI()

# Add CORS middleware
app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],  # You can restrict this to specific origins if needed
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Health check route
@app.get("/api/working")
def home():
    return {"message": "FastAPI server is running on Hugging Face Spaces!"}

# # Prediction route for DL_CatDog
@app.post("/api/predict1")
async def predict_DL_CatDog(file: UploadFile = File(...)):
    try:
        image = read_image(file)
        preprocessed_image = preprocess_image(image)
        prediction = model_DL_CatDog.predict(preprocessed_image)
        predicted_class = "Dog" if np.round(prediction[0][0]) == 1 else "Cat"
        return JSONResponse(content={"ok": 1, "prediction": predicted_class})
    except Exception as e:
        return JSONResponse(content={"ok": -1, "message": f"Something went wrong! {str(e)}"}, status_code=500)

# New Prediction route for ML_StudentPerformance
@app.post("/api/predict2")
async def predict_student_performance_api(request: form1):
    print(request, end='\n\n\n\n')
    try:
        # Create the CustomData object
        custom_data = create_custom_data(
            gender= request.gender,
            ethnicity= request.ethnicity,
            parental_level_of_education= request.parental_level_of_education,
            lunch= request.lunch,
            test_preparation_course= request.test_preparation_course,
            reading_score= request.reading_score,
            writing_score= request.writing_score
        )
        # Perform the prediction
        result = predict_student_performance(custom_data)
        return JSONResponse(content={"ok": 1, "prediction": result})
    except Exception as e:
        return JSONResponse(content={"ok": -1, "message": f"Something went wrong! {str(e)}"}, status_code=500)

# Main function to run the FastAPI server
if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=7860)