Spaces:
Runtime error
Runtime error
import gradio as gr | |
from duckduckgo_search import DDGS | |
# Tool 1: Knowledge Base Search | |
def search_knowledge_base(problem: str) -> list[str]: | |
problem = problem.lower() | |
KB = { | |
"wifi not working": [ | |
"Check if the router is on.", | |
"Restart the router.", | |
"Check if your device is connected to the right network." | |
], | |
"computer is slow": [ | |
"Close unused programs.", | |
"Run antivirus scan.", | |
"Restart the computer.", | |
"Update your OS." | |
] | |
} | |
for key in KB: | |
if key in problem: | |
return KB[key] | |
return ["No matching solution found in the knowledge base."] | |
# Tool 2: Web Search | |
def search_web(query: str) -> list[dict]: | |
results = [] | |
with DDGS() as ddgs: | |
for r in ddgs.text(query, max_results=3): | |
results.append({ | |
"title": r["title"], | |
"body": r["body"], | |
"href": r["href"] | |
}) | |
return results | |
# Tool 3: Format response | |
def format_response(text: str) -> str: | |
return f"🛠️ Suggested Steps:\n{text}" | |
# Combine tools into a single unified function | |
def toolbox(tool_selector, input_text): | |
if tool_selector == "search_knowledge_base": | |
return search_knowledge_base(input_text) | |
elif tool_selector == "search_web": | |
return search_web(input_text) | |
elif tool_selector == "format_response": | |
return format_response(input_text) | |
else: | |
return "Invalid tool selected." | |
# Define Interface | |
demo = gr.Interface( | |
fn=toolbox, | |
inputs=[ | |
gr.Dropdown( | |
["search_knowledge_base", "search_web", "format_response"], | |
label="Tool Selector" | |
), | |
gr.Textbox(label="Input Text") | |
], | |
outputs=gr.JSON(label="Output"), | |
title="Troubleshooting MCP Server", | |
description="Select a tool and provide input." | |
) | |
# Launch with MCP server enabled | |
if __name__ == "__main__": | |
demo.launch(mcp_server=True) |