File size: 2,786 Bytes
c6582f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import requests
import json

def load_registry():
    """
    This function (load_registry) should always be called first, before using the load_doc function. It will load the registry file and return the content of the registry. Based on that, you will then be able to pick and chose the documentation you want to load.
    Returns:
        str: The content of the registry, that contains the ID, a description and the URL location for each item of the registry, in JSON format
    """
    try:
        with open('llms.json', 'r') as file:
            content = json.load(file)
            return "```json\n" + json.dumps(content, indent=2) + "\n```"
    except Exception as error:
        print(f"Error reading llms.json: {error}")
        return f"Error reading file: {str(error)}"

def load_doc(query):
    """
    After getting the registry with the load_registry function, you can use this function to load the documentation you want to use.
    
    Args:
        query (str): The documentation ID you want to load
    
    Returns:
        str: The documentation content
    """
    try:
        # Read the llms.json file
        with open('llms.json', 'r') as file:
            content = json.load(file)
            
        matching_entry = next((item for item in content if item['id'] == query), None)
        
        if matching_entry is None:
            return f"No documentation found for query: {query}"
            
        url = matching_entry['url']
        
        response = requests.get(url)
        response.raise_for_status()  # Raise an exception for bad status codes
        
        return response.text
        
    except FileNotFoundError:
        return "Error: registry file not found"
    except json.JSONDecodeError:
        return "Error: Invalid JSON format in registry"
    except requests.RequestException as e:
        return f"Error fetching content from URL: {str(e)}"
    except Exception as e:
        return f"An unexpected error occurred: {str(e)}"

css = """
#search_results {
    background-color: #ffebc3;
    padding: 10px;
    border-radius: 5px;
}
"""
with gr.Blocks(css=css) as demo:
    gr.HTML("<center><h1>Documentation registry</h1></center>")
    with gr.Row():
        with gr.Column():
            load_button = gr.Button("Load registry")
            gr.HTML("<center><h2>OR</h2></center>")
            query = gr.Textbox(label="Enter your registry ID")
            search_button = gr.Button("Search documentation by ID")

        with gr.Column():
            output = gr.Markdown(elem_id="search_results")

    load_button.click(load_registry, outputs=output)
    search_button.click(load_doc, inputs=query, outputs=output)

if __name__ == "__main__":
    demo.launch(mcp_server=True, strict_cors=False)