Spaces:
Runtime error
Runtime error
Download functions.py from shawhin/yt-search-ui: direct link, hf CLI and curl.
- Browser
- Download file 2.2 kB
-
https://huggingface.co/spaces/shawhin/yt-search-ui/resolve/main/functions.py
- Command line
-
hf download hf://spaces/shawhin/yt-search-ui/functions.py
-
curl -L -o functions.py https://huggingface.co/spaces/shawhin/yt-search-ui/resolve/main/functions.py
2.2 kB
| 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 |