Manavraj commited on
Commit
f4ccfcb
·
verified ·
1 Parent(s): aefe31c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -61
app.py CHANGED
@@ -1,69 +1,47 @@
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()
 
1
  import gradio as gr
 
2
 
3
+ # --- Tool: Simulated troubleshooting knowledge base ---
4
+ def search_knowledge_base(issue: str) -> str:
5
+ if "wifi" in issue.lower():
6
+ return "Check if your router is powered on. Restart your router. Try connecting again."
7
+ elif "screen" in issue.lower():
8
+ return "Adjust display cable. Update graphics drivers. Restart the system."
9
+ return "No predefined solution found in the knowledge base."
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ # --- Tool: Simulated web search (basic dummy) ---
12
+ def search_web(query: str) -> str:
13
+ return f"(Simulated Web Result) Top search result for '{query}'."
 
 
 
 
 
 
 
 
14
 
15
+ # --- Tool: Format output into step-by-step troubleshooting response ---
16
+ def format_response(raw_steps: str) -> str:
17
+ steps = raw_steps.split(". ")
18
+ steps = [f"{i+1}. {step.strip('.')}" for i, step in enumerate(steps) if step.strip()]
19
+ return "\n".join(steps)
20
 
21
+ # Register each tool with a separate interface
22
+ tools = [
23
+ gr.Interface(fn=search_knowledge_base,
24
+ inputs="text",
25
+ outputs="text",
26
+ title="Knowledge Base Search",
27
+ description="Search for predefined troubleshooting instructions.",
28
+ name="search_knowledge_base"),
 
 
29
 
30
+ gr.Interface(fn=search_web,
31
+ inputs="text",
32
+ outputs="text",
33
+ title="Web Search Tool",
34
+ description="Fetch dynamic info from the web (simulated).",
35
+ name="search_web"),
 
 
 
 
 
 
 
 
36
 
37
+ gr.Interface(fn=format_response,
38
+ inputs="text",
39
+ outputs="text",
40
+ title="Response Formatter",
41
+ description="Format the raw instructions into steps.",
42
+ name="format_response")
43
+ ]
44
+
45
+ # Launch all tools under one MCP server
46
  if __name__ == "__main__":
47
+ gr.mount_gradio_apps(tools).launch()