Spaces:
Sleeping
Sleeping
Commit
·
b4d678b
1
Parent(s):
f870834
changed back to original model
Browse files
app.py
CHANGED
|
@@ -7,10 +7,10 @@ from typing import List
|
|
| 7 |
# Initialize FastAPI
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
-
# Load
|
| 11 |
sentiment_pipeline = pipeline(
|
| 12 |
"sentiment-analysis",
|
| 13 |
-
model="
|
| 14 |
)
|
| 15 |
|
| 16 |
# Request models
|
|
@@ -28,11 +28,13 @@ class SentimentResponse(BaseModel):
|
|
| 28 |
sentiment_label: str
|
| 29 |
confidence: float
|
| 30 |
|
| 31 |
-
# Mapping
|
| 32 |
LABEL_MAP = {
|
| 33 |
-
"
|
| 34 |
-
"
|
| 35 |
-
"
|
|
|
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
@app.post("/analyze", response_model=SentimentResponse)
|
|
@@ -40,11 +42,11 @@ def analyze_sentiment(request: SentimentRequest):
|
|
| 40 |
try:
|
| 41 |
# Get model prediction
|
| 42 |
result = sentiment_pipeline(request.text)[0]
|
| 43 |
-
label = result["label"]
|
| 44 |
score = result["score"]
|
| 45 |
|
| 46 |
-
# Convert
|
| 47 |
-
sentiment_score = LABEL_MAP
|
| 48 |
confidence = round(score, 3)
|
| 49 |
|
| 50 |
return SentimentResponse(
|
|
@@ -62,11 +64,11 @@ def analyze_sentiment_batch(request: BatchSentimentRequest):
|
|
| 62 |
responses = []
|
| 63 |
for post in request.posts:
|
| 64 |
result = sentiment_pipeline(post.text)[0]
|
| 65 |
-
label = result["label"]
|
| 66 |
score = result["score"]
|
| 67 |
|
| 68 |
-
# Convert
|
| 69 |
-
sentiment_score = LABEL_MAP
|
| 70 |
confidence = round(score, 3)
|
| 71 |
|
| 72 |
responses.append(SentimentResponse(
|
|
@@ -82,4 +84,4 @@ def analyze_sentiment_batch(request: BatchSentimentRequest):
|
|
| 82 |
# Root endpoint
|
| 83 |
@app.get("/")
|
| 84 |
def root():
|
| 85 |
-
return {"message": "
|
|
|
|
| 7 |
# Initialize FastAPI
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# Load BERT multilingual sentiment analysis model
|
| 11 |
sentiment_pipeline = pipeline(
|
| 12 |
"sentiment-analysis",
|
| 13 |
+
model="nlptown/bert-base-multilingual-uncased-sentiment"
|
| 14 |
)
|
| 15 |
|
| 16 |
# Request models
|
|
|
|
| 28 |
sentiment_label: str
|
| 29 |
confidence: float
|
| 30 |
|
| 31 |
+
# Mapping BERT star ratings to sentiment scores
|
| 32 |
LABEL_MAP = {
|
| 33 |
+
"1 star": -1.0, # Very Negative
|
| 34 |
+
"2 stars": -0.5, # Negative
|
| 35 |
+
"3 stars": 0.0, # Neutral
|
| 36 |
+
"4 stars": 0.5, # Positive
|
| 37 |
+
"5 stars": 1.0 # Very Positive
|
| 38 |
}
|
| 39 |
|
| 40 |
@app.post("/analyze", response_model=SentimentResponse)
|
|
|
|
| 42 |
try:
|
| 43 |
# Get model prediction
|
| 44 |
result = sentiment_pipeline(request.text)[0]
|
| 45 |
+
label = result["label"].lower()
|
| 46 |
score = result["score"]
|
| 47 |
|
| 48 |
+
# Convert BERT star rating to floating-point sentiment score
|
| 49 |
+
sentiment_score = LABEL_MAP.get(label, 0.0) # Default to neutral (0.0) if label is unexpected
|
| 50 |
confidence = round(score, 3)
|
| 51 |
|
| 52 |
return SentimentResponse(
|
|
|
|
| 64 |
responses = []
|
| 65 |
for post in request.posts:
|
| 66 |
result = sentiment_pipeline(post.text)[0]
|
| 67 |
+
label = result["label"].lower()
|
| 68 |
score = result["score"]
|
| 69 |
|
| 70 |
+
# Convert BERT star rating to floating-point sentiment score
|
| 71 |
+
sentiment_score = LABEL_MAP.get(label, 0.0)
|
| 72 |
confidence = round(score, 3)
|
| 73 |
|
| 74 |
responses.append(SentimentResponse(
|
|
|
|
| 84 |
# Root endpoint
|
| 85 |
@app.get("/")
|
| 86 |
def root():
|
| 87 |
+
return {"message": "BERT Multilingual Sentiment Analysis API is running!"}
|