Upload 3 files
Browse files- dockerfile +20 -0
- main.py +28 -0
- requirements.txt +6 -0
dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Use an official Python runtime as a parent image
|
2 |
+
FROM python:3.10-slim
|
3 |
+
|
4 |
+
# Set the working directory in the container
|
5 |
+
WORKDIR /sentiment
|
6 |
+
|
7 |
+
# Copy the requirements.txt file into the root
|
8 |
+
COPY requirements.txt .
|
9 |
+
|
10 |
+
# Copy the current directory contents into the container at /app as well
|
11 |
+
COPY ./app ./app
|
12 |
+
|
13 |
+
# Install any needed packages specified in requirements.txt
|
14 |
+
RUN pip install -r requirements.txt
|
15 |
+
|
16 |
+
# Make port 8000 available to the world outside this container
|
17 |
+
EXPOSE 8000
|
18 |
+
|
19 |
+
# Run main.py when the container launches, as it is contained under the app folder, we define app.main
|
20 |
+
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]
|
main.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import pipeline
|
4 |
+
|
5 |
+
# You can check any other model in the Hugging Face Hub. In my case I chose this one to classify text by positive and negative sentiment.
|
6 |
+
pipe = pipeline(model="distilbert/distilbert-base-uncased-finetuned-sst-2-english")
|
7 |
+
|
8 |
+
# We define the app
|
9 |
+
app = FastAPI()
|
10 |
+
|
11 |
+
# We define that we expect our input to be a string
|
12 |
+
class RequestModel(BaseModel):
|
13 |
+
input: str
|
14 |
+
|
15 |
+
# Now we define that we accept post requests
|
16 |
+
# -> In APIs, requests are made to ask the API to perform a certain task — in this case to analyze a piece of text.
|
17 |
+
@app.post("/sentiment")
|
18 |
+
def get_response(request: RequestModel):
|
19 |
+
# We get the input prompt
|
20 |
+
prompt = request.input
|
21 |
+
|
22 |
+
# We use the hf model to classify the prompt
|
23 |
+
response = pipe(prompt)
|
24 |
+
|
25 |
+
# We get both the label and the score from the input
|
26 |
+
label = response[0]["label"]
|
27 |
+
score = response[0]["score"]
|
28 |
+
return f"The '{prompt}' input is {label} with a score of {score}"
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
pydantic
|
3 |
+
transformers
|
4 |
+
uvicorn
|
5 |
+
requests
|
6 |
+
torch
|