Tesaan commited on
Commit
a9797ee
ยท
verified ยท
1 Parent(s): 5c8aed3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -20
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='image', num=10).execute()
13
 
14
- # ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ์—์„œ ์›น ๋งํฌ์™€ ์ธ๋„ค์ผ ์ด๋ฏธ์ง€๋ฅผ ์ถ”์ถœํ•ฉ๋‹ˆ๋‹ค.
15
  results = []
16
  if 'items' in res:
17
  for item in res['items']:
18
- results.append({
19
- 'title': item['title'],
20
- 'link': item['link'],
21
- 'thumbnail': item.get('image', {}).get('thumbnailLink', 'No thumbnail available')
22
- })
 
 
 
 
 
 
 
23
  return results
24
 
25
- def format_output(results):
26
- # ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ Gradio ์ธํ„ฐํŽ˜์ด์Šค์— ๋งž๊ฒŒ ํฌ๋งทํŒ…ํ•ฉ๋‹ˆ๋‹ค.
27
  output = []
28
  for result in results:
29
- output.append(f"**{result['title']}**\nURL: [{result['link']}]({result['link']})\n![Thumbnail]({result['thumbnail']})")
 
 
 
 
30
  return "\n\n".join(output)
31
 
32
- # Gradio ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
 
 
 
33
  iface = gr.Interface(
34
- fn=lambda query: format_output(search_google(query)),
35
- inputs=gr.Textbox(placeholder="๊ฒ€์ƒ‰ํ•  ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”..."),
36
  outputs=gr.Markdown(),
37
- examples=["Python", "Gradio", "Hugging Face"],
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()