Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -3,107 +3,71 @@ import requests
|
|
3 |
from smolagents import CodeAgent
|
4 |
import logging
|
5 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
6 |
-
from typing import Optional
|
7 |
|
8 |
# Configure logging
|
9 |
logging.basicConfig(level=logging.INFO)
|
10 |
logger = logging.getLogger(__name__)
|
11 |
|
12 |
-
# Correct URL based on your Space
|
13 |
HF_SPACE_URL = "https://manavraj-troubleshoot-mcp.hf.space"
|
14 |
|
15 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
16 |
def call_mcp_server(message: str, tool_type: str = "knowledge_base") -> str:
|
17 |
-
"""Call MCP server
|
18 |
try:
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
}
|
24 |
-
|
25 |
-
endpoint = endpoint_map.get(tool_type, "api/knowledge_base")
|
26 |
-
url = f"{HF_SPACE_URL}/{endpoint}"
|
27 |
-
|
28 |
-
response = requests.post(
|
29 |
-
url,
|
30 |
-
json={"data": [message]},
|
31 |
-
timeout=30
|
32 |
-
)
|
33 |
-
|
34 |
-
if response.status_code != 200:
|
35 |
-
raise Exception(f"MCP server returned {response.status_code}")
|
36 |
-
|
37 |
-
data = response.json()
|
38 |
-
if not isinstance(data, dict) or 'data' not in data:
|
39 |
-
raise Exception("Invalid MCP response format")
|
40 |
-
|
41 |
-
return str(data['data'][0])
|
42 |
-
|
43 |
except Exception as e:
|
44 |
-
logger.error(f"
|
45 |
return f"Error: {str(e)}"
|
46 |
|
47 |
-
# Initialize CodeAgent with proper system prompt including required tags
|
48 |
agent = CodeAgent(
|
49 |
-
tools=[
|
50 |
model="microsoft/DialoGPT-medium",
|
51 |
system_prompt="""{{authorized_imports}}
|
52 |
- requests for API calls
|
53 |
- standard Python libraries
|
54 |
{{managed_agents_descriptions}}
|
55 |
-
You are an advanced Technical Support Assistant with these capabilities:
|
56 |
-
1. Troubleshooting technical issues (wifi, hardware, software)
|
57 |
-
2. Finding information through web search
|
58 |
-
3. Formatting instructions into clear steps
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
4. Use formatter when user provides unorganized steps
|
70 |
-
5. Provide FINAL RESPONSE in clear, helpful language
|
71 |
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
FINAL RESPONSE: Here's how to fix your wifi: [steps]
|
77 |
|
78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
)
|
80 |
|
81 |
-
def chat_interface(message: str, history:
|
82 |
-
"""
|
83 |
try:
|
84 |
-
# Let agent handle the complete process
|
85 |
response = agent.run(message)
|
86 |
-
|
87 |
-
|
88 |
-
if isinstance(response, str):
|
89 |
-
if "FINAL RESPONSE:" in response:
|
90 |
-
return response.split("FINAL RESPONSE:")[-1].strip()
|
91 |
-
return response
|
92 |
-
return str(response)
|
93 |
except Exception as e:
|
94 |
logger.error(f"Chat error: {str(e)}")
|
95 |
-
return f"Error
|
96 |
|
97 |
-
# Gradio interface
|
98 |
demo = gr.ChatInterface(
|
99 |
fn=chat_interface,
|
100 |
-
title="🔧 Technical Support
|
101 |
-
examples=[
|
102 |
-
"My wifi keeps disconnecting",
|
103 |
-
"Find the latest Windows 11 update",
|
104 |
-
"Format: Restart. Check connections. Update drivers"
|
105 |
-
],
|
106 |
-
description="A technical support assistant that can troubleshoot issues, search the web, and format instructions."
|
107 |
)
|
108 |
|
109 |
if __name__ == "__main__":
|
|
|
3 |
from smolagents import CodeAgent
|
4 |
import logging
|
5 |
from tenacity import retry, stop_after_attempt, wait_exponential
|
|
|
6 |
|
7 |
# Configure logging
|
8 |
logging.basicConfig(level=logging.INFO)
|
9 |
logger = logging.getLogger(__name__)
|
10 |
|
|
|
11 |
HF_SPACE_URL = "https://manavraj-troubleshoot-mcp.hf.space"
|
12 |
|
13 |
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
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)}"
|
24 |
|
|
|
25 |
agent = CodeAgent(
|
26 |
+
tools=[],
|
27 |
model="microsoft/DialoGPT-medium",
|
28 |
system_prompt="""{{authorized_imports}}
|
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,
|
69 |
+
title="🔧 Technical Support",
|
70 |
+
examples=["Wifi not working", "Find Windows 11 specs", "Format: Turn off. Wait. Restart"]
|
|
|
|
|
|
|
|
|
|
|
71 |
)
|
72 |
|
73 |
if __name__ == "__main__":
|