Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import requests | |
| import json | |
| from smolagents import CodeAgent | |
| import logging | |
| from tenacity import retry, stop_after_attempt, wait_exponential | |
| # Configure logging | |
| logging.basicConfig(level=logging.INFO) | |
| logger = logging.getLogger(__name__) | |
| HF_SPACE_URL = "https://manavraj-troubleshoot-mcp.hf.space" | |
| def call_mcp_server(message: str, tool_type: str = "knowledge_base") -> str: | |
| """Call MCP server endpoint""" | |
| try: | |
| endpoint = f"{HF_SPACE_URL}/gradio_api/mcp/sse" | |
| response = requests.post( | |
| endpoint, | |
| json={ | |
| "tool": tool_type, | |
| "input": message | |
| }, | |
| stream=True, | |
| timeout=30 | |
| ) | |
| response.raise_for_status() | |
| for line in response.iter_lines(): | |
| if line.startswith(b'data:'): | |
| return json.loads(line[5:])["output"] | |
| return "No response received from MCP server" | |
| except Exception as e: | |
| logger.error(f"API call failed: {str(e)}") | |
| return f"Error: {str(e)}" | |
| agent = CodeAgent( | |
| tools=[], | |
| model="microsoft/DialoGPT-medium", | |
| system_prompt="""{{authorized_imports}} | |
| - requests for API calls | |
| - standard Python libraries | |
| {{managed_agents_descriptions}} | |
| You are a Technical Support Assistant with these capabilities: | |
| 1. Troubleshooting technical issues | |
| 2. Finding information via web search | |
| 3. Formatting instructions | |
| Access tools through MCP server: | |
| - knowledge_base: For technical issues | |
| - web_search: For information lookup | |
| - formatter: To organize steps | |
| Response workflow: | |
| 1. Analyze user request | |
| 2. Choose appropriate tool | |
| 3. Return clear response | |
| Example: | |
| USER: My wifi disconnected | |
| THOUGHT: Should use knowledge_base | |
| ACTION: knowledge_base("wifi disconnection") | |
| RESPONSE: Try these steps: [solution steps] | |
| Never show internal workflow to user.""" | |
| ) | |
| def chat_interface(message: str, history: list) -> str: | |
| """Handle chat interaction""" | |
| try: | |
| response = agent.run(message) | |
| if "ACTION:" in response: | |
| tool_call = response.split("ACTION:")[1].split("(")[0].strip() | |
| input_text = response.split("(")[1].split(")")[0].strip('"') | |
| return call_mcp_server(input_text, tool_call) | |
| return response | |
| except Exception as e: | |
| logger.error(f"Chat error: {str(e)}") | |
| return f"Error: {str(e)}" | |
| demo = gr.ChatInterface( | |
| fn=chat_interface, | |
| title="🔧 Technical Support", | |
| examples=["Wifi not working", "Find Windows 11 specs", "Format: Turn off. Wait. Restart"] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch() |