docs-mcp / app.py
aliabd's picture
aliabd HF Staff
working
f5a6b38 verified
raw
history blame contribute delete
2.48 kB
import gradio as gr
import requests
def load_gradio_docs():
"""
Get Gradio's full documentation, example demos, and useful context before answering questions or generating code. Should be used for general questions about core concepts and features of Gradio.
Returns:
str: Gradio's full documentation, example demos, and useful context.
"""
try:
response = requests.get("https://gradio.app/llms.txt")
text = response.text
return text
except Exception as error:
print(f"Error fetching document: {error}")
return f"Error fetching document: {str(error)}"
def search_gradio_docs(query):
"""
Run embedding search on Gradio's documentation, guides and demos and return the most relevant useful context before answering questions or generating code. This tool should be used when the user's question is specific to a Gradio demo, docs, or guide. The query should be concise and straightforward, ie: 'how to use the image component' or 'audio component demo', and should not include the word 'Gradio'.
Args:
query (str): The query to search for
Returns:
str: The search results
"""
response = requests.post(
"https://playground-worker.pages.dev/api/prompt",
headers={
"Content-Type": "application/json",
"Origin": "https://gradio.app"
},
json={
"prompt_to_embed": query,
"SYSTEM_PROMPT": "$INSERT_GUIDES_DOCS_DEMOS",
"FALLBACK_PROMPT": "No results found"
}
)
res = response.json()
return res["SYS_PROMPT"]
css = """
#search_results {
background-color: #ffebc3;
padding: 10px;
border-radius: 5px;
}
"""
with gr.Blocks(css=css) as demo:
gr.HTML("<center><h1>Gradio Documentation Search</h1></center>")
with gr.Row():
with gr.Column():
load_button = gr.Button("Load Gradio Docs llms.txt")
gr.HTML("<center><h2>OR</h2></center>")
query = gr.Textbox(label="Enter your search query")
search_button = gr.Button("Search by Query")
with gr.Column():
gr.Markdown("### Search results:")
output = gr.Markdown(elem_id="search_results")
load_button.click(load_gradio_docs, outputs=output)
search_button.click(search_gradio_docs, inputs=query, outputs=output)
if __name__ == "__main__":
demo.launch(mcp_server=True, strict_cors=False)