Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,81 @@
|
|
1 |
-
from youtubesearchpython import VideosSearch
|
2 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
results = videos_search.result()
|
7 |
|
|
|
|
|
|
|
8 |
video_urls = [video['link'] for video in results['result']]
|
9 |
return video_urls
|
10 |
|
11 |
-
def
|
12 |
-
|
13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
|
|
15 |
interface = gr.Interface(
|
16 |
-
fn=
|
17 |
-
inputs=gr.Textbox(label="κ²μ
|
18 |
-
outputs=gr.Textbox(label="
|
19 |
-
title="μ νλΈ κ²μ λμ°λ―Έ",
|
20 |
-
description="ν€μλλ₯Ό μ
λ ₯νλ©΄ μ νλΈμμ ν΄λΉ ν€μλλ‘ κ²μν ν λμμ URLμ 보μ¬μ€λλ€."
|
21 |
)
|
22 |
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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()
|