File size: 2,203 Bytes
78dbf5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2e4a88e
78dbf5e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json
import gradio as gr
import os

def callSearchAPI(query: str) -> dict:
    """
        Call search API hosted on AWS
    """

    # hardcode public url of api
    url = os.getenv('GCR_API_URL')

    # make get request
    params = {"query": query}
    response = requests.get(url + "/search", params=params)

    # return response as python dict
    return json.loads(response.text)

def formatResultText(title: str, video_id: str):
    """
        Function to format video title and id as markdown
    """
    
    text = f"""<br> <br>
# {title}<br>

πŸ”— [Video Link](https://youtu.be/{video_id})"""

    return text

def formatVideoEmbed(video_id: str):
    """
        Function to generate video embed from video_id
    """
    
    return '<iframe width="576" height="324" src="https://www.youtube.com/embed/'+ video_id +'"></iframe>'

def searchResults(query):
    """
        Function to update search outputs for gradio ui
    """
    
    # API call
    response = callSearchAPI(query)

    # format search results

    # initialize list of outputs
    output_list = []

    # compute number of null search results (out of 5)
    num_empty_results = 5-len(response['title'])

    # display search results
    for i in range(len(response['title'])):
        video_id = response['video_id'][i]
        title = response['title'][i]

        embed = gr.HTML(value = formatVideoEmbed(video_id), visible=True)
        text = gr.Markdown(value = formatResultText(title, video_id), visible=True)

        output_list.append(embed)
        output_list.append(text)

    # make null search result slots invisible
    for i in range(num_empty_results):
        
        # if no search results display "No results." text
        if num_empty_results==5 and i==0:
            embed = gr.HTML(visible=False)
            text = gr.Markdown(value = "No results. Try rephrasing your query.", visible=True)

            output_list.append(embed)
            output_list.append(text)
            continue

        embed = gr.HTML(visible=False)
        text = gr.Markdown(visible=False)

        output_list.append(embed)
        output_list.append(text)
        
    return output_list