File size: 2,596 Bytes
8626295 2819be2 8626295 2819be2 8626295 2819be2 8626295 2819be2 8626295 2819be2 8626295 2819be2 8626295 2819be2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
import gradio as gr
from youtubesearchpython import VideosSearch
import whisper
import openai
import os
from pytube import YouTube
# OpenAI API ν€ μ€μ
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]: # λ°λͺ¨λ₯Ό μν΄ μ²« λ²μ§Έ URLλ§ μ²λ¦¬
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
# Gradio μΈν°νμ΄μ€
interface = gr.Interface(
fn=process_keyword,
inputs=gr.Textbox(label="κ²μ ν€μλ"),
outputs=gr.Textbox(label="μμ½"),
)
interface.launch()
# from youtubesearchpython import VideosSearch
# from langchain_community.document_loaders import YoutubeLoader
# import gradio as gr
# def search_youtube_videos(keyword, limit=5, order='date'):
# videos_search = VideosSearch(keyword, limit=limit, order='date')
# results = videos_search.result()
# video_urls = [video['link'] for video in results['result']]
# return video_urls
# def gradio_interface(keyword):
# video_urls = search_youtube_videos(keyword)
# return "\n".join(video_urls)
# interface = gr.Interface(
# fn=gradio_interface,
# inputs=gr.Textbox(label="κ²μ ν€μλλ₯Ό μ
λ ₯νμΈμ"),
# outputs=gr.Textbox(label="κ²μλ μ νλΈ λμμ URL"),
# title="μ νλΈ κ²μ λμ°λ―Έ",
# description="ν€μλλ₯Ό μ
λ ₯νλ©΄ μ νλΈμμ ν΄λΉ ν€μλλ‘ κ²μν ν λμμ URLμ 보μ¬μ€λλ€."
# )
# if __name__ == "__main__":
# interface.launch() |