add app requirements and dockerfile
Browse files- Dockerfile +17 -0
- app.py +36 -0
- requirements.txt +4 -0
Dockerfile
ADDED
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 /app
|
6 |
+
|
7 |
+
# Copy the requirements file into the container
|
8 |
+
COPY requirements.txt .
|
9 |
+
|
10 |
+
# Install the dependencies
|
11 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
12 |
+
|
13 |
+
# Copy the rest of the application code into the container
|
14 |
+
COPY . .
|
15 |
+
|
16 |
+
# Command to run the application
|
17 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from pydantic import BaseModel
|
3 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
4 |
+
import torch
|
5 |
+
|
6 |
+
# Inisialisasi model dan tokenizer
|
7 |
+
model_name = "w11wo/indonesian-roberta-base-sentiment-classifier"
|
8 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
9 |
+
model = AutoModelForSequenceClassification.from_pretrained(model_name)
|
10 |
+
|
11 |
+
# Inisialisasi FastAPI
|
12 |
+
app = FastAPI()
|
13 |
+
|
14 |
+
# Model request body
|
15 |
+
class TextInput(BaseModel):
|
16 |
+
text: str
|
17 |
+
|
18 |
+
# Fungsi untuk analisis sentimen
|
19 |
+
def predict_sentiment(text):
|
20 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
21 |
+
outputs = model(**inputs)
|
22 |
+
scores = outputs.logits[0].detach().numpy()
|
23 |
+
predictions = torch.nn.functional.softmax(torch.tensor(scores), dim=0)
|
24 |
+
sentiment = torch.argmax(predictions).item()
|
25 |
+
return sentiment, predictions[sentiment].item()
|
26 |
+
|
27 |
+
# Endpoint untuk analisis sentimen
|
28 |
+
@app.post("/predict")
|
29 |
+
async def predict(input: TextInput):
|
30 |
+
sentiment, confidence = predict_sentiment(input.text)
|
31 |
+
return {"sentiment": sentiment, "confidence": confidence}
|
32 |
+
|
33 |
+
# Endpoint root
|
34 |
+
@app.get("/")
|
35 |
+
async def read_root():
|
36 |
+
return {"message": "Sentiment Analysis API"}
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
fastapi
|
2 |
+
uvicorn
|
3 |
+
transformers
|
4 |
+
torch
|