Olivier-52 commited on
Commit
91f0a33
·
1 Parent(s): fa16c9f

init repo

Browse files
Files changed (3) hide show
  1. Dockerfile +16 -0
  2. app.py +87 -0
  3. requirements.txt +13 -0
Dockerfile ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.10
2
+
3
+ WORKDIR /home/app
4
+
5
+ RUN apt-get update -y
6
+ RUN apt-get install nano unzip -y
7
+ RUN apt install curl -y
8
+
9
+ RUN curl -fsSL https://get.deta.dev/cli.sh | sh
10
+
11
+ COPY requirements.txt /dependencies/requirements.txt
12
+ RUN pip install -r /dependencies/requirements.txt
13
+
14
+ COPY . /home/app
15
+
16
+ CMD gunicorn app:app --bind 0.0.0.0:$PORT --worker-class uvicorn.workers.UvicornWorker
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import uvicorn
3
+ import pandas as pd
4
+ from pydantic import BaseModel
5
+ from fastapi import FastAPI, File, UploadFile
6
+ import mlflow
7
+ import os
8
+ from dotenv import load_dotenv
9
+
10
+ description = """
11
+
12
+ # Climate Fake News Detector(https://github.com/Olivier-52/Fake_news_detector.git)
13
+
14
+ This API allows you to use a Machine Learning model to detect fake news related to climate change.
15
+
16
+ ## Machine-Learning
17
+
18
+ Where you can:
19
+ * `/predict` : prediction for a single value
20
+
21
+ Check out documentation for more information on each endpoint.
22
+ """
23
+
24
+ tags_metadata = [
25
+ {
26
+ "name": "Predictions",
27
+ "description": "Endpoints that uses our Machine Learning model",
28
+ },
29
+ ]
30
+
31
+ load_dotenv()
32
+ MLFLOW_TRACKING_URI = os.environ["MLFLOW_TRACKING_APP_URI"]
33
+ AWS_ACCESS_KEY_ID = os.environ["AWS_ACCESS_KEY_ID"]
34
+ AWS_SECRET_ACCESS_KEY = os.environ["AWS_SECRET_ACCESS_KEY"]
35
+ MODEL_URI = "models:/climate-fake-news-detector-model-XGBoost-v1@production"
36
+ mlflow.set_tracking_uri(MLFLOW_TRACKING_URI)
37
+ model_uri = MODEL_URI
38
+ model = mlflow.sklearn.load_model(model_uri)
39
+
40
+ app = FastAPI(
41
+ title="API for Climate Fake News Detector",
42
+ description=description,
43
+ version="1.0",
44
+ contact={
45
+ "name": "Olivier",
46
+ "url": "https://github.com/Olivier-52/Fake_news_detector",
47
+ },
48
+ openapi_tags=tags_metadata,)
49
+
50
+ @app.get("/")
51
+ def index():
52
+ """Return a message to the user.
53
+
54
+ This endpoint does not take any parameters and returns a message
55
+ to the user. It is used to test the API.
56
+
57
+ Returns:
58
+ str: A message to the user.
59
+ """
60
+ return "Hello world! Go to /docs to try the API."
61
+
62
+
63
+ class PredictionFeatures(BaseModel):
64
+ text: str
65
+
66
+ @app.post("/predict", tags=["Predictions"])
67
+ def predict(features: PredictionFeatures):
68
+ """Predict Climate Fake News.
69
+
70
+ This endpoint takes a text as input and returns the predicted class : fake, real, or biased.
71
+
72
+ Args:
73
+ features (PredictionFeatures): A PredictionFeatures object
74
+ containing the text to analyze.
75
+
76
+ Returns:
77
+ dict: A dictionary containing the predicted class.
78
+ """
79
+ df = pd.DataFrame({
80
+ "text": [features.text],
81
+ })
82
+
83
+ prediction = model.predict(df)[0]
84
+ return {"prediction": float(prediction)}
85
+
86
+ if __name__ == "__main__":
87
+ uvicorn.run(app, host="localhost", port=8000)
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ mlflow==2.21.3
2
+ scikit-learn==1.4.2
3
+ requests>=2.31.0,<3
4
+ fastapi
5
+ uvicorn[standard]
6
+ pydantic
7
+ typing
8
+ pandas
9
+ gunicorn
10
+ openpyxl
11
+ boto3
12
+ python-multipart
13
+ dotenv