Spaces:
Sleeping
Sleeping
Xinqi04 commited on
Commit ·
1ca6006
1
Parent(s): a536b64
Add application file
Browse files- Dockerfile +16 -0
- app.py +30 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
# Set working directory
|
| 4 |
+
WORKDIR /app
|
| 5 |
+
|
| 6 |
+
# Copy all files to the container
|
| 7 |
+
COPY . .
|
| 8 |
+
|
| 9 |
+
# Install dependencies
|
| 10 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 11 |
+
|
| 12 |
+
# Expose port
|
| 13 |
+
EXPOSE 7860
|
| 14 |
+
|
| 15 |
+
# Start the API
|
| 16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel, field_validator
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
app = FastAPI()
|
| 7 |
+
|
| 8 |
+
# Load model
|
| 9 |
+
model = joblib.load("model.pkl")
|
| 10 |
+
|
| 11 |
+
class PredictionInput(BaseModel):
|
| 12 |
+
data: list[int]
|
| 13 |
+
|
| 14 |
+
@field_validator("data")
|
| 15 |
+
def validate_length(cls, v):
|
| 16 |
+
if len(v) != 17:
|
| 17 |
+
raise ValueError("data must contain exactly 17 integers")
|
| 18 |
+
return v
|
| 19 |
+
|
| 20 |
+
@app.post("/predict")
|
| 21 |
+
def predict(input_data: PredictionInput):
|
| 22 |
+
X = np.array(input_data.data).reshape(1, -1)
|
| 23 |
+
prediction = model.predict(X)
|
| 24 |
+
return {"prediction": prediction.tolist()}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# Tambahkan baris ini jika kamu menjalankan app.py langsung!
|
| 28 |
+
if __name__ == "__main__":
|
| 29 |
+
import uvicorn
|
| 30 |
+
uvicorn.run("app:app", host="0.0.0.0", port=8000)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
joblib
|
| 4 |
+
scikit-learn
|
| 5 |
+
numpy
|