File size: 3,580 Bytes
51306d7
1ab5a2e
2819be2
ad28e9f
eb9c355
8626295
2abbd75
8626295
51306d7
 
2819be2
8626295
 
 
51306d7
 
 
 
 
ec4b4c0
51306d7
 
 
e0b59a0
637c10a
ca1bd19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff843a4
ca1bd19
 
 
 
 
 
637c10a
ca1bd19
e0b59a0
ca1bd19
 
 
 
 
 
 
 
 
 
 
e0b59a0
c358952
 
 
 
 
 
 
51306d7
 
e0b59a0
658998d
 
 
 
 
 
1ab5a2e
51306d7
 
658998d
ff843a4
2abbd75
e0b59a0
c196821
 
 
 
2bb8654
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from youtubesearchpython import VideosSearch, Transcript  # Changed import statement
import gradio as gr  
import openai
import os
import requests

# 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):
    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_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_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():
        keyword = gr.Textbox(label="keyword", placeholder='ํ‚ค์›Œ๋“œ๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”. (์˜ˆ.๋น„ํŠธ์ฝ”์ธ)')
        OPENAI_API_KEY = gr.Textbox(label="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, OPENAI_API_KEY],
            outputs=[analysis_result]
        ) 

demo.launch()