Shrishti03 commited on
Commit
18d9710
·
verified ·
1 Parent(s): 947c9ab

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +27 -23
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.post("/sentiment")
11
- async def analyze_sentiment(data: SentencesInput):
12
- happy = ["love", "great", "awesome", "amazing", "good", "fantastic", "best", "wonderful"]
13
- sad = ["hate", "bad", "terrible", "awful", "horrible", "sad", "worst", "disappointed"]
14
- results = []
15
- for s in data.sentences:
16
- text = s.lower()
17
- sentiment = "neutral"
18
- if any(w in text for w in happy):
19
- sentiment = "happy"
20
- elif any(w in text for w in sad):
21
- sentiment = "sad"
22
- results.append({"sentence": s, "sentiment": sentiment})
23
- return {"results": results}
 
 
 
 
 
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}