Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -7,37 +7,45 @@ app = FastAPI()
|
|
| 7 |
class SentencesInput(BaseModel):
|
| 8 |
sentences: List[str]
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
@app.get("/")
|
| 11 |
async def home():
|
| 12 |
-
return {"message": "API is running successfully!"}
|
| 13 |
|
| 14 |
-
#
|
| 15 |
@app.post("/")
|
| 16 |
-
async def analyze_root(data: SentencesInput):
|
| 17 |
-
happy = ["love", "great", "awesome", "amazing", "good", "fantastic", "best", "wonderful", "enjoy"]
|
| 18 |
-
sad = ["hate", "bad", "terrible", "awful", "horrible", "sad", "worst", "disappointed", "angry"]
|
| 19 |
-
results = []
|
| 20 |
-
for s in data.sentences:
|
| 21 |
-
text = s.lower()
|
| 22 |
-
sentiment = "neutral"
|
| 23 |
-
if any(w in text for w in happy):
|
| 24 |
-
sentiment = "happy"
|
| 25 |
-
elif any(w in text for w in sad):
|
| 26 |
-
sentiment = "sad"
|
| 27 |
-
results.append({"sentence": s, "sentiment": sentiment})
|
| 28 |
-
return {"results": results}
|
| 29 |
-
|
| 30 |
@app.post("/sentiment")
|
| 31 |
async def analyze_sentiment(data: SentencesInput):
|
| 32 |
-
happy = ["love", "great", "awesome", "amazing", "good", "fantastic", "best", "wonderful", "enjoy"]
|
| 33 |
-
sad = ["hate", "bad", "terrible", "awful", "horrible", "sad", "worst", "disappointed", "angry"]
|
| 34 |
results = []
|
| 35 |
-
for
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
if any(w in text for w in happy):
|
| 39 |
-
sentiment = "happy"
|
| 40 |
-
elif any(w in text for w in sad):
|
| 41 |
-
sentiment = "sad"
|
| 42 |
-
results.append({"sentence": s, "sentiment": sentiment})
|
| 43 |
return {"results": results}
|
|
|
|
| 7 |
class SentencesInput(BaseModel):
|
| 8 |
sentences: List[str]
|
| 9 |
|
| 10 |
+
def detect_sentiment(text: str) -> str:
|
| 11 |
+
text = text.lower()
|
| 12 |
+
|
| 13 |
+
happy_words = [
|
| 14 |
+
"love", "like", "enjoy", "great", "awesome", "amazing", "good", "fantastic",
|
| 15 |
+
"best", "wonderful", "happy", "pleased", "delight", "joy", "joyful", "excited",
|
| 16 |
+
"excellent", "positive", "thrilled", "glad"
|
| 17 |
+
]
|
| 18 |
+
sad_words = [
|
| 19 |
+
"hate", "bad", "terrible", "awful", "horrible", "sad", "worst", "disappointed",
|
| 20 |
+
"angry", "upset", "depress", "cry", "pain", "poor", "unhappy", "miserable",
|
| 21 |
+
"lonely", "heartbroken", "sucks"
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# Rule-based detection with negations
|
| 25 |
+
for word in happy_words:
|
| 26 |
+
if word in text:
|
| 27 |
+
if "not " + word in text or "no " + word in text:
|
| 28 |
+
return "sad"
|
| 29 |
+
return "happy"
|
| 30 |
+
|
| 31 |
+
for word in sad_words:
|
| 32 |
+
if word in text:
|
| 33 |
+
if "not " + word in text or "no " + word in text:
|
| 34 |
+
return "happy"
|
| 35 |
+
return "sad"
|
| 36 |
+
|
| 37 |
+
return "neutral"
|
| 38 |
+
|
| 39 |
@app.get("/")
|
| 40 |
async def home():
|
| 41 |
+
return {"message": "Sentiment API is running successfully!"}
|
| 42 |
|
| 43 |
+
# Allow POST at both '/' and '/sentiment'
|
| 44 |
@app.post("/")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
@app.post("/sentiment")
|
| 46 |
async def analyze_sentiment(data: SentencesInput):
|
|
|
|
|
|
|
| 47 |
results = []
|
| 48 |
+
for sentence in data.sentences:
|
| 49 |
+
sentiment = detect_sentiment(sentence)
|
| 50 |
+
results.append({"sentence": sentence, "sentiment": sentiment})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
return {"results": results}
|