|
import gradio as gr |
|
from youtubesearchpython import VideosSearch |
|
import whisper |
|
import openai |
|
import os |
|
from pytube import YouTube |
|
|
|
|
|
openai.api_key = 'μ¬κΈ°μ OpenAI API ν€ μ
λ ₯' |
|
|
|
def search_youtube_videos(keyword, limit=5, order='date'): |
|
videos_search = VideosSearch(keyword, limit=limit, order=order) |
|
results = videos_search.result() |
|
video_urls = [video['link'] for video in results['result']] |
|
return video_urls |
|
|
|
def download_audio_from_youtube(url, output_path="downloaded_audio.mp4"): |
|
yt = YouTube(url) |
|
audio_stream = yt.streams.get_audio_only() |
|
audio_stream.download(output_filename=output_path) |
|
return output_path |
|
|
|
def convert_audio_to_text(audio_path): |
|
model = whisper.load_model("base") |
|
result = model.transcribe(audio_path) |
|
return result["text"] |
|
|
|
def summarize_text(text): |
|
response = openai.Completion.create( |
|
engine="text-davinci-003", |
|
prompt=f"μμ½ν΄μ€: {text}", |
|
max_tokens=150 |
|
) |
|
return response.choices[0].text.strip() |
|
|
|
def process_keyword(keyword): |
|
urls = search_youtube_videos(keyword) |
|
summaries = [] |
|
for url in urls[:1]: |
|
audio_path = download_audio_from_youtube(url) |
|
text = convert_audio_to_text(audio_path) |
|
summary = summarize_text(text) |
|
summaries.append(summary) |
|
os.remove(audio_path) |
|
return summaries |
|
|
|
|
|
interface = gr.Interface( |
|
fn=process_keyword, |
|
inputs=gr.Textbox(label="κ²μ ν€μλ"), |
|
outputs=gr.Textbox(label="μμ½"), |
|
) |
|
|
|
interface.launch() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|