Spaces:
Sleeping
Sleeping
Upload 5 files
Browse files- DockerFile +30 -0
- app.py +45 -0
- models/logistic_regression_model.pkl +3 -0
- models/tfidf_vectorizer.pkl +3 -0
- requirements.txt +6 -0
DockerFile
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use the official Python image from the Docker Hub
|
| 2 |
+
FROM python:3.9
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /code
|
| 6 |
+
|
| 7 |
+
# Copy the requirements.txt file into the container
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
# Install the dependencies
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 12 |
+
|
| 13 |
+
# Set up a new user named "user"
|
| 14 |
+
RUN useradd user
|
| 15 |
+
|
| 16 |
+
# Switch to the "user" user
|
| 17 |
+
USER user
|
| 18 |
+
|
| 19 |
+
# Set home to the user's home directory
|
| 20 |
+
ENV HOME=/home/user \
|
| 21 |
+
PATH=/home/user/.local/bin:$PATH
|
| 22 |
+
|
| 23 |
+
# Set working directory to the user's home directory
|
| 24 |
+
WORKDIR $HOME/app
|
| 25 |
+
|
| 26 |
+
# Copy the rest of the application into the container at $HOME/app
|
| 27 |
+
COPY --chown=user . $Home/app
|
| 28 |
+
|
| 29 |
+
# Set the command to run the FastAPI app with Uvicorn at 7860
|
| 30 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
import string
|
| 3 |
+
import joblib
|
| 4 |
+
from sklearn.feature_extraction.text import TfidfVectorizer
|
| 5 |
+
from fastapi import FastAPI, Request, Query
|
| 6 |
+
from pydantic import BaseModel
|
| 7 |
+
|
| 8 |
+
# Load the logistic regression model
|
| 9 |
+
model = joblib.load('models/logistic_regression_model.pkl')
|
| 10 |
+
|
| 11 |
+
# Load the TfidfVectorizer
|
| 12 |
+
vectorization = joblib.load('models/tfidf_vectorizer.pkl')
|
| 13 |
+
|
| 14 |
+
# Define the wordopt function
|
| 15 |
+
def wordopt(text):
|
| 16 |
+
text = text.lower()
|
| 17 |
+
text = re.sub('\[.*?\]', '', text)
|
| 18 |
+
text = re.sub("\\W", " ", text)
|
| 19 |
+
text = re.sub('https?://\S+|www\.\S+', '', text)
|
| 20 |
+
text = re.sub('<.*?>+', '', text)
|
| 21 |
+
text = re.sub('[%s]' % re.escape(string.punctuation), '', text)
|
| 22 |
+
text = re.sub('\n', '', text)
|
| 23 |
+
text = re.sub('\w*\d\w*', '', text)
|
| 24 |
+
return text
|
| 25 |
+
|
| 26 |
+
# Define the prediction function
|
| 27 |
+
def predict_news(text):
|
| 28 |
+
text = wordopt(text)
|
| 29 |
+
text_vector = vectorization.transform([text])
|
| 30 |
+
prediction = model.predict(text_vector)
|
| 31 |
+
return "Fake" if prediction[0] == 1 else "Real"
|
| 32 |
+
|
| 33 |
+
# FastAPI app
|
| 34 |
+
app = FastAPI()
|
| 35 |
+
|
| 36 |
+
@app.get("/")
|
| 37 |
+
def home():
|
| 38 |
+
return {"message": "Hello World"}
|
| 39 |
+
|
| 40 |
+
@app.get("/predict")
|
| 41 |
+
def predict(text: str):
|
| 42 |
+
result = predict_news(text)
|
| 43 |
+
return {"result": result}
|
| 44 |
+
|
| 45 |
+
# def predict(text: str = Query(..., description="Text to classify as fake or real news")):
|
models/logistic_regression_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:34898207a65a3c8ecd391c24297d6d5aa76def1814db94dcdf5b1e90996c8833
|
| 3 |
+
size 759023
|
models/tfidf_vectorizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:025e9ba8145dba27be38bb70954699b92a2eaef4af9d1fc122e16ae7dd6c9412
|
| 3 |
+
size 2892814
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
scikit-learn
|
| 4 |
+
joblib
|
| 5 |
+
transformers
|
| 6 |
+
datasets
|