Spaces:
Build error
Build error
Upload 3 files
Browse files- Dockerfile +10 -0
- main.py +52 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10
|
| 2 |
+
|
| 3 |
+
WORKDIR /code
|
| 4 |
+
|
| 5 |
+
COPY requirements.txt .
|
| 6 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 7 |
+
|
| 8 |
+
COPY ./app /code/app
|
| 9 |
+
|
| 10 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]
|
main.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File, HTTPException
|
| 2 |
+
import pickle
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from pydantic import BaseModel
|
| 5 |
+
from typing import List
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
app = FastAPI()
|
| 9 |
+
MODEL_PATH = "tfidf_models.pkl"
|
| 10 |
+
|
| 11 |
+
class TrainRequest(BaseModel):
|
| 12 |
+
texts: List[str]
|
| 13 |
+
labels: List[List[int]]
|
| 14 |
+
|
| 15 |
+
class PredictRequest(BaseModel):
|
| 16 |
+
texts: List[str]
|
| 17 |
+
|
| 18 |
+
@app.post("/train")
|
| 19 |
+
def train_model(request: TrainRequest):
|
| 20 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 21 |
+
from sklearn.multioutput import MultiOutputClassifier
|
| 22 |
+
from sklearn.linear_model import LogisticRegression
|
| 23 |
+
|
| 24 |
+
if len(request.texts) != len(request.labels):
|
| 25 |
+
raise HTTPException(status_code=400, detail="Texts and labels length mismatch")
|
| 26 |
+
|
| 27 |
+
X = request.texts
|
| 28 |
+
y = pd.DataFrame(request.labels)
|
| 29 |
+
|
| 30 |
+
vectorizer = TfidfVectorizer()
|
| 31 |
+
X_tfidf = vectorizer.fit_transform(X)
|
| 32 |
+
|
| 33 |
+
classifier = MultiOutputClassifier(LogisticRegression(max_iter=1000))
|
| 34 |
+
classifier.fit(X_tfidf, y)
|
| 35 |
+
|
| 36 |
+
with open(MODEL_PATH, "wb") as f:
|
| 37 |
+
pickle.dump((vectorizer, classifier), f)
|
| 38 |
+
|
| 39 |
+
return {"message": "Model trained and saved successfully."}
|
| 40 |
+
|
| 41 |
+
@app.post("/predict")
|
| 42 |
+
def predict(request: PredictRequest):
|
| 43 |
+
if not os.path.exists(MODEL_PATH):
|
| 44 |
+
raise HTTPException(status_code=404, detail="Model not found. Train the model first.")
|
| 45 |
+
|
| 46 |
+
with open(MODEL_PATH, "rb") as f:
|
| 47 |
+
vectorizer, classifier = pickle.load(f)
|
| 48 |
+
|
| 49 |
+
X_tfidf = vectorizer.transform(request.texts)
|
| 50 |
+
predictions = classifier.predict(X_tfidf)
|
| 51 |
+
|
| 52 |
+
return {"predictions": predictions.tolist()}
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
scikit-learn
|
| 4 |
+
pandas
|
| 5 |
+
python-multipart
|