Spaces:
Sleeping
Sleeping
Initial commit
Browse files- Dockerfile +17 -0
- main.py +38 -0
- requirements.txt +5 -0
Dockerfile
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use a lightweight Python base image
|
| 2 |
+
FROM python:3.10
|
| 3 |
+
|
| 4 |
+
# Set the working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Copy all files from the local directory into the container
|
| 8 |
+
COPY . /app
|
| 9 |
+
|
| 10 |
+
# Install dependencies
|
| 11 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 12 |
+
|
| 13 |
+
# Expose the port FastAPI runs on
|
| 14 |
+
EXPOSE 7860
|
| 15 |
+
|
| 16 |
+
# Run the FastAPI app
|
| 17 |
+
CMD ["python", "main.py"]
|
main.py
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from huggingface_hub import hf_hub_download
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoModelForSequenceClassification as modelSC, AutoTokenizer as token
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
|
| 7 |
+
app = FastAPI()
|
| 8 |
+
|
| 9 |
+
model_path = hf_hub_download(repo_id="MienOlle/sentiment_analysis_api",
|
| 10 |
+
filename="sentimentAnalysis.pth"
|
| 11 |
+
)
|
| 12 |
+
modelToken = token.from_pretrained("mdhugol/indonesia-bert-sentiment-classification")
|
| 13 |
+
model = modelSC.from_pretrained("mdhugol/indonesia-bert-sentiment-classification", num_labels=3)
|
| 14 |
+
model.load_state_dict(torch.load(model_path, map_location=torch.device("cpu")))
|
| 15 |
+
model.eval()
|
| 16 |
+
|
| 17 |
+
class TextInput(BaseModel):
|
| 18 |
+
text: str
|
| 19 |
+
|
| 20 |
+
def predict(input):
|
| 21 |
+
inputs = modelToken(input, return_tensors="pt", padding=True, truncation=True, max_length=512)
|
| 22 |
+
|
| 23 |
+
with torch.no_grad():
|
| 24 |
+
outputs = model(**inputs)
|
| 25 |
+
|
| 26 |
+
logits = outputs.logits
|
| 27 |
+
ret = logits.argmax().item()
|
| 28 |
+
|
| 29 |
+
labels = ["positive", "neutral", "negative"]
|
| 30 |
+
return {labels[ret]}
|
| 31 |
+
|
| 32 |
+
@app.post("/predict")
|
| 33 |
+
def get_sentiment(data: TextInput):
|
| 34 |
+
return predict(data.text)
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
import uvicorn
|
| 38 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
torch
|
| 4 |
+
transformers
|
| 5 |
+
huggingface_hub
|