|
from youtubesearchpython import VideosSearch, Transcript |
|
import gradio as gr |
|
import openai |
|
import os |
|
|
|
|
|
|
|
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, OPENAI_API_KEY): |
|
|
|
response = openai.ChatCompletion.create( |
|
model="gpt-4-turbo-preview", |
|
messages=[ |
|
{"role": "system", "content": "You are a helpful assistant."}, |
|
{"role": "user", "content": f"Summarize this: {contents}"} |
|
], |
|
headers={ |
|
"Content-Type": "application/json", |
|
"Authorization": f"Bearer {OPENAI_API_KEY}" |
|
} |
|
) |
|
return response.choices[0].message['content'].strip() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def summarize_youtube_videos(keyword): |
|
urls = search_youtube_videos(keyword) |
|
contents = get_transcript(urls) |
|
summary = summarize_text(contents) |
|
return summary |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iface = gr.Interface( |
|
inputs=[ |
|
gr.Textbox(label="keyword", placeholder='ํค์๋๋ฅผ ์
๋ ฅํ์ธ์. (์.๋นํธ์ฝ์ธ)'), |
|
gr.Textbox(label="OpenAI API ํค", placeholder="์ฌ๊ธฐ์ OpenAI API ํค๋ฅผ ์
๋ ฅํ์ธ์") |
|
], |
|
outputs=gr.JSON(label='์ ํ๋ธ 5๊ฐ ์์ฝ ๊ฒฐ๊ณผ'), |
|
title="Summarize YouTube Videos", |
|
description="Enter a keyword to summarize related YouTube videos." |
|
) |
|
|
|
def summarize_button_click(keyword, OPENAI_API_KEY): |
|
summary = summarize_youtube_videos(keyword, OPENAI_API_KEY) |
|
return summary |
|
|
|
iface.button("submit", summarize_button_click) |
|
|
|
|
|
iface.launch() |
|
|