Manavraj commited on
Commit
7ee3d61
·
verified ·
1 Parent(s): 6ded4c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -12
app.py CHANGED
@@ -1,5 +1,6 @@
1
  import gradio as gr
2
  import requests
 
3
  from smolagents import CodeAgent
4
  import logging
5
  from tenacity import retry, stop_after_attempt, wait_exponential
@@ -14,10 +15,26 @@ HF_SPACE_URL = "https://manavraj-troubleshoot-mcp.hf.space"
14
  def call_mcp_server(message: str, tool_type: str = "knowledge_base") -> str:
15
  """Call MCP server endpoint"""
16
  try:
17
- endpoint = f"{HF_SPACE_URL}/api/{tool_type}"
18
- response = requests.post(endpoint, json={"data": [message]}, timeout=30)
 
 
 
 
 
 
 
 
 
 
 
19
  response.raise_for_status()
20
- return response.json()['data'][0]
 
 
 
 
 
21
  except Exception as e:
22
  logger.error(f"API call failed: {str(e)}")
23
  return f"Error: {str(e)}"
@@ -29,40 +46,49 @@ agent = CodeAgent(
29
  - requests for API calls
30
  - standard Python libraries
31
  {{managed_agents_descriptions}}
32
-
33
  You are a Technical Support Assistant with these capabilities:
34
  1. Troubleshooting technical issues
35
  2. Finding information via web search
36
  3. Formatting instructions
37
-
38
  Access tools through MCP server:
39
  - knowledge_base: For technical issues
40
  - web_search: For information lookup
41
  - formatter: To organize steps
42
-
43
  Response workflow:
44
  1. Analyze user request
45
  2. Choose appropriate tool
46
  3. Return clear response
47
-
48
  Example:
49
  USER: My wifi disconnected
50
  THOUGHT: Should use knowledge_base
51
  ACTION: knowledge_base("wifi disconnection")
52
  RESPONSE: Try these steps: [solution steps]
53
-
54
- Never show internal workflow to user."""
 
 
55
  )
56
 
57
  def chat_interface(message: str, history: list) -> str:
58
  """Handle chat interaction"""
59
  try:
 
 
 
 
 
 
60
  response = agent.run(message)
61
- final = str(response).split("RESPONSE:")[-1].strip()
62
- return final if final else "I couldn't process that request."
 
 
 
 
 
63
  except Exception as e:
64
  logger.error(f"Chat error: {str(e)}")
65
- return f"Error: {str(e)}"
66
 
67
  demo = gr.ChatInterface(
68
  fn=chat_interface,
 
1
  import gradio as gr
2
  import requests
3
+ import json
4
  from smolagents import CodeAgent
5
  import logging
6
  from tenacity import retry, stop_after_attempt, wait_exponential
 
15
  def call_mcp_server(message: str, tool_type: str = "knowledge_base") -> str:
16
  """Call MCP server endpoint"""
17
  try:
18
+ tool_endpoint_map = {
19
+ "knowledge_base": "predict_1",
20
+ "web_search": "predict_2",
21
+ "formatter": "predict_3"
22
+ }
23
+
24
+ endpoint = f"{HF_SPACE_URL}/gradio_api/call/{tool_endpoint_map[tool_type]}/"
25
+ response = requests.post(
26
+ endpoint,
27
+ json={"data": [message]},
28
+ timeout=30,
29
+ headers={"Content-Type": "application/json"}
30
+ )
31
  response.raise_for_status()
32
+
33
+ if response.text.startswith("event: predict"):
34
+ data = json.loads(response.text.split("\n")[1][5:])
35
+ return data["data"][0]
36
+
37
+ return response.json()["data"][0]
38
  except Exception as e:
39
  logger.error(f"API call failed: {str(e)}")
40
  return f"Error: {str(e)}"
 
46
  - requests for API calls
47
  - standard Python libraries
48
  {{managed_agents_descriptions}}
 
49
  You are a Technical Support Assistant with these capabilities:
50
  1. Troubleshooting technical issues
51
  2. Finding information via web search
52
  3. Formatting instructions
 
53
  Access tools through MCP server:
54
  - knowledge_base: For technical issues
55
  - web_search: For information lookup
56
  - formatter: To organize steps
 
57
  Response workflow:
58
  1. Analyze user request
59
  2. Choose appropriate tool
60
  3. Return clear response
 
61
  Example:
62
  USER: My wifi disconnected
63
  THOUGHT: Should use knowledge_base
64
  ACTION: knowledge_base("wifi disconnection")
65
  RESPONSE: Try these steps: [solution steps]
66
+ Important:
67
+ - Always return the full response including ACTION and RESPONSE
68
+ - Never show internal workflow to user
69
+ - If no tool is needed, respond directly"""
70
  )
71
 
72
  def chat_interface(message: str, history: list) -> str:
73
  """Handle chat interaction"""
74
  try:
75
+ tool_mapping = {
76
+ "knowledge_base": lambda x: call_mcp_server(x, "knowledge_base"),
77
+ "web_search": lambda x: call_mcp_server(x, "web_search"),
78
+ "formatter": lambda x: call_mcp_server(x, "formatter")
79
+ }
80
+
81
  response = agent.run(message)
82
+
83
+ if isinstance(response, str):
84
+ if "ACTION:" in response and "RESPONSE:" in response:
85
+ final = response.split("RESPONSE:")[-1].strip()
86
+ return final if final else "I couldn't process that request."
87
+ return response
88
+ return str(response)
89
  except Exception as e:
90
  logger.error(f"Chat error: {str(e)}")
91
+ return f"Error processing your request: {str(e)}"
92
 
93
  demo = gr.ChatInterface(
94
  fn=chat_interface,