Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ __pycache__/
instance/
.webassets-cache/

.DS_Store
.DS_Store

venv/
29 changes: 29 additions & 0 deletions ML/sentiment_analyzer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# TextBlob library used for simple sentiment analysis
from textblob import TextBlob

def analyze_sentiment(text):
"""
Analyze sentiment of input text and return standardized output.
"""

blob = TextBlob(text)
polarity = blob.sentiment.polarity

if polarity > 0:
sentiment = "positive"
elif polarity < 0:
sentiment = "negative"
else:
sentiment = "neutral"

return {
"sentiment": sentiment,
"confidence_score": abs(polarity),
"text_summary": f"Detected {sentiment} sentiment from the given text."
}


if __name__ == "__main__":
sample = "I really love this product!"
result = analyze_sentiment(sample)
print(result)
3 changes: 2 additions & 1 deletion app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from .routes import altsense, linksense, geminisense, colorsense, axe_colorsense
from .routes import altsense, linksense, geminisense, colorsense, axe_colorsense, sentiment

app = FastAPI(
title="Web Accessibility Analyzer API",
Expand All @@ -23,6 +23,7 @@
app.include_router(geminisense.router, prefix="/geminisense", tags=["GeminiSense Analysis"])
# app.include_router(colorsense.router, prefix="/colorsense-old", tags=["ColorSense Analysis - Deprecated"]) # Old implementation
app.include_router(axe_colorsense.router, tags=["ColorSense Analysis"])
app.include_router(sentiment.router, prefix="/sentiment", tags=["Sentiment Analysis"])

@app.get("/")
async def root():
Expand Down
38 changes: 38 additions & 0 deletions app/routes/sentiment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Sentiment analysis endpoint using TextBlob
# This exposes the sentiment analyzer module through the API
from fastapi import APIRouter
from pydantic import BaseModel
from ML.sentiment_analyzer import analyze_sentiment

router = APIRouter()

class SentimentRequest(BaseModel):
text: str
class BatchSentimentRequest(BaseModel):
texts: list[str]

@router.post(
"/analyze",
summary="Analyze sentiment of text",
description="Takes a text input and returns sentiment, confidence score, and a short summary."
)
def analyze_text(data: SentimentRequest):
if not data.text.strip():
return {"error": "Text input cannot be empty"}

result = analyze_sentiment(data.text)
return result


@router.post("/batch")
def analyze_batch(data: BatchSentimentRequest):
"""
Analyze sentiment for multiple texts.
"""

results = []

for text in data.texts:
result = analyze_sentiment(text)
results.append(result)
return results
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,4 @@ wsproto==1.2.0
transformers>=4.25.0
torch>=2.0.0
pillow>=9.0.0
textblob==0.19.0