Spaces:
Sleeping
Sleeping
| 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" | |
| } | |
| 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) | |