springwater's picture
Update app.py
2819be2 verified
raw
history blame
2.6 kB
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()