Spaces:
Sleeping
Sleeping
Commit ·
06060fa
1
Parent(s): 46d4a2c
feat: added post id to request to keep track of posts
Browse files
app.py
CHANGED
|
@@ -9,20 +9,25 @@ sentiment_analyzer = pipeline("sentiment-analysis", model="nlptown/bert-base-mul
|
|
| 9 |
|
| 10 |
class SentimentRequest(BaseModel):
|
| 11 |
text: str
|
|
|
|
| 12 |
|
| 13 |
@app.post("/sentiment")
|
| 14 |
async def analyze_sentiment(request: SentimentRequest):
|
| 15 |
result = sentiment_analyzer(request.text)
|
| 16 |
-
return {"label": result[0]["label"], "score": result[0]["score"]}
|
| 17 |
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
class SentimentBatchRequest(BaseModel):
|
| 20 |
-
|
| 21 |
|
| 22 |
@app.post("/sentiment/batch")
|
| 23 |
async def analyze_sentiment_batch(request: SentimentBatchRequest):
|
| 24 |
-
|
| 25 |
-
|
|
|
|
| 26 |
|
| 27 |
@app.get("/")
|
| 28 |
def greet_json():
|
|
|
|
| 9 |
|
| 10 |
class SentimentRequest(BaseModel):
|
| 11 |
text: str
|
| 12 |
+
post_id: str
|
| 13 |
|
| 14 |
@app.post("/sentiment")
|
| 15 |
async def analyze_sentiment(request: SentimentRequest):
|
| 16 |
result = sentiment_analyzer(request.text)
|
| 17 |
+
return {"post_id": request.post_id, "label": result[0]["label"], "score": result[0]["score"]}
|
| 18 |
|
| 19 |
+
class SentimentPost(BaseModel):
|
| 20 |
+
post_id: str
|
| 21 |
+
text: str
|
| 22 |
|
| 23 |
class SentimentBatchRequest(BaseModel):
|
| 24 |
+
posts: List[SentimentPost]
|
| 25 |
|
| 26 |
@app.post("/sentiment/batch")
|
| 27 |
async def analyze_sentiment_batch(request: SentimentBatchRequest):
|
| 28 |
+
texts = [post.text for post in request.posts]
|
| 29 |
+
results = sentiment_analyzer(texts)
|
| 30 |
+
return [{"text": post.text, "post_id": post.post_id, "label": res["label"], "score": res["score"]} for post, res in zip(request.posts, results)]
|
| 31 |
|
| 32 |
@app.get("/")
|
| 33 |
def greet_json():
|