MichaelNieto commited on
Commit
4af2c8a
·
verified ·
1 Parent(s): bf965fb

Upload folder using huggingface_hub

Browse files
Files changed (3) hide show
  1. Dockerfile +11 -14
  2. app.py +44 -47
  3. requirements.txt +2 -5
Dockerfile CHANGED
@@ -1,19 +1,16 @@
1
- # Use Python 3.9 as base image
2
- FROM python:3.9
3
 
4
- # Set working directory
5
  WORKDIR /app
6
 
7
- # Copy requirements.txt and install dependencies
8
- COPY requirements.txt .
9
- RUN pip install --no-cache-dir -r requirements.txt
10
 
11
- # Copy application code
12
- COPY app.py .
13
-
14
- # Expose port (matches port specified in app.py)
15
- EXPOSE 7860
16
-
17
- # Run the app using uvicorn
18
- CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
19
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
 
2
 
3
+ # Set the working directory inside the container
4
  WORKDIR /app
5
 
6
+ # Copy all files from the current directory to the container's working directory
7
+ COPY . .
 
8
 
9
+ # Install dependencies from the requirements file without using cache to reduce image size
10
+ RUN pip install --no-cache-dir --upgrade -r requirements.txt
 
 
 
 
 
 
11
 
12
+ # Define the command to start the application using Gunicorn with 4 worker processes
13
+ # - `-w 4`: Uses 4 worker processes for handling requests
14
+ # - `-b 0.0.0.0:7860`: Binds the server to port 7860 on all network interfaces
15
+ # - `app:superkart_api`: Runs the Flask app (assuming `app.py` contains the Flask instance named `superkart_api`)
16
+ CMD ["gunicorn", "-w", "4", "-b", "0.0.0.0:7860", "app:superkart_api"]
app.py CHANGED
@@ -1,47 +1,44 @@
1
- import os
2
- from fastapi import FastAPI, Request
3
- from fastapi.responses import JSONResponse
4
- from fastapi.middleware.cors import CORSMiddleware
5
- from huggingface_hub import hf_hub_download
6
- import joblib
7
- import pandas as pd
8
-
9
- app = FastAPI()
10
-
11
- # Add CORS middleware
12
- app.add_middleware(
13
- CORSMiddleware,
14
- allow_origins=["*"],
15
- allow_methods=["*"],
16
- allow_headers=["*"],
17
- )
18
-
19
- # Force Hugging Face to use a writable directory
20
- os.environ["HF_HUB_CACHE"] = "/tmp/hf_cache"
21
-
22
- # Create the cache directory if it doesn't exist
23
- cache_dir = "/tmp/hf_cache"
24
- os.makedirs(cache_dir, exist_ok=True)
25
-
26
- # Diagnostic print to confirm we can write to /tmp
27
- print("Writable directories in /tmp:")
28
- print(os.listdir("/tmp"))
29
-
30
- # Download the model from Hugging Face
31
- model_path = hf_hub_download(
32
- repo_id="MichaelNieto/super_model",
33
- filename="best_model.joblib",
34
- cache_dir=cache_dir
35
- )
36
-
37
- # Load the model
38
- model = joblib.load(model_path)
39
-
40
- # Define the prediction endpoint
41
- @app.post("/v1/predict")
42
- async def predict(request: Request):
43
- data = await request.json()
44
- input_df = pd.DataFrame([data])
45
- prediction = model.predict(input_df)[0]
46
- result = {"Sales": round(float(prediction), 2)}
47
- return JSONResponse(content=result)
 
1
+ # Import necessary libraries
2
+ import numpy as np
3
+ import joblib # For loading the serialized model
4
+ import pandas as pd # For data manipulation
5
+ from flask import Flask, request, jsonify # For creating the Flask API
6
+ import os # For creating directories if needed
7
+
8
+ # Ensure the model directory exists
9
+ os.makedirs("model", exist_ok=True)
10
+
11
+ # Initialize Flask app with a name
12
+ superkart_api = Flask("superkart_api") # Name of the app
13
+
14
+ # Load the trained churn prediction model
15
+ model = joblib.load("model/best_model.joblib") # Path to the serialized model
16
+
17
+ # Define a route for the home page
18
+ @superkart_api.get('/')
19
+ def home():
20
+ return "Welcome to Superkart Sales Prediction API!" # Welcome message
21
+
22
+ # Define an endpoint to predict churn for a single customer
23
+ @superkart_api.post('/v1/predict')
24
+ def predict_sales():
25
+ data = request.get_json()
26
+ sample = {
27
+ 'Product_Weight': data['Product_Weight'],
28
+ 'Product_Sugar_Content': data['Product_Sugar_Content'],
29
+ 'Product_Allocated_Area': data['Product_Allocated_Area'],
30
+ 'Product_MRP': data['Product_MRP'],
31
+ 'Store_Size': data['Store_Size'],
32
+ 'Store_Location_City_Type': data['Store_Location_City_Type'],
33
+ 'Store_Type': data['Store_Type'],
34
+ 'Product_Id_char': data['Product_Id_char'],
35
+ 'Store_Age_Years': data['Store_Age_Years'],
36
+ 'Product_Type_Category': data['Product_Type_Category']
37
+ }
38
+ input_data = pd.DataFrame([sample])
39
+ prediction = model.predict(input_data).tolist()[0]
40
+ return jsonify({'Sales': prediction})
41
+
42
+ # Run the Flask app in debug mode
43
+ if __name__ == '__main__':
44
+ superkart_api.run(debug=True)
 
 
 
requirements.txt CHANGED
@@ -1,15 +1,12 @@
1
  pandas==2.2.2
2
- numpy==2.0.0
3
  scikit-learn==1.6.1
4
  seaborn==0.13.2
5
  joblib==1.4.2
6
  xgboost==2.1.4
7
- werkzeug==2.2.2
8
  flask==2.2.2
9
  gunicorn==20.1.0
10
  requests==2.32.3
11
  uvicorn[standard]
12
  streamlit==1.43.2
13
- fastapi==0.109.0
14
- uvicorn==0.29.0
15
- huggingface_hub
 
1
  pandas==2.2.2
2
+ numpy==2.0.2
3
  scikit-learn==1.6.1
4
  seaborn==0.13.2
5
  joblib==1.4.2
6
  xgboost==2.1.4
7
+ Werkzeug==2.2.2
8
  flask==2.2.2
9
  gunicorn==20.1.0
10
  requests==2.32.3
11
  uvicorn[standard]
12
  streamlit==1.43.2