Spaces:
Sleeping
Sleeping
mr687
commited on
Commit
•
1eecf37
1
Parent(s):
f7d6bb2
initial app
Browse files- .dockerignore +5 -0
- .env.example +1 -0
- .gitignore +5 -0
- Dockerfile +14 -0
- Sentiment/ConfigEnv.py +0 -0
- Sentiment/__init__.py +13 -0
- Sentiment/model.py +11 -0
- Sentiment/router.py +50 -0
- app.py +1 -0
- docker-compose.yml +9 -0
- requirements.txt +5 -0
.dockerignore
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.git
|
2 |
+
__pycache__
|
3 |
+
venv
|
4 |
+
.env*
|
5 |
+
!.env.example
|
.env.example
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
API_KEY=
|
.gitignore
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
.DS_Store
|
2 |
+
venv
|
3 |
+
.env*
|
4 |
+
!.env.example
|
5 |
+
__pycache__
|
Dockerfile
ADDED
@@ -0,0 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.12-slim
|
2 |
+
|
3 |
+
RUN useradd -m -u 1000 user
|
4 |
+
USER user
|
5 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
6 |
+
|
7 |
+
WORKDIR /app
|
8 |
+
|
9 |
+
COPY --chown=user ./requirements.txt requirements.txt
|
10 |
+
RUN pip install --no-cache-dir --upgrade -r /app/requirements.txt
|
11 |
+
|
12 |
+
COPY --chown=user . /app
|
13 |
+
|
14 |
+
CMD [ "uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860", "--no-server-header" ]
|
Sentiment/ConfigEnv.py
ADDED
File without changes
|
Sentiment/__init__.py
ADDED
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
|
4 |
+
app = FastAPI(title="Analyze sentiment of text")
|
5 |
+
app.add_middleware(
|
6 |
+
CORSMiddleware,
|
7 |
+
allow_origins=["*"],
|
8 |
+
allow_credentials=True,
|
9 |
+
allow_methods=["*"],
|
10 |
+
allow_headers=["*"],
|
11 |
+
)
|
12 |
+
|
13 |
+
from Sentiment import router
|
Sentiment/model.py
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from functools import lru_cache
|
2 |
+
from transformers import BertTokenizer, BertConfig, BertForSequenceClassification
|
3 |
+
|
4 |
+
@lru_cache()
|
5 |
+
def initialize_model():
|
6 |
+
tokenizer = BertTokenizer.from_pretrained('indobenchmark/indobert-base-p1')
|
7 |
+
config = BertConfig.from_pretrained('daphinokio/indobert-smsa-fine-tuned')
|
8 |
+
model = BertForSequenceClassification.from_pretrained('daphinokio/indobert-smsa-fine-tuned', config=config)
|
9 |
+
return model, tokenizer
|
10 |
+
|
11 |
+
model, tokenizer = initialize_model()
|
Sentiment/router.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import torch
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from transformers import BertTokenizer, BertConfig, BertForSequenceClassification
|
5 |
+
from fastapi import FastAPI, Depends, HTTPException, status
|
6 |
+
from fastapi.security import HTTPBasic, HTTPBasicCredentials
|
7 |
+
from fastapi.middleware.cors import CORSMiddleware
|
8 |
+
from pydantic import BaseModel
|
9 |
+
from typing_extensions import Annotated
|
10 |
+
from Sentiment import app
|
11 |
+
from .model import model, tokenizer
|
12 |
+
|
13 |
+
app_security = HTTPBasic()
|
14 |
+
api_key = os.getenv("API_KEY")
|
15 |
+
|
16 |
+
if not api_key:
|
17 |
+
raise ValueError("API_KEY is missing.")
|
18 |
+
|
19 |
+
i2l = {0: 'positive', 1: 'neutral', 2: 'negative'}
|
20 |
+
|
21 |
+
class AnalyzeRequest(BaseModel):
|
22 |
+
text: str
|
23 |
+
|
24 |
+
class AnalyzeResponse(BaseModel):
|
25 |
+
text: str
|
26 |
+
label: str
|
27 |
+
score: float
|
28 |
+
|
29 |
+
@app.get("/checkhealth", tags=["CheckHealth"])
|
30 |
+
def checkhealth():
|
31 |
+
return "Sentiment API is running."
|
32 |
+
|
33 |
+
@app.post("/predict", tags=["Analyze"], summary="Analyze text from prompt", response_model=AnalyzeResponse)
|
34 |
+
def predict(creds: Annotated[HTTPBasicCredentials, Depends(app_security)], data: AnalyzeRequest):
|
35 |
+
if creds.password != api_key:
|
36 |
+
print(creds.password, api_key)
|
37 |
+
raise HTTPException(
|
38 |
+
status_code=status.HTTP_401_UNAUTHORIZED,
|
39 |
+
detail="Incorrect Password",
|
40 |
+
headers={"WWW-Authenticate": "Basic"}
|
41 |
+
)
|
42 |
+
|
43 |
+
text = data.text
|
44 |
+
test_sample = tokenizer.encode(text)
|
45 |
+
test_sample = torch.LongTensor(test_sample).view(1, -1).to(model.device)
|
46 |
+
logits = model(test_sample)[0]
|
47 |
+
label_index = torch.topk(logits, k=1, dim=-1)[1].squeeze().item()
|
48 |
+
label = i2l[label_index]
|
49 |
+
score = f'{F.softmax(logits, dim=-1).squeeze()[label_index]:.3f}'
|
50 |
+
return {"text":text, "label": label, "score": score}
|
app.py
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
from Sentiment import app
|
docker-compose.yml
ADDED
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
services:
|
2 |
+
server:
|
3 |
+
container_name: server
|
4 |
+
build:
|
5 |
+
context: .
|
6 |
+
dockerfile: Dockerfile
|
7 |
+
env_file: .env
|
8 |
+
ports:
|
9 |
+
- 7860:7860
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
transformers
|
2 |
+
torch
|
3 |
+
uvicorn[standard]
|
4 |
+
fastapi
|
5 |
+
numpy
|