ShawhinT commited on
Commit
78dbf5e
·
1 Parent(s): e19ecf2

adding app files

Browse files
Files changed (2) hide show
  1. app.py +22 -0
  2. functions.py +85 -0
app.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from functions import *
3
+
4
+ # demo
5
+ output_list = []
6
+
7
+ with gr.Blocks() as demo:
8
+ gr.Markdown("# YouTube Search")
9
+
10
+ with gr.Row():
11
+ inp = gr.Textbox(placeholder="What are you looking for?", label="Query", scale=3)
12
+ btn = gr.Button("Search")
13
+ btn.click(fn=searchResults, inputs=inp, outputs=output_list)
14
+
15
+ for i in range(5):
16
+ with gr.Row():
17
+ output_list.append(gr.HTML())
18
+ output_list.append(gr.Markdown())
19
+
20
+ inp.submit(fn=searchResults, inputs=inp, outputs=output_list)
21
+
22
+ demo.launch()
functions.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import requests
2
+ import json
3
+ import gradio as gr
4
+ import os
5
+
6
+ def callSearchAPI(query: str) -> dict:
7
+ """
8
+ Call search API hosted on AWS
9
+ """
10
+
11
+ # hardcode public url of api
12
+ url = os.getenv('GCR_API_URL')
13
+
14
+ # make get request
15
+ params = {"query": query}
16
+ response = requests.get(url, params=params)
17
+
18
+ # return response as python dict
19
+ return json.loads(response.text)
20
+
21
+ def formatResultText(title: str, video_id: str):
22
+ """
23
+ Function to format video title and id as markdown
24
+ """
25
+
26
+ text = f"""<br> <br>
27
+ # {title}<br>
28
+
29
+ 🔗 [Video Link](https://youtu.be/{video_id})"""
30
+
31
+ return text
32
+
33
+ def formatVideoEmbed(video_id: str):
34
+ """
35
+ Function to generate video embed from video_id
36
+ """
37
+
38
+ return '<iframe width="576" height="324" src="https://www.youtube.com/embed/'+ video_id +'"></iframe>'
39
+
40
+ def searchResults(query):
41
+ """
42
+ Function to update search outputs for gradio ui
43
+ """
44
+
45
+ # API call
46
+ response = callSearchAPI(query)
47
+
48
+ # format search results
49
+
50
+ # initialize list of outputs
51
+ output_list = []
52
+
53
+ # compute number of null search results (out of 5)
54
+ num_empty_results = 5-len(response['title'])
55
+
56
+ # display search results
57
+ for i in range(len(response['title'])):
58
+ video_id = response['video_id'][i]
59
+ title = response['title'][i]
60
+
61
+ embed = gr.HTML(value = formatVideoEmbed(video_id), visible=True)
62
+ text = gr.Markdown(value = formatResultText(title, video_id), visible=True)
63
+
64
+ output_list.append(embed)
65
+ output_list.append(text)
66
+
67
+ # make null search result slots invisible
68
+ for i in range(num_empty_results):
69
+
70
+ # if no search results display "No results." text
71
+ if num_empty_results==5 and i==0:
72
+ embed = gr.HTML(visible=False)
73
+ text = gr.Markdown(value = "No results. Try rephrasing your query.", visible=True)
74
+
75
+ output_list.append(embed)
76
+ output_list.append(text)
77
+ continue
78
+
79
+ embed = gr.HTML(visible=False)
80
+ text = gr.Markdown(visible=False)
81
+
82
+ output_list.append(embed)
83
+ output_list.append(text)
84
+
85
+ return output_list