Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,69 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
if __name__ == "__main__":
|
9 |
-
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from duckduckgo_search import DDGS
|
3 |
|
4 |
+
# Tool 1: Knowledge Base Search
|
5 |
+
def search_knowledge_base(problem: str) -> list[str]:
|
6 |
+
problem = problem.lower()
|
7 |
+
KB = {
|
8 |
+
"wifi not working": [
|
9 |
+
"Check if the router is on.",
|
10 |
+
"Restart the router.",
|
11 |
+
"Check if your device is connected to the right network."
|
12 |
+
],
|
13 |
+
"computer is slow": [
|
14 |
+
"Close unused programs.",
|
15 |
+
"Run antivirus scan.",
|
16 |
+
"Restart the computer.",
|
17 |
+
"Update your OS."
|
18 |
+
]
|
19 |
+
}
|
20 |
+
for key in KB:
|
21 |
+
if key in problem:
|
22 |
+
return KB[key]
|
23 |
+
return ["No matching solution found in the knowledge base."]
|
24 |
|
25 |
+
# Tool 2: Web Search
|
26 |
+
def search_web(query: str) -> list[dict]:
|
27 |
+
results = []
|
28 |
+
with DDGS() as ddgs:
|
29 |
+
for r in ddgs.text(query, max_results=3):
|
30 |
+
results.append({
|
31 |
+
"title": r["title"],
|
32 |
+
"body": r["body"],
|
33 |
+
"href": r["href"]
|
34 |
+
})
|
35 |
+
return results
|
36 |
|
37 |
+
# Tool 3: Format response
|
38 |
+
def format_response(text: str) -> str:
|
39 |
+
return f"🛠️ Suggested Steps:\n{text}"
|
40 |
+
|
41 |
+
# Combine tools into a single unified function
|
42 |
+
def toolbox(tool_selector, input_text):
|
43 |
+
if tool_selector == "search_knowledge_base":
|
44 |
+
return search_knowledge_base(input_text)
|
45 |
+
elif tool_selector == "search_web":
|
46 |
+
return search_web(input_text)
|
47 |
+
elif tool_selector == "format_response":
|
48 |
+
return format_response(input_text)
|
49 |
+
else:
|
50 |
+
return "Invalid tool selected."
|
51 |
+
|
52 |
+
# Define Interface
|
53 |
+
demo = gr.Interface(
|
54 |
+
fn=toolbox,
|
55 |
+
inputs=[
|
56 |
+
gr.Dropdown(
|
57 |
+
["search_knowledge_base", "search_web", "format_response"],
|
58 |
+
label="Tool Selector"
|
59 |
+
),
|
60 |
+
gr.Textbox(label="Input Text")
|
61 |
+
],
|
62 |
+
outputs=gr.JSON(label="Output"),
|
63 |
+
title="Troubleshooting MCP Server",
|
64 |
+
description="Select a tool and provide input."
|
65 |
+
)
|
66 |
+
|
67 |
+
# Launch with MCP server enabled
|
68 |
if __name__ == "__main__":
|
69 |
+
demo.launch(mcp_server=True)
|