Spaces:
Runtime error
Runtime error
import gradio as gr | |
from duckduckgo_search import DDGS | |
# Simulated knowledge base | |
TROUBLESHOOTING_KB = { | |
"wifi not working": [ | |
"Ensure your router is powered on.", | |
"Restart your router.", | |
"Verify your device is connected to the correct Wi-Fi network.", | |
"Attempt to connect another device to determine if the issue is device-specific." | |
], | |
"computer is slow": [ | |
"Close unnecessary applications.", | |
"Run a virus scan using your antivirus software.", | |
"Restart your computer.", | |
"Check for and install available system updates." | |
] | |
} | |
# Tool: search_knowledge_base | |
def search_knowledge_base(problem: str) -> list[str]: | |
problem = problem.lower() | |
for key in TROUBLESHOOTING_KB: | |
if key in problem: | |
return TROUBLESHOOTING_KB[key] | |
return ["No matching solution found in the knowledge base."] | |
# Tool: search_web | |
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: format_response | |
def format_response(text: str) -> str: | |
return f"🛠️ Troubleshooting Steps:\n{text}" | |
# Create Gradio Interfaces for each tool | |
kb_interface = gr.Interface( | |
fn=search_knowledge_base, | |
inputs=gr.Textbox(label="Describe your problem"), | |
outputs=gr.JSON(label="Knowledge Base Steps"), | |
title="Knowledge Base Search", | |
description="Search for troubleshooting steps in the internal knowledge base." | |
) | |
web_interface = gr.Interface( | |
fn=search_web, | |
inputs=gr.Textbox(label="Enter search query"), | |
outputs=gr.JSON(label="Web Search Results"), | |
title="Web Search", | |
description="Perform a web search for additional troubleshooting information." | |
) | |
format_interface = gr.Interface( | |
fn=format_response, | |
inputs=gr.Textbox(label="Response Text"), | |
outputs=gr.Textbox(label="Formatted Response"), | |
title="Response Formatter", | |
description="Format the troubleshooting steps for better readability." | |
) | |
# Launch the interfaces with MCP server enabled | |
if __name__ == "__main__": | |
kb_interface.launch(mcp_server=True) | |
web_interface.launch(mcp_server=True) | |
format_interface.launch(mcp_server=True) | |