|
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, 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( |
|
input = gr.Textbox(label="keyword", placeholder='ํค์๋๋ฅผ ์
๋ ฅํ์ธ์. (์.๋นํธ์ฝ์ธ)'), |
|
OPENAI_API_KEY = gr.Textbox(label="OpenAI API ํค", placeholder="์ฌ๊ธฐ์ OpenAI API ํค๋ฅผ ์
๋ ฅํ์ธ์"), |
|
output =gr.JSON(label='์ ํ๋ธ 5๊ฐ ์์ฝ ๊ฒฐ๊ณผ'), |
|
result = gr.Button('submit'), |
|
result.click( |
|
fn = summarize_youtube_videos, |
|
inputs = [input, OPEN_API_KEY], |
|
outputs = [output] |
|
), |
|
title="Summarize YouTube Videos", |
|
description="Enter a keyword to summarize related YouTube videos.") |
|
|
|
|
|
iface.launch() |
|
|