Naman Omar commited on
Commit
5a2156d
·
1 Parent(s): c5a57a2

Add application file

Browse files
Files changed (5) hide show
  1. Dockerfile +12 -0
  2. app/main.py +23 -0
  3. app/model.pkl +3 -0
  4. app/requirements.txt +4 -0
  5. app/train.py +21 -0
Dockerfile ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11-slim
2
+
3
+ WORKDIR /code
4
+
5
+ COPY ./app /code/app
6
+
7
+ RUN pip install --upgrade pip
8
+ RUN pip install -r app/requirements.txt
9
+
10
+ EXPOSE 7860
11
+
12
+ CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
app/main.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI
2
+ from pydantic import BaseModel
3
+ import pickle
4
+ import numpy as np
5
+
6
+ app = FastAPI()
7
+
8
+ # Load model at startup
9
+ with open("app/model.pkl", "rb") as f:
10
+ model = pickle.load(f)
11
+
12
+ # Input schema
13
+ class InputData(BaseModel):
14
+ features: list[float]
15
+
16
+ @app.get("/")
17
+ def home():
18
+ return {"message": "Logistic Regression API is up!"}
19
+
20
+ @app.post("/predict")
21
+ def predict(data: InputData):
22
+ prediction = model.predict([np.array(data.features)])
23
+ return {"prediction": int(prediction[0])}
app/model.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:45b08c2fb38af8d0d1a364923427ba12b8751ff44039eea2b4068dcf7f7579b0
3
+ size 953
app/requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ fastapi
2
+ uvicorn
3
+ scikit-learn
4
+ pandas
app/train.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pandas as pd
2
+ from sklearn.linear_model import LogisticRegression
3
+ from sklearn.datasets import load_iris
4
+ from sklearn.model_selection import train_test_split
5
+ import pickle
6
+
7
+ # Load data
8
+ data = load_iris()
9
+ X = pd.DataFrame(data.data, columns=data.feature_names)
10
+ y = data.target
11
+
12
+ # Train-test split
13
+ X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)
14
+
15
+ # Train model
16
+ model = LogisticRegression(max_iter=200)
17
+ model.fit(X_train, y_train)
18
+
19
+ # Save model
20
+ with open("app/model.pkl", "wb") as f:
21
+ pickle.dump(model, f)