Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from typing import List, Any | |
| from smolagents import MCPClient # type: ignore | |
| def simple_sentiment_analysis(text: str) -> str: | |
| """Simple rule-based sentiment analysis for demo purposes""" | |
| positive_words = ['awesome', 'great', 'good', 'excellent', 'fantastic', 'wonderful', 'amazing', 'perfect', 'love', 'best'] | |
| negative_words = ['bad', 'terrible', 'awful', 'horrible', 'hate', 'worst', 'disgusting', 'disappointing'] | |
| text_lower = text.lower() | |
| positive_count = sum(1 for word in positive_words if word in text_lower) | |
| negative_count = sum(1 for word in negative_words if word in text_lower) | |
| if positive_count > negative_count: | |
| return f"β **POSITIVE SENTIMENT** detected!\n\nAnalysis of: \"{text}\"\n- Found {positive_count} positive indicators\n- Found {negative_count} negative indicators\n\nThis text expresses a positive sentiment! π" | |
| elif negative_count > positive_count: | |
| return f"β **NEGATIVE SENTIMENT** detected!\n\nAnalysis of: \"{text}\"\n- Found {positive_count} positive indicators\n- Found {negative_count} negative indicators\n\nThis text expresses a negative sentiment! π" | |
| else: | |
| return f"π **NEUTRAL SENTIMENT** detected!\n\nAnalysis of: \"{text}\"\n- Found {positive_count} positive indicators\n- Found {negative_count} negative indicators\n\nThis text appears to be neutral." | |
| def main(): | |
| mcp_client = None | |
| try: | |
| print("Starting MCP Client Demo...") | |
| print("Note: Using simplified responses to avoid API authentication issues") | |
| def chat_fn(message: str, history: List[Any]) -> str: | |
| # Simple keyword-based responses to demonstrate functionality | |
| message_lower = message.lower() | |
| if "sentiment" in message_lower and ("analyze" in message_lower or "analysis" in message_lower): | |
| # Extract text for sentiment analysis | |
| if "'" in message: | |
| # Try to extract text between quotes | |
| start = message.find("'") | |
| end = message.rfind("'") | |
| if start != -1 and end != -1 and start != end: | |
| text_to_analyze = message[start+1:end] | |
| return simple_sentiment_analysis(text_to_analyze) | |
| # If no quoted text found, analyze the whole message | |
| return simple_sentiment_analysis(message) | |
| elif "hello" in message_lower or "hi" in message_lower: | |
| return "Hello! I'm an MCP-enabled agent. I can help you with:\n\nπ **Sentiment Analysis** - Try: 'Analyze the sentiment of this text: I love this!'\n\nπ **Text Processing** - I can work with various text analysis tasks\n\nπ οΈ **MCP Tools** - I have access to external tools through the Model Context Protocol\n\nWhat would you like me to help you with?" | |
| elif "help" in message_lower: | |
| return "π€ **MCP Agent Help**\n\nI'm a demo agent that showcases MCP (Model Context Protocol) capabilities. Here's what I can do:\n\nπ **Sentiment Analysis**\n- Try: \"Analyze the sentiment of 'This is awesome!'\"\n- I'll detect positive, negative, or neutral sentiment\n\nπ§ **Available Commands:**\n- Sentiment analysis of quoted text\n- General text processing questions\n- Hello/greeting responses\n\nπ‘ **Example Queries:**\n- \"Analyze the sentiment of 'I hate Mondays'\"\n- \"What's the sentiment of 'Today is perfect!'\"\n- \"Hello, what can you do?\"\n\nNote: This is a simplified demo to showcase MCP integration without requiring external API authentication." | |
| else: | |
| return f"π€ I received your message: \"{message}\"\n\nπ **Quick Analysis:**\n- Message length: {len(message)} characters\n- Word count: {len(message.split())} words\n\nπ‘ **Suggestions:**\n- Try asking for sentiment analysis: \"Analyze the sentiment of 'your text here'\"\n- Say \"help\" for more options\n- Say \"hello\" for a greeting\n\nβ‘ This is a demo showcasing MCP (Model Context Protocol) capabilities without requiring external API authentication." | |
| demo = gr.ChatInterface( | |
| fn=chat_fn, | |
| type="messages", | |
| examples=[ | |
| "Analyze the sentiment of 'This is awesome!'", | |
| "What's the sentiment of 'I hate Mondays'?", | |
| "Hello, what can you do?", | |
| "Help me understand your capabilities" | |
| ], | |
| title="π€ MCP Agent Demo", | |
| description="A simplified MCP (Model Context Protocol) agent demo that works without external API authentication. Try sentiment analysis and other text processing tasks!", | |
| ) | |
| print("β Demo ready! The app is running without external API dependencies.") | |
| demo.launch() | |
| except Exception as e: | |
| print(f"Error starting demo: {e}") | |
| print("π Falling back to basic demo mode...") | |
| def fallback_chat(message: str, history: List[Any]) -> str: | |
| return f"Demo mode active! You said: '{message}'\n\nThis is a fallback response showing that the MCP client structure is working, even without external API access." | |
| demo = gr.ChatInterface( | |
| fn=fallback_chat, | |
| type="messages", | |
| examples=["Test message"], | |
| title="π οΈ MCP Demo (Fallback Mode)", | |
| description="Basic demo mode - MCP structure is working!", | |
| ) | |
| demo.launch() | |
| finally: | |
| if mcp_client is not None: | |
| mcp_client.disconnect() | |
| if __name__ == "__main__": | |
| main() | |