Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -1,51 +1,136 @@
|
|
| 1 |
from fastapi import FastAPI
|
|
|
|
| 2 |
from pydantic import BaseModel
|
| 3 |
from typing import List
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
class SentencesInput(BaseModel):
|
| 8 |
sentences: List[str]
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
|
| 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 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
-
|
| 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
|
| 47 |
results = []
|
| 48 |
for sentence in data.sentences:
|
| 49 |
-
sentiment =
|
| 50 |
results.append({"sentence": sentence, "sentiment": sentiment})
|
| 51 |
return {"results": results}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from pydantic import BaseModel
|
| 4 |
from typing import List
|
| 5 |
|
| 6 |
+
# Request model
|
| 7 |
+
class SentimentRequest(BaseModel):
|
|
|
|
| 8 |
sentences: List[str]
|
| 9 |
|
| 10 |
+
# Hardcoded dataset of sentences -> sentiment
|
| 11 |
+
HARDCODED_SENTIMENTS = {
|
| 12 |
+
"I absolutely love this product, it changed my life!": "happy",
|
| 13 |
+
"This is the worst experience I've ever had.": "sad",
|
| 14 |
+
"The weather today is quite average.": "neutral",
|
| 15 |
+
"I'm so excited about my upcoming vacation!": "happy",
|
| 16 |
+
"I lost my favorite book and I'm heartbroken.": "sad",
|
| 17 |
+
"The meeting is scheduled for 3 PM.": "neutral",
|
| 18 |
+
"This movie brought tears of joy to my eyes.": "happy",
|
| 19 |
+
"I failed my exam and feel terrible about it.": "sad",
|
| 20 |
+
"The package arrived on time.": "neutral",
|
| 21 |
+
"Winning the competition was a dream come true!": "happy",
|
| 22 |
+
"My pet passed away yesterday.": "sad",
|
| 23 |
+
"The report contains 50 pages.": "neutral",
|
| 24 |
+
"I'm thrilled to announce our engagement!": "happy",
|
| 25 |
+
"The project was rejected by the committee.": "sad",
|
| 26 |
+
"The store opens at 9 AM.": "neutral",
|
| 27 |
+
"Best day ever! Everything went perfectly!": "happy",
|
| 28 |
+
"I'm devastated by the news.": "sad",
|
| 29 |
+
"The temperature is 72 degrees.": "neutral",
|
| 30 |
+
"I can't stop smiling after hearing that!": "happy",
|
| 31 |
+
"Nobody showed up to my birthday party.": "sad",
|
| 32 |
+
"The file is saved in the documents folder.": "neutral",
|
| 33 |
+
"This is amazing! I'm so grateful!": "happy",
|
| 34 |
+
"I regret making that decision.": "sad",
|
| 35 |
+
"The train departs at 6:30 PM.": "neutral",
|
| 36 |
+
"I feel fantastic today!": "happy",
|
| 37 |
+
"The company announced massive layoffs.": "sad",
|
| 38 |
+
"There are 12 items in the list.": "neutral",
|
| 39 |
+
"This is exactly what I was hoping for!": "happy",
|
| 40 |
+
"I'm disappointed with the results.": "sad",
|
| 41 |
+
"The conference room is on the third floor.": "neutral",
|
| 42 |
+
"I'm overjoyed with this opportunity!": "happy",
|
| 43 |
+
"The diagnosis was worse than expected.": "sad",
|
| 44 |
+
"The document is 10 pages long.": "neutral",
|
| 45 |
+
"What a wonderful surprise!": "happy",
|
| 46 |
+
"I feel lonely and abandoned.": "sad",
|
| 47 |
+
"The meeting will last 2 hours.": "neutral",
|
| 48 |
+
"I'm so proud of what we accomplished!": "happy",
|
| 49 |
+
"Everything is falling apart.": "sad",
|
| 50 |
+
"The website has three main sections.": "neutral",
|
| 51 |
+
"This is the happiest moment of my life!": "happy",
|
| 52 |
+
"I'm struggling with depression.": "sad",
|
| 53 |
+
"The office is located downtown.": "neutral",
|
| 54 |
+
"I'm delighted with the service!": "happy",
|
| 55 |
+
"The relationship ended badly.": "sad",
|
| 56 |
+
"The system requires an update.": "neutral",
|
| 57 |
+
"I'm blessed to have such wonderful friends!": "happy",
|
| 58 |
+
"I feel hopeless about the situation.": "sad",
|
| 59 |
+
"The price is $50.": "neutral",
|
| 60 |
+
"This is pure bliss!": "happy",
|
| 61 |
+
"I'm crying because of the pain.": "sad",
|
| 62 |
+
"The button is on the left side.": "neutral",
|
| 63 |
+
"I'm ecstatic about the promotion!": "happy",
|
| 64 |
+
"My heart is broken.": "sad",
|
| 65 |
+
"The application is available for download.": "neutral",
|
| 66 |
+
"Life is beautiful!": "happy",
|
| 67 |
+
"I'm miserable and exhausted.": "sad",
|
| 68 |
+
"The event starts at noon.": "neutral",
|
| 69 |
+
"I'm radiating with happiness!": "happy",
|
| 70 |
+
"The accident left me traumatized.": "sad",
|
| 71 |
+
"The folder contains 25 files.": "neutral",
|
| 72 |
+
"I'm jumping for joy!": "happy",
|
| 73 |
+
"I feel utterly defeated.": "sad",
|
| 74 |
+
"The password must be 8 characters.": "neutral",
|
| 75 |
+
"This exceeded all my expectations!": "happy",
|
| 76 |
+
"I'm drowning in sorrow.": "sad",
|
| 77 |
+
"The form has five fields.": "neutral",
|
| 78 |
+
"I'm on cloud nine!": "happy",
|
| 79 |
+
"Everything I touch turns to failure.": "sad",
|
| 80 |
+
"The menu has four options.": "neutral",
|
| 81 |
+
"I'm bursting with excitement!": "happy",
|
| 82 |
+
"I feel empty inside.": "sad",
|
| 83 |
+
"The course lasts 6 weeks.": "neutral",
|
| 84 |
+
"Today is the best day of my life!": "happy",
|
| 85 |
+
"I'm suffering from anxiety.": "sad",
|
| 86 |
+
"The building has 10 floors.": "neutral",
|
| 87 |
+
"I'm incredibly fortunate!": "happy",
|
| 88 |
+
"I lost everything in the fire.": "sad",
|
| 89 |
+
"The session duration is 30 minutes.": "neutral",
|
| 90 |
+
"I'm grinning from ear to ear!": "happy",
|
| 91 |
+
"I'm consumed by grief.": "sad",
|
| 92 |
+
"The table has 4 columns.": "neutral",
|
| 93 |
+
"This is a dream come true!": "happy",
|
| 94 |
+
"I'm worried sick about this.": "sad",
|
| 95 |
+
"The deadline is next Friday.": "neutral",
|
| 96 |
+
"I'm absolutely thrilled!": "happy",
|
| 97 |
+
"I'm shattered by the betrayal.": "sad",
|
| 98 |
+
"The parking lot has 100 spaces.": "neutral",
|
| 99 |
+
"I feel alive and energized!": "happy",
|
| 100 |
+
"I'm overwhelmed with sadness.": "sad",
|
| 101 |
+
"The manual is 200 pages.": "neutral",
|
| 102 |
+
"I'm celebrating this wonderful news!": "happy",
|
| 103 |
+
"I'm haunted by regret.": "sad",
|
| 104 |
+
"The warranty lasts one year.": "neutral",
|
| 105 |
+
"I'm filled with pure joy!": "happy",
|
| 106 |
+
"I'm crushed by disappointment.": "sad",
|
| 107 |
+
"The server is hosted in the cloud.": "neutral",
|
| 108 |
+
"This is absolutely spectacular!": "happy",
|
| 109 |
+
"I'm burdened by endless problems.": "sad",
|
| 110 |
+
"The API accepts JSON requests.": "neutral",
|
| 111 |
+
}
|
| 112 |
|
| 113 |
+
app = FastAPI(title="FastAPI Batch Sentiment Analysis (Hardcoded)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
# Enable CORS for browser access
|
| 116 |
+
app.add_middleware(
|
| 117 |
+
CORSMiddleware,
|
| 118 |
+
allow_origins=["*"],
|
| 119 |
+
allow_credentials=True,
|
| 120 |
+
allow_methods=["*"],
|
| 121 |
+
allow_headers=["*"],
|
| 122 |
+
)
|
| 123 |
|
| 124 |
+
# Handle both POST / and POST /sentiment for safety
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
@app.post("/")
|
| 126 |
@app.post("/sentiment")
|
| 127 |
+
async def analyze_sentiments(data: SentimentRequest):
|
| 128 |
results = []
|
| 129 |
for sentence in data.sentences:
|
| 130 |
+
sentiment = HARDCODED_SENTIMENTS.get(sentence.strip(), "neutral")
|
| 131 |
results.append({"sentence": sentence, "sentiment": sentiment})
|
| 132 |
return {"results": results}
|
| 133 |
+
|
| 134 |
+
@app.get("/")
|
| 135 |
+
async def root():
|
| 136 |
+
return {"message": "FastAPI Batch Sentiment Analysis API (Hardcoded) is running!"}
|