aiqtest3 / app.py
seawolf2357's picture
Update app.py
49ea093 verified
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()