Spaces:
Sleeping
Sleeping
Maicol2001
commited on
Commit
•
38df1da
1
Parent(s):
8940078
Upload 3 files
Browse files- Dockerfile +16 -0
- app.py +45 -0
- requirements.txt +6 -0
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Usa una imagen base de Python
|
2 |
+
FROM python:3.9
|
3 |
+
# Establece el directorio de trabajo
|
4 |
+
WORKDIR /code
|
5 |
+
|
6 |
+
# Copia los archivos necesarios al contenedor
|
7 |
+
COPY ./requirements.txt /code/requirements.txt
|
8 |
+
RUN pip install --no-cache-dir -r /code/requirements.txt
|
9 |
+
RUN pip install fastapi uvicorn
|
10 |
+
|
11 |
+
COPY . .
|
12 |
+
|
13 |
+
RUN chmod -R 777 /code
|
14 |
+
|
15 |
+
# Comando para ejecutar la aplicación
|
16 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
np.random.seed(0)
|
3 |
+
import pickle
|
4 |
+
from sklearn.compose import ColumnTransformer
|
5 |
+
from sklearn.datasets import fetch_openml
|
6 |
+
from sklearn.pipeline import Pipeline
|
7 |
+
from sklearn.impute import SimpleImputer
|
8 |
+
from sklearn.preprocessing import StandardScaler, OneHotEncoder
|
9 |
+
from sklearn.linear_model import LogisticRegression
|
10 |
+
from sklearn.model_selection import train_test_split
|
11 |
+
|
12 |
+
from sklearn import tree
|
13 |
+
|
14 |
+
from fastapi import FastAPI, HTTPException
|
15 |
+
from fastapi.responses import HTMLResponse
|
16 |
+
from pydantic import BaseModel
|
17 |
+
from typing import List
|
18 |
+
|
19 |
+
class InputData(BaseModel):
|
20 |
+
data: List[float]
|
21 |
+
|
22 |
+
# Inicializar la aplicación FastAPI
|
23 |
+
app = FastAPI()
|
24 |
+
|
25 |
+
def build_model():
|
26 |
+
with open('miarbol.pkl', 'rb') as fid:
|
27 |
+
miarbol = pickle.load(fid)
|
28 |
+
return miarbol
|
29 |
+
|
30 |
+
miarbol = build_model()
|
31 |
+
|
32 |
+
# Ruta de predicción
|
33 |
+
@app.post("/predict/")
|
34 |
+
async def predict(data: InputData):
|
35 |
+
print(f"Data: {data}")
|
36 |
+
global miarbol
|
37 |
+
try:
|
38 |
+
# Convertir la lista de entrada a un array de NumPy para la predicción
|
39 |
+
input_data = np.array(data.data).reshape(
|
40 |
+
1, -1
|
41 |
+
) # Asumiendo que la entrada debe ser de forma (1, num_features)
|
42 |
+
prediction = miarbol.predict(input_data).round()
|
43 |
+
return {"prediction": prediction.tolist()}
|
44 |
+
except Exception as e:
|
45 |
+
raise HTTPException(status_code=500, detail=str(e))
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
numpy
|
3 |
+
setuptools
|
4 |
+
scikit-learn
|
5 |
+
pydantic
|
6 |
+
uvicorn
|