Jarisko
commited on
Commit
·
2a169e3
1
Parent(s):
921ee57
other version of server and some attempts on clients
Browse files- app.py +26 -10
- client.py +58 -0
- requirements.txt +3 -1
- test.ipynb +33 -3
app.py
CHANGED
|
@@ -1,18 +1,34 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
| 2 |
|
| 3 |
-
def
|
| 4 |
"""
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
"""
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from textblob import TextBlob
|
| 3 |
|
| 4 |
+
def sentiment_analysis(text: str) -> dict:
|
| 5 |
"""
|
| 6 |
+
Analyze the sentiment of the given text.
|
| 7 |
+
|
| 8 |
+
Args:
|
| 9 |
+
text (str): The text to analyze
|
| 10 |
+
|
| 11 |
+
Returns:
|
| 12 |
+
dict: A dictionary containing polarity, subjectivity, and assessment
|
| 13 |
"""
|
| 14 |
+
blob = TextBlob(text)
|
| 15 |
+
sentiment = blob.sentiment
|
| 16 |
|
| 17 |
+
return {
|
| 18 |
+
"polarity": round(sentiment.polarity, 2), # -1 (negative) to 1 (positive)
|
| 19 |
+
"subjectivity": round(sentiment.subjectivity, 2), # 0 (objective) to 1 (subjective)
|
| 20 |
+
"assessment": "positive" if sentiment.polarity > 0 else "negative" if sentiment.polarity < 0 else "neutral"
|
| 21 |
+
}
|
| 22 |
|
| 23 |
+
# Create the Gradio interface
|
| 24 |
+
demo = gr.Interface(
|
| 25 |
+
fn=sentiment_analysis,
|
| 26 |
+
inputs=gr.Textbox(placeholder="Enter text to analyze..."),
|
| 27 |
+
outputs=gr.JSON(),
|
| 28 |
+
title="Text Sentiment Analysis",
|
| 29 |
+
description="Analyze the sentiment of text using TextBlob"
|
| 30 |
)
|
| 31 |
|
| 32 |
+
# Launch the interface and MCP server
|
| 33 |
+
if __name__ == "__main__":
|
| 34 |
+
demo.launch(mcp_server=True)
|
client.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import List, Dict
|
| 2 |
+
import asyncio
|
| 3 |
+
|
| 4 |
+
from mcp import ClientSession, StdioServerParameters
|
| 5 |
+
from mcp.client.stdio import stdio_client
|
| 6 |
+
from mcp.client.sse import sse_client
|
| 7 |
+
from contextlib import AsyncExitStack
|
| 8 |
+
|
| 9 |
+
async def connect_to_stdio_server() -> None:
|
| 10 |
+
"""Connect to a single MCP server."""
|
| 11 |
+
|
| 12 |
+
sessions: List[ClientSession] = []
|
| 13 |
+
exit_stack = AsyncExitStack()
|
| 14 |
+
|
| 15 |
+
try:
|
| 16 |
+
server_params = StdioServerParameters(
|
| 17 |
+
command="npx",
|
| 18 |
+
args=[
|
| 19 |
+
"mcp-remote",
|
| 20 |
+
"https://jarisko-mcp-server-deployment-sample.hf.space/gradio_api/mcp/sse",
|
| 21 |
+
"--transport",
|
| 22 |
+
"sse-only"
|
| 23 |
+
]
|
| 24 |
+
)
|
| 25 |
+
|
| 26 |
+
stdio_transport = await exit_stack.enter_async_context(
|
| 27 |
+
stdio_client(server_params)
|
| 28 |
+
)
|
| 29 |
+
|
| 30 |
+
read, write = stdio_transport
|
| 31 |
+
session = await exit_stack.enter_async_context(
|
| 32 |
+
ClientSession(read, write)
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
await session.initialize()
|
| 36 |
+
sessions.append(session)
|
| 37 |
+
|
| 38 |
+
print("3")
|
| 39 |
+
|
| 40 |
+
# List available tools for this session
|
| 41 |
+
response = await session.list_tools()
|
| 42 |
+
tools = response.tools
|
| 43 |
+
print(f"{tools=}")
|
| 44 |
+
|
| 45 |
+
except Exception as e:
|
| 46 |
+
print(f"Failed to connect: {e}")
|
| 47 |
+
|
| 48 |
+
|
| 49 |
+
async def connect_to_sse_server():
|
| 50 |
+
|
| 51 |
+
async with sse_client("https://jarisko-mcp-server-deployment-sample.hf.space/gradio_api/mcp/sse") as streams:
|
| 52 |
+
async with ClientSession(*streams) as session:
|
| 53 |
+
await session.initialize()
|
| 54 |
+
tools_result = await session.list_tools()
|
| 55 |
+
print(f"{tools_result=}")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
asyncio.run(connect_to_sse_server())
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
mcp[cli]
|
| 2 |
-
gradio[mcp]
|
|
|
|
|
|
|
|
|
| 1 |
mcp[cli]
|
| 2 |
+
gradio[mcp]
|
| 3 |
+
smolagents[mcp]
|
| 4 |
+
textblob
|
test.ipynb
CHANGED
|
@@ -3,11 +3,33 @@
|
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
"execution_count": null,
|
| 6 |
-
"id": "
|
| 7 |
"metadata": {},
|
| 8 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
"source": [
|
| 10 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
]
|
| 12 |
}
|
| 13 |
],
|
|
@@ -18,7 +40,15 @@
|
|
| 18 |
"name": "python3"
|
| 19 |
},
|
| 20 |
"language_info": {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"name": "python",
|
|
|
|
|
|
|
| 22 |
"version": "3.11.6"
|
| 23 |
}
|
| 24 |
},
|
|
|
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
"execution_count": null,
|
| 6 |
+
"id": "5eff6c71",
|
| 7 |
"metadata": {},
|
| 8 |
+
"outputs": [
|
| 9 |
+
{
|
| 10 |
+
"name": "stderr",
|
| 11 |
+
"output_type": "stream",
|
| 12 |
+
"text": [
|
| 13 |
+
"c:\\Users\\jaros\\Documents\\Python\\mcp_server_deployment_sample\\.venv\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 14 |
+
" from .autonotebook import tqdm as notebook_tqdm\n",
|
| 15 |
+
"Error in post_writer: Client error '404 Not Found' for url 'http://127.0.0.1:7860/gradio_api/mcp/gradio_api/mcp/messages/?session_id=da1cf9f12be04584ba0d3f8cbf880871'\n",
|
| 16 |
+
"For more information check: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/404\n"
|
| 17 |
+
]
|
| 18 |
+
}
|
| 19 |
+
],
|
| 20 |
"source": [
|
| 21 |
+
"import gradio as gr\n",
|
| 22 |
+
"\n",
|
| 23 |
+
"from mcp.client.stdio import StdioServerParameters\n",
|
| 24 |
+
"from smolagents import ToolCollection, CodeAgent\n",
|
| 25 |
+
"from smolagents import CodeAgent, InferenceClientModel\n",
|
| 26 |
+
"from smolagents.mcp_client import MCPClient\n",
|
| 27 |
+
"\n",
|
| 28 |
+
"mcp_client = MCPClient(\n",
|
| 29 |
+
" {\"url\": \"http://127.0.0.1:7860/gradio_api/mcp/sse\"}\n",
|
| 30 |
+
")\n",
|
| 31 |
+
"tools = mcp_client.get_tools()\n",
|
| 32 |
+
"print(tools)"
|
| 33 |
]
|
| 34 |
}
|
| 35 |
],
|
|
|
|
| 40 |
"name": "python3"
|
| 41 |
},
|
| 42 |
"language_info": {
|
| 43 |
+
"codemirror_mode": {
|
| 44 |
+
"name": "ipython",
|
| 45 |
+
"version": 3
|
| 46 |
+
},
|
| 47 |
+
"file_extension": ".py",
|
| 48 |
+
"mimetype": "text/x-python",
|
| 49 |
"name": "python",
|
| 50 |
+
"nbconvert_exporter": "python",
|
| 51 |
+
"pygments_lexer": "ipython3",
|
| 52 |
"version": "3.11.6"
|
| 53 |
}
|
| 54 |
},
|