Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,23 +1,27 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
| 2 |
-
from pydantic import BaseModel
|
| 3 |
-
from typing import List
|
| 4 |
-
|
| 5 |
-
app = FastAPI()
|
| 6 |
-
|
| 7 |
-
class SentencesInput(BaseModel):
|
| 8 |
-
sentences: List[str]
|
| 9 |
-
|
| 10 |
-
@app.
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
+
from typing import List
|
| 4 |
+
|
| 5 |
+
app = FastAPI()
|
| 6 |
+
|
| 7 |
+
class SentencesInput(BaseModel):
|
| 8 |
+
sentences: List[str]
|
| 9 |
+
|
| 10 |
+
@app.get("/")
|
| 11 |
+
def home():
|
| 12 |
+
return {"message": "Sentiment API is running. Use /sentiment for POST requests."}
|
| 13 |
+
|
| 14 |
+
@app.post("/sentiment")
|
| 15 |
+
async def analyze_sentiment(data: SentencesInput):
|
| 16 |
+
happy = ["love", "great", "awesome", "amazing", "good", "fantastic", "best", "wonderful"]
|
| 17 |
+
sad = ["hate", "bad", "terrible", "awful", "horrible", "sad", "worst", "disappointed"]
|
| 18 |
+
results = []
|
| 19 |
+
for s in data.sentences:
|
| 20 |
+
text = s.lower()
|
| 21 |
+
sentiment = "neutral"
|
| 22 |
+
if any(w in text for w in happy):
|
| 23 |
+
sentiment = "happy"
|
| 24 |
+
elif any(w in text for w in sad):
|
| 25 |
+
sentiment = "sad"
|
| 26 |
+
results.append({"sentence": s, "sentiment": sentiment})
|
| 27 |
+
return {"results": results}
|