Spaces:
Sleeping
Sleeping
Try to move mcp server to a different thread
Browse files
app.py
CHANGED
|
@@ -7,6 +7,7 @@ from mcp import Tool
|
|
| 7 |
from mcp.server import Server
|
| 8 |
from mcp.types import TextContent
|
| 9 |
import uvicorn
|
|
|
|
| 10 |
|
| 11 |
# ============================================================
|
| 12 |
# Suppress harmless asyncio warnings from Gradio
|
|
@@ -110,40 +111,54 @@ demo = gr.Interface(
|
|
| 110 |
]
|
| 111 |
)
|
| 112 |
|
| 113 |
-
# ============================================================
|
| 114 |
-
# Main async runner for MCP + Gradio
|
| 115 |
-
# ============================================================
|
| 116 |
-
async def main():
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
# Launch Gradio UI
|
| 127 |
-
demo.launch(
|
| 128 |
-
server_name="0.0.0.0",
|
| 129 |
-
server_port=8000,
|
| 130 |
-
prevent_thread_lock=True,
|
| 131 |
-
ssr_mode=False
|
| 132 |
-
)
|
| 133 |
-
print("main line 4")
|
| 134 |
-
|
| 135 |
-
# Keep MCP server alive
|
| 136 |
-
await mcp_task
|
| 137 |
-
print("main line 5")
|
| 138 |
|
| 139 |
-
#
|
| 140 |
-
#
|
| 141 |
-
#
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
from mcp.server import Server
|
| 8 |
from mcp.types import TextContent
|
| 9 |
import uvicorn
|
| 10 |
+
import threading
|
| 11 |
|
| 12 |
# ============================================================
|
| 13 |
# Suppress harmless asyncio warnings from Gradio
|
|
|
|
| 111 |
]
|
| 112 |
)
|
| 113 |
|
| 114 |
+
# # ============================================================
|
| 115 |
+
# # Main async runner for MCP + Gradio
|
| 116 |
+
# # ============================================================
|
| 117 |
+
# async def main():
|
| 118 |
+
# # Run MCP server
|
| 119 |
+
# print("main line 0")
|
| 120 |
+
# config = uvicorn.Config(mcp_asgi_app, host="0.0.0.0", port=7860, log_level="info")
|
| 121 |
+
# print("main line 1")
|
| 122 |
+
# server = uvicorn.Server(config)
|
| 123 |
+
# print("main line 2")
|
| 124 |
+
# mcp_task = asyncio.create_task(server.serve())
|
| 125 |
+
# print("main line 3")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# # Launch Gradio UI
|
| 128 |
+
# demo.launch(
|
| 129 |
+
# server_name="0.0.0.0",
|
| 130 |
+
# server_port=8000,
|
| 131 |
+
# prevent_thread_lock=True,
|
| 132 |
+
# ssr_mode=False
|
| 133 |
+
# )
|
| 134 |
+
# print("main line 4")
|
| 135 |
+
|
| 136 |
+
# # Keep MCP server alive
|
| 137 |
+
# await mcp_task
|
| 138 |
+
# print("main line 5")
|
| 139 |
+
|
| 140 |
+
# # ============================================================
|
| 141 |
+
# # Entry point
|
| 142 |
+
# # ============================================================
|
| 143 |
+
# if __name__ == "__main__":
|
| 144 |
+
# # print("=" * 60)
|
| 145 |
+
# # print("Starting Sentiment Analysis Services")
|
| 146 |
+
# # print("=" * 60)
|
| 147 |
+
# # print("🔌 MCP Server: http://0.0.0.0:7860/sse")
|
| 148 |
+
# # print("📊 Gradio UI : http://0.0.0.0:8000")
|
| 149 |
+
# # print("=" * 60)
|
| 150 |
+
# asyncio.run(main())
|
| 151 |
+
|
| 152 |
+
def run_mcp_server():
|
| 153 |
+
async def start():
|
| 154 |
+
config = uvicorn.Config(mcp_asgi_app, host="0.0.0.0", port=8000)
|
| 155 |
+
server = uvicorn.Server(config)
|
| 156 |
+
await server.serve()
|
| 157 |
+
|
| 158 |
+
loop = asyncio.new_event_loop()
|
| 159 |
+
asyncio.set_event_loop(loop)
|
| 160 |
+
loop.run_until_complete(start())
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
threading.Thread(target=run_mcp_server, daemon=True).start()
|
| 164 |
+
demo.launch(server_name="0.0.0.0", server_port=7860)
|