File size: 2,320 Bytes
7fb6aae
1ab5a2e
2819be2
ad28e9f
eb9c355
8626295
 
51306d7
 
2819be2
8626295
 
 
51306d7
 
 
 
 
ec4b4c0
51306d7
 
 
e0b59a0
637c10a
ca1bd19
 
7fb6aae
ca1bd19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff843a4
ca1bd19
 
 
 
1ab5a2e
51306d7
 
8f3a300
ff843a4
2abbd75
e0b59a0
c196821
 
7fb6aae
c196821
6000f8f
 
c196821
 
 
 
2bb8654
ff843a4
c196821
2abbd75
c196821
2fa67b7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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()