File size: 1,531 Bytes
49ea093
03bb178
145ad7d
bb2db31
49ea093
03bb178
 
bb2db31
03bb178
 
 
 
 
bb2db31
03bb178
 
 
 
bb2db31
49ea093
 
 
 
 
03bb178
 
 
 
 
 
 
 
bb2db31
ef26ce8
 
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

from googleapiclient.discovery import build
import gradio as gr

# API ํ‚ค์™€ ๊ฒ€์ƒ‰ ์—”์ง„ ID ์„ค์ •
API_KEY = 'AIzaSyDUz3wkGal0ewRtPlzeMit88bV4hS4ZIVY'
CSE_ID = '56b34994f47704ddd'

def multi_search(query):
    # Google Custom Search JSON API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์›น ๊ฒ€์ƒ‰
    service = build("customsearch", "v1", developerKey=API_KEY)
    web_search_result = service.cse().list(q=query, cx=CSE_ID).execute()
    web_results = [item['link'] for item in web_search_result.get('items', [])]

    # YouTube Data API๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋น„๋””์˜ค ๊ฒ€์ƒ‰
    youtube = build('youtube', 'v3', developerKey=API_KEY)
    video_search_result = youtube.search().list(q=query, part='snippet', type='video', maxResults=5).execute()
    video_results = [f"https://www.youtube.com/watch?v={item['id']['videoId']}" for item in video_search_result.get('items', [])]

    # ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ํŠœํ”Œ์˜ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ€ํ™˜
    web_results_tuples = [(url,) for url in web_results]
    video_results_tuples = [(url,) for url in video_results]

    return web_results_tuples, video_results_tuples

iface = gr.Interface(
    fn=multi_search,
    inputs=gr.Textbox(lines=2, placeholder="๊ฒ€์ƒ‰ํ•  ํ…์ŠคํŠธ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”..."),
    outputs=[gr.List(label="์›น ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ"), gr.List(label="YouTube ๋น„๋””์˜ค ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ")],
    title="๋ฉ€ํ‹ฐ ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ถœ๋ ฅ",
    description="์ž…๋ ฅ ํ…์ŠคํŠธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์›น ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์™€ YouTube ๋น„๋””์˜ค ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค."
)

if __name__ == "__main__":
    iface.launch()