Spaces:
Running
Running
| """ | |
| Gradio Documentation MCP Server Integration | |
| DEVELOPMENT ONLY - DO NOT IMPORT IN MAIN APPLICATION | |
| This module provides access to live Gradio documentation via MCP server | |
| for local development assistance. It should NOT be imported in app.py or | |
| support_docs.py as it's not needed for the deployed application. | |
| Usage: | |
| python -c "from gradio_docs import gradio_docs; print(gradio_docs.search_docs('ChatInterface'))" | |
| """ | |
| import requests | |
| import json | |
| from typing import Optional, Dict, Any | |
| class GradioDocsClient: | |
| """Client for accessing Gradio documentation via MCP server""" | |
| def __init__(self, base_url: str = "https://gradio-docs-mcp.hf.space"): | |
| self.base_url = base_url.rstrip('/') | |
| self.session = requests.Session() | |
| self.session.headers.update({ | |
| 'User-Agent': 'ChatUI-Helper/1.0' | |
| }) | |
| def search_docs(self, query: str, limit: int = 5) -> Optional[Dict[str, Any]]: | |
| """ | |
| Search Gradio documentation for relevant information | |
| Args: | |
| query: Search query string | |
| limit: Maximum number of results to return | |
| Returns: | |
| Dictionary with search results or None if error | |
| """ | |
| try: | |
| # Try to use the API endpoint for search | |
| search_url = f"{self.base_url}/gradio_api" | |
| response = self.session.post( | |
| search_url, | |
| json={"query": query, "limit": limit}, | |
| timeout=10 | |
| ) | |
| if response.status_code == 200: | |
| return response.json() | |
| else: | |
| print(f"Gradio docs search failed: {response.status_code}") | |
| return None | |
| except requests.RequestException as e: | |
| print(f"Error connecting to Gradio docs server: {e}") | |
| return None | |
| def get_component_info(self, component_name: str) -> Optional[str]: | |
| """ | |
| Get information about a specific Gradio component | |
| Args: | |
| component_name: Name of the Gradio component (e.g., 'ChatInterface', 'Textbox') | |
| Returns: | |
| Documentation string or None if not found | |
| """ | |
| result = self.search_docs(f"gr.{component_name}") | |
| if result and "results" in result: | |
| # Extract relevant documentation from results | |
| docs = [] | |
| for item in result["results"][:3]: # Top 3 results | |
| if "content" in item: | |
| docs.append(item["content"]) | |
| return "\n\n".join(docs) if docs else None | |
| return None | |
| def get_latest_changes(self) -> Optional[str]: | |
| """ | |
| Get information about latest Gradio changes and updates | |
| Returns: | |
| Latest changes information or None if not available | |
| """ | |
| result = self.search_docs("changelog updates latest version") | |
| if result and "results" in result: | |
| changes = [] | |
| for item in result["results"][:2]: # Top 2 results | |
| if "content" in item: | |
| changes.append(item["content"]) | |
| return "\n\n".join(changes) if changes else None | |
| return None | |
| def is_available(self) -> bool: | |
| """ | |
| Check if the Gradio docs server is available | |
| Returns: | |
| True if server is accessible, False otherwise | |
| """ | |
| try: | |
| response = self.session.get(self.base_url, timeout=5) | |
| return response.status_code == 200 | |
| except requests.RequestException: | |
| return False | |
| # Global instance for easy access | |
| gradio_docs = GradioDocsClient() |