File size: 2,446 Bytes
51306d7
1ab5a2e
2819be2
ad28e9f
8626295
2abbd75
8626295
51306d7
 
2819be2
8626295
 
 
51306d7
 
 
 
 
ec4b4c0
51306d7
 
 
e0b59a0
637c10a
 
a427bef
e0b59a0
 
c358952
 
 
7e9da05
 
 
 
 
51306d7
c358952
e0b59a0
c358952
 
 
 
 
 
 
51306d7
 
e0b59a0
1ab5a2e
51306d7
 
802a1f3
51306d7
 
e0b59a0
 
 
2abbd75
e0b59a0
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
72
73
74
75
76
77
78
from youtubesearchpython import VideosSearch, Transcript  # Changed import statement
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_text(contents):
#     response = openai.ChatCompletion.create(
#         engine="gpt-3.5-turbo",
#         prompt=f"์š”์•ฝ: {contents}",
#         max_tokens=150
#     )
#     return response.choices[0].text.strip()

# Function to integrate all the functionalities
    
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

def summarize_button_click(keyword, OPENAI_API_KEY):
    summary = summarize_youtube_videos(keyword, OPENAI_API_KEY)
    return summary


with gr.Blocks(css="footer {visibility: hidden;}") as demo:

    with gr.Tab():
        keyword = gr.Textbox(label="keyword", placeholder='ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. (์˜ˆ.๋น„ํŠธ์ฝ”์ธ)')
        OPEN_API_KEY = gr.Textbox("OpenAI API ํ‚ค", placeholder="์—ฌ๊ธฐ์— OpenAI API ํ‚ค๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”")
        output = gr.JSON(label="๊ฒฐ๊ณผ")
        analysis_result = gr.HTML()  # ์‚ฌ์ฃผ ๋ถ„์„ ๊ฒฐ๊ณผ๋ฅผ ์ถœ๋ ฅํ•  HTML ์ปดํฌ๋„ŒํŠธ
        analysis_btn = gr.Button("submit")

        analysis_btn.click(
            fn=summarize_youtube_videos,
            inputs=[keyword, OPEN_API_KEY],
            outputs=[output, analysis_result]
        ) 

demo.launch()