Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,44 +1,52 @@
|
|
1 |
from googleapiclient.discovery import build
|
2 |
import gradio as gr
|
3 |
|
4 |
-
# Google Cloud API ํค์ Custom Search Engine ID๋ฅผ ์ฌ๊ธฐ์ ์
๋ ฅํ์ธ์.
|
5 |
-
# ์ฃผ์: ์ค์ ํค์ ID๋ฅผ ๊ณต๊ฐ์ ์ผ๋ก ๊ณต์ ํ์ง ๋ง์ธ์.
|
6 |
API_KEY = 'AIzaSyDUz3wkGal0ewRtPlzeMit88bV4hS4ZIVY'
|
7 |
CSE_ID = '56b34994f47704ddd'
|
8 |
|
9 |
-
def search_google(query):
|
10 |
-
# Google Custom Search JSON API๋ฅผ ์ฌ์ฉํ์ฌ ๊ฒ์์ ์ํํฉ๋๋ค.
|
11 |
service = build("customsearch", "v1", developerKey=API_KEY)
|
12 |
-
res = service.cse().list(q=query, cx=CSE_ID, searchType=
|
13 |
|
14 |
-
# ๊ฒ์ ๊ฒฐ๊ณผ์์ ์น ๋งํฌ์ ์ธ๋ค์ผ ์ด๋ฏธ์ง๋ฅผ ์ถ์ถํฉ๋๋ค.
|
15 |
results = []
|
16 |
if 'items' in res:
|
17 |
for item in res['items']:
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
return results
|
24 |
|
25 |
-
def format_output(results):
|
26 |
-
# ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ Gradio ์ธํฐํ์ด์ค์ ๋ง๊ฒ ํฌ๋งทํ
ํฉ๋๋ค.
|
27 |
output = []
|
28 |
for result in results:
|
29 |
-
|
|
|
|
|
|
|
|
|
30 |
return "\n\n".join(output)
|
31 |
|
32 |
-
|
|
|
|
|
|
|
33 |
iface = gr.Interface(
|
34 |
-
fn=
|
35 |
-
inputs=gr.Textbox(placeholder="๊ฒ์ํ ํค์๋๋ฅผ ์
๋ ฅํ์ธ์..."),
|
36 |
outputs=gr.Markdown(),
|
37 |
-
examples=["Python", "
|
38 |
title="Google ๊ฒ์ ๊ฒฐ๊ณผ ๋ฐ ์ธ๋ค์ผ ๋ณด๊ธฐ",
|
39 |
-
description="์
๋ ฅํ ํค์๋์ ๋ํ ๊ตฌ๊ธ ๊ฒ์ ๊ฒฐ๊ณผ์ ์น ๋งํฌ์
|
40 |
)
|
41 |
|
42 |
-
# Gradio ์ฑ์ ์คํํฉ๋๋ค.
|
43 |
if __name__ == "__main__":
|
44 |
iface.launch()
|
|
|
1 |
from googleapiclient.discovery import build
|
2 |
import gradio as gr
|
3 |
|
|
|
|
|
4 |
API_KEY = 'AIzaSyDUz3wkGal0ewRtPlzeMit88bV4hS4ZIVY'
|
5 |
CSE_ID = '56b34994f47704ddd'
|
6 |
|
7 |
+
def search_google(query, search_type='image'):
|
|
|
8 |
service = build("customsearch", "v1", developerKey=API_KEY)
|
9 |
+
res = service.cse().list(q=query, cx=CSE_ID, searchType=search_type, num=10).execute()
|
10 |
|
|
|
11 |
results = []
|
12 |
if 'items' in res:
|
13 |
for item in res['items']:
|
14 |
+
if search_type == 'image':
|
15 |
+
results.append({
|
16 |
+
'title': item['title'],
|
17 |
+
'link': item['link'],
|
18 |
+
'thumbnail': item.get('image', {}).get('thumbnailLink', 'No thumbnail available')
|
19 |
+
})
|
20 |
+
elif search_type == 'video':
|
21 |
+
# ๋น๋์ค ๊ฒ์ ๊ฒฐ๊ณผ ์ฒ๋ฆฌ (๋น๋์ค ์ธ๋ค์ผ์ด๋ ๋งํฌ๊ฐ ๋ค๋ฅผ ์ ์์)
|
22 |
+
results.append({
|
23 |
+
'title': item['title'],
|
24 |
+
'link': item['link']
|
25 |
+
})
|
26 |
return results
|
27 |
|
28 |
+
def format_output(results, search_type='image'):
|
|
|
29 |
output = []
|
30 |
for result in results:
|
31 |
+
if search_type == 'image':
|
32 |
+
output.append(f"**{result['title']}**\nURL: [{result['link']}]({result['link']})\n![Thumbnail]({result['thumbnail']})")
|
33 |
+
elif search_type == 'video':
|
34 |
+
# ๋น๋์ค ๊ฒฐ๊ณผ ํฌ๋งทํ
|
35 |
+
output.append(f"**{result['title']}**\nURL: [{result['link']}]({result['link']})")
|
36 |
return "\n\n".join(output)
|
37 |
|
38 |
+
def search_and_format(query, search_type='image'):
|
39 |
+
return format_output(search_google(query, search_type), search_type)
|
40 |
+
|
41 |
+
# Gradio ์ธํฐํ์ด์ค ์ ์
|
42 |
iface = gr.Interface(
|
43 |
+
fn=search_and_format,
|
44 |
+
inputs=[gr.Textbox(placeholder="๊ฒ์ํ ํค์๋๋ฅผ ์
๋ ฅํ์ธ์..."), gr.Radio(choices=["image", "video"], label="๊ฒ์ ์ ํ")],
|
45 |
outputs=gr.Markdown(),
|
46 |
+
examples=[["Python", "image"], ["Gradio", "video"]],
|
47 |
title="Google ๊ฒ์ ๊ฒฐ๊ณผ ๋ฐ ์ธ๋ค์ผ ๋ณด๊ธฐ",
|
48 |
+
description="์
๋ ฅํ ํค์๋์ ๋ํ ๊ตฌ๊ธ ๊ฒ์ ๊ฒฐ๊ณผ์ ์น ๋งํฌ์ ์ธ๋ค์ผ(์ด๋ฏธ์ง) ๋๋ ๋น๋์ค ๋งํฌ๋ฅผ ๋ณด์ฌ์ค๋๋ค."
|
49 |
)
|
50 |
|
|
|
51 |
if __name__ == "__main__":
|
52 |
iface.launch()
|