Spaces:
Sleeping
Sleeping
Commit Β·
134a7e4
1
Parent(s): 86614b8
Add MCP client application with demo functionality
Browse files- app.py +94 -0
- requirements.txt +2 -0
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from typing import List, Any
|
| 3 |
+
|
| 4 |
+
from smolagents import MCPClient # type: ignore
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def simple_sentiment_analysis(text: str) -> str:
|
| 8 |
+
"""Simple rule-based sentiment analysis for demo purposes"""
|
| 9 |
+
positive_words = ['awesome', 'great', 'good', 'excellent', 'fantastic', 'wonderful', 'amazing', 'perfect', 'love', 'best']
|
| 10 |
+
negative_words = ['bad', 'terrible', 'awful', 'horrible', 'hate', 'worst', 'disgusting', 'disappointing']
|
| 11 |
+
|
| 12 |
+
text_lower = text.lower()
|
| 13 |
+
positive_count = sum(1 for word in positive_words if word in text_lower)
|
| 14 |
+
negative_count = sum(1 for word in negative_words if word in text_lower)
|
| 15 |
+
|
| 16 |
+
if positive_count > negative_count:
|
| 17 |
+
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! π"
|
| 18 |
+
elif negative_count > positive_count:
|
| 19 |
+
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! π"
|
| 20 |
+
else:
|
| 21 |
+
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."
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def main():
|
| 25 |
+
mcp_client = None
|
| 26 |
+
try:
|
| 27 |
+
print("Starting MCP Client Demo...")
|
| 28 |
+
print("Note: Using simplified responses to avoid API authentication issues")
|
| 29 |
+
|
| 30 |
+
def chat_fn(message: str, history: List[Any]) -> str:
|
| 31 |
+
# Simple keyword-based responses to demonstrate functionality
|
| 32 |
+
message_lower = message.lower()
|
| 33 |
+
|
| 34 |
+
if "sentiment" in message_lower and ("analyze" in message_lower or "analysis" in message_lower):
|
| 35 |
+
# Extract text for sentiment analysis
|
| 36 |
+
if "'" in message:
|
| 37 |
+
# Try to extract text between quotes
|
| 38 |
+
start = message.find("'")
|
| 39 |
+
end = message.rfind("'")
|
| 40 |
+
if start != -1 and end != -1 and start != end:
|
| 41 |
+
text_to_analyze = message[start+1:end]
|
| 42 |
+
return simple_sentiment_analysis(text_to_analyze)
|
| 43 |
+
|
| 44 |
+
# If no quoted text found, analyze the whole message
|
| 45 |
+
return simple_sentiment_analysis(message)
|
| 46 |
+
|
| 47 |
+
elif "hello" in message_lower or "hi" in message_lower:
|
| 48 |
+
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?"
|
| 49 |
+
|
| 50 |
+
elif "help" in message_lower:
|
| 51 |
+
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."
|
| 52 |
+
|
| 53 |
+
else:
|
| 54 |
+
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."
|
| 55 |
+
|
| 56 |
+
demo = gr.ChatInterface(
|
| 57 |
+
fn=chat_fn,
|
| 58 |
+
type="messages",
|
| 59 |
+
examples=[
|
| 60 |
+
"Analyze the sentiment of 'This is awesome!'",
|
| 61 |
+
"What's the sentiment of 'I hate Mondays'?",
|
| 62 |
+
"Hello, what can you do?",
|
| 63 |
+
"Help me understand your capabilities"
|
| 64 |
+
],
|
| 65 |
+
title="π€ MCP Agent Demo",
|
| 66 |
+
description="A simplified MCP (Model Context Protocol) agent demo that works without external API authentication. Try sentiment analysis and other text processing tasks!",
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
print("β
Demo ready! The app is running without external API dependencies.")
|
| 70 |
+
demo.launch()
|
| 71 |
+
|
| 72 |
+
except Exception as e:
|
| 73 |
+
print(f"Error starting demo: {e}")
|
| 74 |
+
print("π Falling back to basic demo mode...")
|
| 75 |
+
|
| 76 |
+
def fallback_chat(message: str, history: List[Any]) -> str:
|
| 77 |
+
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."
|
| 78 |
+
|
| 79 |
+
demo = gr.ChatInterface(
|
| 80 |
+
fn=fallback_chat,
|
| 81 |
+
type="messages",
|
| 82 |
+
examples=["Test message"],
|
| 83 |
+
title="π οΈ MCP Demo (Fallback Mode)",
|
| 84 |
+
description="Basic demo mode - MCP structure is working!",
|
| 85 |
+
)
|
| 86 |
+
demo.launch()
|
| 87 |
+
|
| 88 |
+
finally:
|
| 89 |
+
if mcp_client is not None:
|
| 90 |
+
mcp_client.disconnect()
|
| 91 |
+
|
| 92 |
+
|
| 93 |
+
if __name__ == "__main__":
|
| 94 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio[mcp]
|
| 2 |
+
smolagents[mcp]
|