springwater commited on
Commit
2819be2
Β·
verified Β·
1 Parent(s): 3121ba2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -14
app.py CHANGED
@@ -1,24 +1,81 @@
1
- from youtubesearchpython import VideosSearch
2
  import gradio as gr
 
 
 
 
 
3
 
4
- def search_youtube_videos(keyword, limit=5):
5
- videos_search = VideosSearch(keyword, limit=limit)
6
- results = videos_search.result()
7
 
 
 
 
8
  video_urls = [video['link'] for video in results['result']]
9
  return video_urls
10
 
11
- def gradio_interface(keyword):
12
- video_urls = search_youtube_videos(keyword, limit=5, order='date')
13
- return "\n".join(video_urls)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
 
15
  interface = gr.Interface(
16
- fn=gradio_interface,
17
- inputs=gr.Textbox(label="검색 ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•˜μ„Έμš”"),
18
- outputs=gr.Textbox(label="κ²€μƒ‰λœ 유튜브 λ™μ˜μƒ URL"),
19
- title="유튜브 검색 λ„μš°λ―Έ",
20
- description="ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•˜λ©΄ μœ νŠœλΈŒμ—μ„œ ν•΄λ‹Ή ν‚€μ›Œλ“œλ‘œ κ²€μƒ‰ν•œ ν›„ λ™μ˜μƒ URL을 λ³΄μ—¬μ€λ‹ˆλ‹€."
21
  )
22
 
23
- if __name__ == "__main__":
24
- interface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
+ from youtubesearchpython import VideosSearch
3
+ import whisper
4
+ import openai
5
+ import os
6
+ from pytube import YouTube
7
 
8
+ # OpenAI API ν‚€ μ„€μ •
9
+ openai.api_key = '여기에 OpenAI API ν‚€ μž…λ ₯'
 
10
 
11
+ def search_youtube_videos(keyword, limit=5, order='date'):
12
+ videos_search = VideosSearch(keyword, limit=limit, order=order)
13
+ results = videos_search.result()
14
  video_urls = [video['link'] for video in results['result']]
15
  return video_urls
16
 
17
+ def download_audio_from_youtube(url, output_path="downloaded_audio.mp4"):
18
+ yt = YouTube(url)
19
+ audio_stream = yt.streams.get_audio_only()
20
+ audio_stream.download(output_filename=output_path)
21
+ return output_path
22
+
23
+ def convert_audio_to_text(audio_path):
24
+ model = whisper.load_model("base")
25
+ result = model.transcribe(audio_path)
26
+ return result["text"]
27
+
28
+ def summarize_text(text):
29
+ response = openai.Completion.create(
30
+ engine="text-davinci-003",
31
+ prompt=f"μš”μ•½ν•΄μ€˜: {text}",
32
+ max_tokens=150
33
+ )
34
+ return response.choices[0].text.strip()
35
+
36
+ def process_keyword(keyword):
37
+ urls = search_youtube_videos(keyword)
38
+ summaries = []
39
+ for url in urls[:1]: # 데λͺ¨λ₯Ό μœ„ν•΄ 첫 번째 URL만 처리
40
+ audio_path = download_audio_from_youtube(url)
41
+ text = convert_audio_to_text(audio_path)
42
+ summary = summarize_text(text)
43
+ summaries.append(summary)
44
+ os.remove(audio_path) # μž„μ‹œ μ˜€λ””μ˜€ 파일 μ‚­μ œ
45
+ return summaries
46
 
47
+ # Gradio μΈν„°νŽ˜μ΄μŠ€
48
  interface = gr.Interface(
49
+ fn=process_keyword,
50
+ inputs=gr.Textbox(label="검색 ν‚€μ›Œλ“œ"),
51
+ outputs=gr.Textbox(label="μš”μ•½"),
 
 
52
  )
53
 
54
+ interface.launch()
55
+
56
+
57
+ # from youtubesearchpython import VideosSearch
58
+ # from langchain_community.document_loaders import YoutubeLoader
59
+ # import gradio as gr
60
+
61
+ # def search_youtube_videos(keyword, limit=5, order='date'):
62
+ # videos_search = VideosSearch(keyword, limit=limit, order='date')
63
+ # results = videos_search.result()
64
+
65
+ # video_urls = [video['link'] for video in results['result']]
66
+ # return video_urls
67
+
68
+ # def gradio_interface(keyword):
69
+ # video_urls = search_youtube_videos(keyword)
70
+ # return "\n".join(video_urls)
71
+
72
+ # interface = gr.Interface(
73
+ # fn=gradio_interface,
74
+ # inputs=gr.Textbox(label="검색 ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•˜μ„Έμš”"),
75
+ # outputs=gr.Textbox(label="κ²€μƒ‰λœ 유튜브 λ™μ˜μƒ URL"),
76
+ # title="유튜브 검색 λ„μš°λ―Έ",
77
+ # description="ν‚€μ›Œλ“œλ₯Ό μž…λ ₯ν•˜λ©΄ μœ νŠœλΈŒμ—μ„œ ν•΄λ‹Ή ν‚€μ›Œλ“œλ‘œ κ²€μƒ‰ν•œ ν›„ λ™μ˜μƒ URL을 λ³΄μ—¬μ€λ‹ˆλ‹€."
78
+ # )
79
+
80
+ # if __name__ == "__main__":
81
+ # interface.launch()