mcp-sentiment / app.py
Zoro-147's picture
Update app.py
6c3e4b6 verified
raw
history blame
1.33 kB
import json
from textblob import TextBlob
from fastapi import FastAPI, HTTPException
import uvicorn
app = FastAPI()
def analyze_sentiment(text: str) -> dict:
"""Core sentiment analysis logic"""
blob = TextBlob(text)
sentiment = blob.sentiment
return {
"polarity": round(sentiment.polarity, 2),
"subjectivity": round(sentiment.subjectivity, 2),
"assessment": "positive" if sentiment.polarity > 0
else "negative" if sentiment.polarity < 0
else "neutral"
}
@app.post("/mcp/sentiment")
async def handle_mcp_request(data: dict):
"""
MCP-compatible endpoint
Format: {"parameters": {"text": "your text"}}
"""
try:
text = data.get("parameters", {}).get("text", "")
if not text:
raise HTTPException(status_code=400, detail="Missing 'text' parameter")
return {
"jsonrpc": "2.0",
"result": analyze_sentiment(text),
"id": "sentiment-response"
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
@app.get("/")
async def health_check():
"""Required for Hugging Face health checks"""
return {"status": "OK"}
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=7860) # HF uses 7860