Spaces:
Sleeping
Sleeping
File size: 1,576 Bytes
685f307 85b90a3 685f307 85b90a3 685f307 85b90a3 685f307 85b90a3 685f307 85b90a3 685f307 85b90a3 685f307 85b90a3 685f307 85b90a3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
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
Expected input: {"parameters": {"text": "your text here"}}
"""
try:
text = data.get("parameters", {}).get("text", "")
if not text:
raise HTTPException(status_code=400, detail="Missing 'text' parameter")
result = analyze_sentiment(text)
return {
"jsonrpc": "2.0",
"result": result,
"id": "sentiment-response"
}
except Exception as e:
raise HTTPException(status_code=500, detail=str(e))
from fastapi.staticfiles import StaticFiles
import gradio as gr
# Mount Gradio interface at /ui
app.mount("/ui", gr.routes.App.create_app(demo))
# Create Gradio interface (same as original)
demo = gr.Interface(
fn=lambda text: analyze_sentiment(text),
inputs=gr.Textbox(),
outputs=gr.JSON(),
title="Sentiment Analysis UI"
)
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)
|