File size: 3,736 Bytes
0a378f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
109
"""
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()