import os import urllib.parse import requests import gradio as gr from AinaTheme import theme SEARCH_TEMPLATE = "https://ca.wikipedia.org/w/api.php?action=opensearch&search=%s&limit=1&namespace=0&format=json" CONTENT_TEMPLATE = "https://ca.wikipedia.org/w/api.php?format=json&action=query&prop=extracts&exintro&explaintext&redirects=1&titles=%s" def search_wikipedia(query): query = urllib.parse.quote_plus(query) data = requests.get(SEARCH_TEMPLATE % query).json() if data and data[1]: page = urllib.parse.quote_plus(data[1][0]) content = requests.get(CONTENT_TEMPLATE % page).json() content = list(content["query"]["pages"].values())[0]["extract"] source = data[3][0] return content else: return "No results found.", "" def gradio_app(): with gr.Blocks(theme=theme) as demo: with gr.Row(equal_height=True): o_source = gr.Textbox( lines=10, label="Source", interactive=False, show_copy_button=True ) output = gr.Textbox( lines=10, label="Content", interactive=False, show_copy_button=True ) with gr.Row(equal_height=True): input_ = gr.Textbox( label="Input", placeholder="Buscar...", ) with gr.Row(equal_height=True): submit_btn = gr.Button("Submit", variant="primary") submit_btn.click( fn=search_wikipedia, inputs=[input_], outputs=[output], api_name="get-wikipedia-content" ) demo.launch(show_api=True) if __name__ == "__main__": gradio_app()