|
from youtubesearchpython import VideosSearch, Transcript |
|
import gradio as gr |
|
import openai |
|
import os |
|
import requests |
|
|
|
|
|
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): |
|
API_URL = "https://api.openai.com/v1/chat/completions" |
|
payload = { |
|
"model": "gpt-4-0125-preview", |
|
"messages": [{ |
|
"role": "system", |
|
"content": "λΉμ μ λ΄μ©μ μμ½νλ μ±λ΄μ
λλ€." |
|
}, { |
|
"role": "user", |
|
"content": f"μ νλΈ λ΄μ©μΈ '{contents}'μ λν΄ μμ½ν΄μ£ΌμΈμ." |
|
}], |
|
} |
|
headers = { |
|
"Content-Type": "application/json", |
|
"Authorization": f"Bearer {OPENAI_API_KEY}" |
|
} |
|
response = requests.post(API_URL, headers=headers, json=payload) |
|
if response.status_code == 200: |
|
data = response.json() |
|
return data["choices"][0]["message"]["content"] |
|
else: |
|
|
|
return f"μ€λ₯κ° λ°μνμ΅λλ€. μν μ½λ: {response.status_code}, λ©μμ§: {response.json().get('error', {}).get('message', 'Unknown error')}", "" |
|
|
|
def summarize_youtube_videos(keyword, OPENAI_API_KEY): |
|
urls = search_youtube_videos(keyword) |
|
contents = get_transcript(urls) |
|
summary = summarize_text(contents, OPENAI_API_KEY) |
|
return summary |
|
|
|
|
|
with gr.Blocks(css="footer {visibility: hidden;}") as demo: |
|
|
|
with gr.Tab("youtube"): |
|
keyword = gr.Textbox(label="keyword", placeholder='ν€μλλ₯Ό μ
λ ₯νμΈμ. (μ.λΉνΈμ½μΈ)') |
|
OPENAI_API_KEY = gr.Textbox(label="OpenAI API ν€", placeholder="OpenAI API ν€λ₯Ό μ
λ ₯νμΈμ") |
|
analysis_result = gr.HTML() |
|
analysis_btn = gr.Button("submit") |
|
|
|
analysis_btn.click( |
|
fn=summarize_youtube_videos, |
|
inputs=[keyword, OPENAI_API_KEY], |
|
outputs=[analysis_result] |
|
) |
|
|
|
demo.launch() |
|
|
|
|