File size: 1,801 Bytes
cce321f
 
 
 
c651b27
cce321f
6723527
 
cce321f
 
 
 
 
 
 
 
 
 
ca85d49
cce321f
 
 
06059e5
 
 
737d8de
06059e5
 
 
 
 
737d8de
06059e5
 
 
 
 
cce321f
06059e5
737d8de
06059e5
 
 
 
95c000d
737d8de
06059e5
 
 
 
 
ca85d49
06059e5
 
 
 
cce321f
 
 
06059e5
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
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()