Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,219 +1,86 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import requests
|
| 3 |
-
from bs4 import BeautifulSoup
|
| 4 |
import json
|
| 5 |
-
from
|
| 6 |
-
import time
|
| 7 |
import logging
|
| 8 |
-
import
|
| 9 |
-
from smolagents.tools import tool
|
| 10 |
-
from fastapi import FastAPI
|
| 11 |
-
import uvicorn
|
| 12 |
|
| 13 |
# Configure logging
|
| 14 |
logging.basicConfig(level=logging.INFO)
|
| 15 |
logger = logging.getLogger(__name__)
|
| 16 |
|
| 17 |
-
|
| 18 |
|
| 19 |
-
@
|
| 20 |
-
def
|
| 21 |
-
"""
|
| 22 |
-
Search the knowledge base for solutions related to the provided issue.
|
| 23 |
-
Args:
|
| 24 |
-
issue (str): The technical issue to search for
|
| 25 |
-
Returns:
|
| 26 |
-
str: Solution steps or message if no solution found
|
| 27 |
-
"""
|
| 28 |
try:
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
return f"Error searching knowledge base: {str(e)}"
|
| 40 |
-
|
| 41 |
-
def search_web_duckduckgo(query: str) -> str:
|
| 42 |
-
"""
|
| 43 |
-
Perform actual web search using DuckDuckGo API.
|
| 44 |
-
Args:
|
| 45 |
-
query (str): The search query
|
| 46 |
-
Returns:
|
| 47 |
-
str: Formatted search results
|
| 48 |
-
"""
|
| 49 |
-
try:
|
| 50 |
-
url = f"https://api.duckduckgo.com/?q={quote_plus(query)}&format=json&no_html=1&skip_disambig=1"
|
| 51 |
-
|
| 52 |
-
response = requests.get(url, timeout=10)
|
| 53 |
response.raise_for_status()
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
if data.get('RelatedTopics'):
|
| 62 |
-
results.append("\nRelated Topics:")
|
| 63 |
-
for i, topic in enumerate(data['RelatedTopics'][:3], 1):
|
| 64 |
-
if isinstance(topic, dict) and topic.get('Text'):
|
| 65 |
-
results.append(f"{i}. {topic['Text']}")
|
| 66 |
-
elif hasattr(topic, 'Topics'):
|
| 67 |
-
for subtopic in topic['Topics'][:3]:
|
| 68 |
-
if subtopic.get('Text'):
|
| 69 |
-
results.append(f"{i}. {subtopic['Text']}")
|
| 70 |
-
|
| 71 |
-
if data.get('Definition'):
|
| 72 |
-
results.append(f"\nDefinition: {data['Definition']}")
|
| 73 |
-
|
| 74 |
-
if not results or len(''.join(results).strip()) < 20:
|
| 75 |
-
return search_web_scraper(query)
|
| 76 |
-
return "\n".join(results)
|
| 77 |
except Exception as e:
|
| 78 |
-
logger.error(f"
|
| 79 |
-
return
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
if results:
|
| 109 |
-
return f"Search Results for '{query}':\n" + "\n".join(results)
|
| 110 |
-
return f"No results found for '{query}'. Try different keywords."
|
| 111 |
-
except Exception as e:
|
| 112 |
-
logger.error(f"Web scraping error: {str(e)}")
|
| 113 |
-
return f"Error in backup search: {str(e)}"
|
| 114 |
-
|
| 115 |
-
@tool
|
| 116 |
-
def search_web(query: str) -> str:
|
| 117 |
-
"""
|
| 118 |
-
Main web search function that tries multiple methods.
|
| 119 |
-
Args:
|
| 120 |
-
query (str): The search query
|
| 121 |
-
Returns:
|
| 122 |
-
str: Formatted search results
|
| 123 |
-
"""
|
| 124 |
-
try:
|
| 125 |
-
if not query.strip():
|
| 126 |
-
return "Please enter a search query."
|
| 127 |
-
return search_web_duckduckgo(query)
|
| 128 |
-
except Exception as e:
|
| 129 |
-
logger.error(f"Error in main web search: {str(e)}")
|
| 130 |
-
return f"Error processing your search: {str(e)}"
|
| 131 |
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
"""
|
| 135 |
-
Format the raw steps into a numbered list.
|
| 136 |
-
Args:
|
| 137 |
-
raw_steps (str): Raw text steps separated by periods
|
| 138 |
-
Returns:
|
| 139 |
-
str: Formatted numbered list of steps
|
| 140 |
-
"""
|
| 141 |
try:
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
return "\n".join(steps)
|
| 149 |
except Exception as e:
|
| 150 |
-
logger.error(f"
|
| 151 |
-
return f"Error
|
| 152 |
-
|
| 153 |
-
# Create interfaces
|
| 154 |
-
demo1 = gr.Interface(
|
| 155 |
-
fn=search_knowledge_base,
|
| 156 |
-
inputs=[gr.Textbox(label="Technical Issue", placeholder="Enter your technical problem")],
|
| 157 |
-
outputs=[gr.Textbox(label="Solution")],
|
| 158 |
-
title="Knowledge Base Search"
|
| 159 |
-
)
|
| 160 |
|
| 161 |
-
|
| 162 |
-
fn=
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
title="Web Search"
|
| 166 |
)
|
| 167 |
|
| 168 |
-
demo3 = gr.Interface(
|
| 169 |
-
fn=format_response,
|
| 170 |
-
inputs=[gr.Textbox(label="Raw Steps", placeholder="Enter steps separated by periods")],
|
| 171 |
-
outputs=[gr.Textbox(label="Formatted Steps")],
|
| 172 |
-
title="Response Formatter"
|
| 173 |
-
)
|
| 174 |
-
|
| 175 |
-
demo = gr.TabbedInterface([demo1, demo2, demo3], ["Knowledge Base", "Web Search", "Formatter"])
|
| 176 |
-
|
| 177 |
-
# Add MCP schema endpoint
|
| 178 |
-
@app.get("/gradio_api/mcp/schema")
|
| 179 |
-
async def mcp_schema():
|
| 180 |
-
return {
|
| 181 |
-
"tools": [
|
| 182 |
-
{
|
| 183 |
-
"name": "search_knowledge_base",
|
| 184 |
-
"description": "Search knowledge base for technical solutions",
|
| 185 |
-
"parameters": {
|
| 186 |
-
"type": "object",
|
| 187 |
-
"properties": {
|
| 188 |
-
"issue": {"type": "string"}
|
| 189 |
-
}
|
| 190 |
-
}
|
| 191 |
-
},
|
| 192 |
-
{
|
| 193 |
-
"name": "search_web",
|
| 194 |
-
"description": "Perform web search",
|
| 195 |
-
"parameters": {
|
| 196 |
-
"type": "object",
|
| 197 |
-
"properties": {
|
| 198 |
-
"query": {"type": "string"}
|
| 199 |
-
}
|
| 200 |
-
}
|
| 201 |
-
},
|
| 202 |
-
{
|
| 203 |
-
"name": "format_response",
|
| 204 |
-
"description": "Format steps into numbered list",
|
| 205 |
-
"parameters": {
|
| 206 |
-
"type": "object",
|
| 207 |
-
"properties": {
|
| 208 |
-
"raw_steps": {"type": "string"}
|
| 209 |
-
}
|
| 210 |
-
}
|
| 211 |
-
}
|
| 212 |
-
]
|
| 213 |
-
}
|
| 214 |
-
|
| 215 |
-
# Mount Gradio app
|
| 216 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
| 217 |
-
|
| 218 |
if __name__ == "__main__":
|
| 219 |
-
|
|
|
|
| 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
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Configure logging
|
| 9 |
logging.basicConfig(level=logging.INFO)
|
| 10 |
logger = logging.getLogger(__name__)
|
| 11 |
|
| 12 |
+
HF_SPACE_URL = "https://manavraj-troubleshoot-mcp.hf.space"
|
| 13 |
|
| 14 |
+
@retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=4, max=10))
|
| 15 |
+
def call_mcp_server(message: str, tool_type: str = "knowledge_base") -> str:
|
| 16 |
+
"""Call MCP server endpoint"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
+
endpoint = f"{HF_SPACE_URL}/gradio_api/mcp/sse"
|
| 19 |
+
response = requests.post(
|
| 20 |
+
endpoint,
|
| 21 |
+
json={
|
| 22 |
+
"tool": tool_type,
|
| 23 |
+
"input": message
|
| 24 |
+
},
|
| 25 |
+
stream=True,
|
| 26 |
+
timeout=30
|
| 27 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
response.raise_for_status()
|
| 29 |
|
| 30 |
+
for line in response.iter_lines():
|
| 31 |
+
if line.startswith(b'data:'):
|
| 32 |
+
return json.loads(line[5:])["output"]
|
| 33 |
+
|
| 34 |
+
return "No response received from MCP server"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
+
logger.error(f"API call failed: {str(e)}")
|
| 37 |
+
return f"Error: {str(e)}"
|
| 38 |
|
| 39 |
+
agent = CodeAgent(
|
| 40 |
+
tools=[],
|
| 41 |
+
model="microsoft/DialoGPT-medium",
|
| 42 |
+
system_prompt="""{{authorized_imports}}
|
| 43 |
+
- requests for API calls
|
| 44 |
+
- standard Python libraries
|
| 45 |
+
{{managed_agents_descriptions}}
|
| 46 |
+
You are a Technical Support Assistant with these capabilities:
|
| 47 |
+
1. Troubleshooting technical issues
|
| 48 |
+
2. Finding information via web search
|
| 49 |
+
3. Formatting instructions
|
| 50 |
+
Access tools through MCP server:
|
| 51 |
+
- knowledge_base: For technical issues
|
| 52 |
+
- web_search: For information lookup
|
| 53 |
+
- formatter: To organize steps
|
| 54 |
+
Response workflow:
|
| 55 |
+
1. Analyze user request
|
| 56 |
+
2. Choose appropriate tool
|
| 57 |
+
3. Return clear response
|
| 58 |
+
Example:
|
| 59 |
+
USER: My wifi disconnected
|
| 60 |
+
THOUGHT: Should use knowledge_base
|
| 61 |
+
ACTION: knowledge_base("wifi disconnection")
|
| 62 |
+
RESPONSE: Try these steps: [solution steps]
|
| 63 |
+
Never show internal workflow to user."""
|
| 64 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
def chat_interface(message: str, history: list) -> str:
|
| 67 |
+
"""Handle chat interaction"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
try:
|
| 69 |
+
response = agent.run(message)
|
| 70 |
+
if "ACTION:" in response:
|
| 71 |
+
tool_call = response.split("ACTION:")[1].split("(")[0].strip()
|
| 72 |
+
input_text = response.split("(")[1].split(")")[0].strip('"')
|
| 73 |
+
return call_mcp_server(input_text, tool_call)
|
| 74 |
+
return response
|
|
|
|
| 75 |
except Exception as e:
|
| 76 |
+
logger.error(f"Chat error: {str(e)}")
|
| 77 |
+
return f"Error: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 78 |
|
| 79 |
+
demo = gr.ChatInterface(
|
| 80 |
+
fn=chat_interface,
|
| 81 |
+
title="🔧 Technical Support",
|
| 82 |
+
examples=["Wifi not working", "Find Windows 11 specs", "Format: Turn off. Wait. Restart"]
|
|
|
|
| 83 |
)
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
if __name__ == "__main__":
|
| 86 |
+
demo.launch()
|