Sushan commited on
Commit
e781781
·
1 Parent(s): 00b4833

all required files done

Browse files
Files changed (4) hide show
  1. Dockerfile +27 -0
  2. app.py +54 -0
  3. best_model.pkl +3 -0
  4. requirements.txt +4 -0
Dockerfile ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Dockerfile
2
+
3
+ # Use the official Python image from Docker Hub
4
+ FROM python:3.9
5
+
6
+ # Create a user and set up the environment
7
+ RUN useradd -m -u 1000 user
8
+ USER user
9
+ ENV PATH="/home/user/.local/bin:$PATH"
10
+
11
+ # Set the working directory inside the container
12
+ WORKDIR /app
13
+
14
+ # Copy the requirements file to the working directory
15
+ COPY --chown=user ./requirements.txt requirements.txt
16
+
17
+ # Install the Python dependencies
18
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
19
+
20
+ # Copy the entire project into the container's working directory
21
+ COPY --chown=user . /app
22
+
23
+ # Expose port 7860
24
+ EXPOSE 7860
25
+
26
+ # Command to run the FastAPI app using Uvicorn
27
+ CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
app.py ADDED
@@ -0,0 +1,54 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # app.py
2
+ from fastapi import FastAPI, Request
3
+ from pydantic import BaseModel
4
+ import pickle
5
+ import numpy as np
6
+ from fastapi.middleware.cors import CORSMiddleware
7
+
8
+ app = FastAPI()
9
+
10
+ # Enable CORS for all origins, methods, and headers to avoid CORS issues when making requests from React, Axios, etc.
11
+ app.add_middleware(
12
+ CORSMiddleware,
13
+ allow_origins=["*"], # Allows all origins
14
+ allow_credentials=True,
15
+ allow_methods=["*"], # Allows all methods
16
+ allow_headers=["*"], # Allows all headers
17
+ )
18
+
19
+ # Load the trained model
20
+ with open('best_model.pkl', 'rb') as f:
21
+ model = pickle.load(f)
22
+
23
+ # Input schema for FastAPI
24
+ class AlgaeInput(BaseModel):
25
+ Light: float
26
+ Nitrate: float
27
+ Iron: float
28
+ Phosphate: float
29
+ Temperature: float
30
+ pH: float
31
+ CO2: float
32
+
33
+ # Root endpoint to check if the API is running
34
+ @app.get("/")
35
+ def greet_json():
36
+ return {"Hello": "World!, the prediction is at /predict"}
37
+
38
+ # Prediction endpoint to accept input data and return the predicted algae quantity
39
+ @app.post("/predict")
40
+ async def predict_algae(input_data: AlgaeInput):
41
+ try:
42
+ # Convert input data to the correct format
43
+ input_array = np.array([[input_data.Light, input_data.Nitrate, input_data.Iron,
44
+ input_data.Phosphate, input_data.Temperature,
45
+ input_data.pH, input_data.CO2]])
46
+
47
+ # Perform prediction
48
+ prediction = model.predict(input_array)
49
+
50
+ # Return the prediction as a JSON response
51
+ return {"predicted_population": prediction[0]}
52
+ except Exception as e:
53
+ # Return an error message if prediction fails
54
+ return {"error": str(e)}
best_model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:03052d9f978b0f379eded6f7a9b7312fefc6cf91c4c88837857ee83165777d22
3
+ size 136352
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ numpy
4
+ scikit-learn