|
from youtubesearchpython import VideosSearch, Transcript |
|
import gradio as gr |
|
import openai |
|
import os |
|
|
|
openai.api_key = os.getenv('O_API_KEY') |
|
|
|
def search_youtube_videos(keyword): |
|
videos_search = VideosSearch(keyword, limit=5) |
|
results = videos_search.result() |
|
video_urls = [video['link'] for video in results['result']] |
|
return video_urls |
|
|
|
|
|
def get_transcript(urls): |
|
contents = '' |
|
for url in urls: |
|
data = Transcript.get(url) |
|
text = "" |
|
for segment in data['segments']: |
|
text += segment['text'] |
|
contents += text |
|
return contents |
|
|
|
|
|
def summarize_text(contents): |
|
response = openai.ChatCompletion.create( |
|
engine="gpt-3.5-turbo-0125", |
|
prompt=f"μμ½: {contents}", |
|
max_tokens=150 |
|
) |
|
return response.choices[0].text.strip() |
|
|
|
|
|
def summarize_youtube_videos(keyword): |
|
urls = search_youtube_videos(keyword) |
|
contents = get_transcript(urls) |
|
summary = summarize_text(contents) |
|
return summary |
|
|
|
|
|
iface = gr.Interface( |
|
fn=summarize_youtube_videos, |
|
inputs="text", |
|
outputs="text", |
|
title="Summarize YouTube Videos", |
|
description="Enter a keyword to summarize related YouTube videos.", |
|
) |
|
|
|
|
|
iface.launch() |
|
|