File size: 11,463 Bytes
08e9a37
 
 
 
 
dc8bec9
08e9a37
dc8bec9
08e9a37
 
dc8bec9
 
 
08e9a37
0bc13f7
08e9a37
 
0bc13f7
dc8bec9
0bc13f7
 
dc8bec9
08e9a37
0bc13f7
 
 
 
 
 
dc8bec9
 
0bc13f7
dc8bec9
 
0bc13f7
 
 
 
 
dc8bec9
08e9a37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0bc13f7
 
08e9a37
 
0bc13f7
08e9a37
 
 
 
 
 
 
 
 
 
 
 
0bc13f7
 
08e9a37
 
0bc13f7
08e9a37
 
 
 
 
 
 
 
 
 
 
 
0bc13f7
 
08e9a37
 
0bc13f7
08e9a37
 
 
 
 
 
 
 
 
 
 
 
0bc13f7
 
08e9a37
 
0bc13f7
08e9a37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import os
import gradio as gr

# ๋ชจ๋“ˆ ์ฝ”๋“œ๋“ค์„ ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ ๊ฐ€์ ธ์™€์„œ ๋™์ ์œผ๋กœ ์‹คํ–‰
def load_hidden_modules():
    """ํ™˜๊ฒฝ๋ณ€์ˆ˜์—์„œ ๋ชจ๋“ˆ ์ฝ”๋“œ๋“ค์„ ๊ฐ€์ ธ์™€์„œ ์‹คํ–‰ (์˜์กด์„ฑ ์ˆœ์„œ ๊ณ ๋ ค)"""
    
    # 1๋‹จ๊ณ„: youtube_transcript_module ์ฝ”๋“œ ๋จผ์ € ๋กœ๋“œ (๋‹ค๋ฅธ ๋ชจ๋“ˆ๋“ค์ด ์˜์กดํ•จ)
    youtube_transcript_code = os.environ.get("YOUTUBE_TRANSCRIPT_MODULE_CODE", "")
    if youtube_transcript_code:
        # import ๋ฌธ ์ œ๊ฑฐํ•˜๊ณ  ์‹คํ–‰
        cleaned_code = youtube_transcript_code.replace("from youtube_transcript_module import", "# from youtube_transcript_module import")
        exec(cleaned_code, globals())
    
    # 2๋‹จ๊ณ„: analyze_module ์ฝ”๋“œ ๋กœ๋“œ (ํ•จ์ˆ˜๋ช… ์ถฉ๋Œ ๋ฐฉ์ง€)
    analyze_module_code = os.environ.get("ANALYZE_MODULE_CODE", "")
    if analyze_module_code:
        # import ๋ฌธ ์ˆ˜์ • ๋ฐ ํ•จ์ˆ˜๋ช… ๋ณ€๊ฒฝ
        cleaned_code = analyze_module_code.replace("from youtube_transcript_module import YouTubeTranscriptClient", "# from youtube_transcript_module import YouTubeTranscriptClient")
        cleaned_code = cleaned_code.replace("def get_youtube_script(", "def get_youtube_script_analyze(")
        cleaned_code = cleaned_code.replace("get_youtube_script(url)", "get_youtube_script_analyze(url)")
        exec(cleaned_code, globals())
    
    # 3๋‹จ๊ณ„: ๋‚˜๋จธ์ง€ blog ๋ชจ๋“ˆ๋“ค ๋กœ๋“œ (๊ฐ๊ฐ ๊ณ ์œ ํ•œ ํ•จ์ˆ˜๋ช… ์‚ฌ์šฉ)
    module_configs = [
        ("GENERAL_BLOG_MODULE_CODE", "general", "general_process_youtube_url"),
        ("HEALTH_BLOG_MODULE_CODE", "health", "health_process_youtube_url"), 
        ("TRAVEL_BLOG_MODULE_CODE", "travel", "travel_process_youtube_url"),
        ("PRODUCT_REVIEW_MODULE_CODE", "product", "product_process_youtube_url")
    ]
    
    for env_key, prefix, func_name in module_configs:
        module_code = os.environ.get(env_key, "")
        if module_code:
            # import ๋ฌธ ์ •๋ฆฌ ๋ฐ ํ•จ์ˆ˜๋ช… ๋ณ€๊ฒฝ์œผ๋กœ ์ถฉ๋Œ ๋ฐฉ์ง€
            cleaned_code = module_code.replace("from youtube_transcript_module import YouTubeTranscriptClient", "# from youtube_transcript_module import YouTubeTranscriptClient")
            cleaned_code = cleaned_code.replace("def get_youtube_script(", f"def get_youtube_script_{prefix}(")
            cleaned_code = cleaned_code.replace("get_youtube_script(url)", f"get_youtube_script_{prefix}(url)")
            cleaned_code = cleaned_code.replace("def process_youtube_url(", f"def {func_name}(")
            exec(cleaned_code, globals())

# ์ˆจ๊ฒจ์ง„ ๋ชจ๋“ˆ๋“ค ๋กœ๋“œ
load_hidden_modules()

# ๊ธฐ์กด ์ฒซ ๋ฒˆ์งธ API์—์„œ ์š”๊ตฌํ•˜๋Š” ํ˜•์‹์œผ๋กœ ํ•จ์ˆ˜ ๋ž˜ํ•‘
def analyze_video(url):
    """๋ถ„์„ API ์—”๋“œํฌ์ธํŠธ - ๊ธฐ์กด ํ˜•์‹ ์œ ์ง€"""
    results = analyze(url)
    
    # ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์—์„œ ๋งˆ์ง€๋ง‰ ๊ฒฐ๊ณผ ๊ฐ€์ ธ์˜ค๊ธฐ
    last_result = None
    for result in results:
        last_result = result
    
    # ๊ธฐ์กด ๋ฐ˜ํ™˜ ํ˜•์‹: [script_output, summary_output, thumbnail_output]
    return [last_result[0], last_result[1], last_result[2]]

def process_youtube_url(url, style):
    """์ผ๋ฐ˜ ๋ธ”๋กœ๊ทธ API ์—”๋“œํฌ์ธํŠธ - ๊ธฐ์กด ํ˜•์‹ ์œ ์ง€"""
    # general_blog_module์—์„œ process_youtube_url ํ•จ์ˆ˜ ํ˜ธ์ถœ
    process_func = globals().get('general_process_youtube_url')
    if not process_func:
        return ["", "์ผ๋ฐ˜ ๋ธ”๋กœ๊ทธ ๋ชจ๋“ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."]
    
    results = process_func(url, style)
    
    # ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์—์„œ ๋งˆ์ง€๋ง‰ ๊ฒฐ๊ณผ ๊ฐ€์ ธํ•˜๊ธฐ
    last_result = None
    for result in results:
        last_result = result
    
    # ๊ธฐ์กด ๋ฐ˜ํ™˜ ํ˜•์‹: [script_output, blog_output]
    return [last_result[0], last_result[1]]

def process_youtube_url_v1_2(url, style):
    """์ƒํ™œ๊ฑด๊ฐ• ๋ธ”๋กœ๊ทธ API ์—”๋“œํฌ์ธํŠธ - ๊ธฐ์กด ํ˜•์‹ ์œ ์ง€"""
    # health_blog_module์—์„œ process_youtube_url ํ•จ์ˆ˜ ํ˜ธ์ถœ
    process_func = globals().get('health_process_youtube_url')
    if not process_func:
        return ["", "์ƒํ™œ๊ฑด๊ฐ• ๋ธ”๋กœ๊ทธ ๋ชจ๋“ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."]
    
    results = process_func(url, style)
    
    # ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์—์„œ ๋งˆ์ง€๋ง‰ ๊ฒฐ๊ณผ ๊ฐ€์ ธ์˜ค๊ธฐ
    last_result = None
    for result in results:
        last_result = result
    
    # ๊ธฐ์กด ๋ฐ˜ํ™˜ ํ˜•์‹: [script_output, blog_output]
    return [last_result[0], last_result[1]]

def process_youtube_url_v2(url, style):
    """์—ฌํ–‰ ๋ธ”๋กœ๊ทธ API ์—”๋“œํฌ์ธํŠธ - ๊ธฐ์กด ํ˜•์‹ ์œ ์ง€"""
    # travel_blog_module์—์„œ process_youtube_url ํ•จ์ˆ˜ ํ˜ธ์ถœ
    process_func = globals().get('travel_process_youtube_url')
    if not process_func:
        return ["", "์—ฌํ–‰ ๋ธ”๋กœ๊ทธ ๋ชจ๋“ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."]
    
    results = process_func(url, style)
    
    # ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์—์„œ ๋งˆ์ง€๋ง‰ ๊ฒฐ๊ณผ ๊ฐ€์ ธ์˜ค๊ธฐ
    last_result = None
    for result in results:
        last_result = result
    
    # ๊ธฐ์กด ๋ฐ˜ํ™˜ ํ˜•์‹: [script_output, blog_output]
    return [last_result[0], last_result[1]]

def process_youtube_url_v3(url, style):
    """์ƒํ’ˆํ›„๊ธฐ ๋ธ”๋กœ๊ทธ API ์—”๋“œํฌ์ธํŠธ - ๊ธฐ์กด ํ˜•์‹ ์œ ์ง€"""
    # product_review_module์—์„œ process_youtube_url ํ•จ์ˆ˜ ํ˜ธ์ถœ
    process_func = globals().get('product_process_youtube_url')
    if not process_func:
        return ["", "์ƒํ’ˆํ›„๊ธฐ ๋ธ”๋กœ๊ทธ ๋ชจ๋“ˆ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."]
    
    results = process_func(url, style)
    
    # ์ œ๋„ˆ๋ ˆ์ดํ„ฐ์—์„œ ๋งˆ์ง€๋ง‰ ๊ฒฐ๊ณผ ๊ฐ€์ ธ์˜ค๊ธฐ
    last_result = None
    for result in results:
        last_result = result
    
    # ๊ธฐ์กด ๋ฐ˜ํ™˜ ํ˜•์‹: [script_output, blog_output]
    return [last_result[0], last_result[1]]

# Gradio UI ๊ตฌ์„ฑ (Gradio 5.0 ์ด์ƒ์˜ ๋ฒ„์ „์—์„œ ์ž‘๋™)
with gr.Blocks(css="""
    .thumbnail-container {
        display: flex;
        justify-content: center;
        margin-bottom: 15px;
        border-radius: 8px;
        overflow: hidden;
        box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    }
    .thumbnail-container img {
        max-width: 100%;
        height: auto;
        object-fit: cover;
    }
    .script-box {
        border: 1px solid #e0e0e0;
        padding: 15px;
        border-radius: 8px;
        background-color: #f9f9f9;
        margin-bottom: 15px;
    }
    .output-title {
        font-size: 18px;
        font-weight: bold;
        margin-bottom: 10px;
        color: #333;
    }
    details summary {
        cursor: pointer;
        font-weight: bold;
        padding: 8px 0;
        color: #2196F3;
    }
    li {
        margin-bottom: 5px;
    }
    .error-box {
        border: 1px solid #ff4444;
        padding: 15px;
        border-radius: 8px;
        background-color: #ffeeee;
        margin-bottom: 15px;
        color: #cc0000;
    }
""") as demo:
    with gr.Tabs():
        # ์ฒซ ๋ฒˆ์งธ ํƒญ: /analyze API
        with gr.TabItem("์œ ํŠœ๋ธŒ ์š”์•ฝ"):
            gr.Markdown("### YouTube URL ์ž…๋ ฅ")
            with gr.Row():
                url_input = gr.Textbox(label="YouTube URL ์ž…๋ ฅ", placeholder="https://www.youtube.com/watch?v=example")
            
            # ์ถœ๋ ฅ ์˜์—ญ ์„ค์ •
            script_output = gr.HTML(label="์›๋ฌธ ์Šคํฌ๋ฆฝํŠธ")
            summary_output = gr.HTML(label="์š”์•ฝ")
            thumbnail_output = gr.Image(label="์ธ๋„ค์ผ", show_label=True)

            # ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ ๋ถ„์„ ํ•จ์ˆ˜ ํ˜ธ์ถœ
            analyze_button = gr.Button("๋ถ„์„ ์‹คํ–‰")
            analyze_button.click(analyze_video, inputs=[url_input], outputs=[script_output, summary_output, thumbnail_output])

        # ๋‘ ๋ฒˆ์งธ ํƒญ: ์ผ๋ฐ˜ ๋ธ”๋กœ๊ทธ
        with gr.TabItem("์ผ๋ฐ˜ ๋ธ”๋กœ๊ทธ"):
            gr.Markdown("### YouTube URL๊ณผ ํฌ์ŠคํŒ… ์Šคํƒ€์ผ ์ž…๋ ฅ")
            with gr.Row():
                youtube_url_input = gr.Textbox(label="YouTube URL ์ž…๋ ฅ", placeholder="https://www.youtube.com/watch?v=example")
                style_input = gr.Radio(["์นœ๊ทผํ•œ", "์ผ๋ฐ˜์ ์ธ", "์ „๋ฌธ์ ์ธ"], label="ํฌ์ŠคํŒ… ์Šคํƒ€์ผ", value="์นœ๊ทผํ•œ")
            
            # ์ถœ๋ ฅ ์˜์—ญ ์„ค์ • - ์›๋ฌธ ์Šคํฌ๋ฆฝํŠธ ๋ฐ ์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€
            script_output = gr.HTML(label="์›๋ฌธ ์Šคํฌ๋ฆฝํŠธ")
            blog_output = gr.HTML(label="์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€")

            # ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ /process_youtube_url ํ˜ธ์ถœ
            generate_blog_button = gr.Button("๋ธ”๋กœ๊ทธ ๊ธ€ ์ƒ์„ฑ")
            generate_blog_button.click(process_youtube_url, inputs=[youtube_url_input, style_input], outputs=[script_output, blog_output])

        # ์„ธ ๋ฒˆ์งธ ํƒญ: ์ƒํ™œ๊ฑด๊ฐ• ๋ธ”๋กœ๊ทธ
        with gr.TabItem("์ƒํ™œ๊ฑด๊ฐ• ๋ธ”๋กœ๊ทธ"):
            gr.Markdown("### YouTube URL๊ณผ ํฌ์ŠคํŒ… ์Šคํƒ€์ผ ์ž…๋ ฅ")
            with gr.Row():
                youtube_url_input_v1_2 = gr.Textbox(label="YouTube URL ์ž…๋ ฅ", placeholder="https://www.youtube.com/watch?v=example")
                style_input_v1_2 = gr.Radio(["์นœ๊ทผํ•œ", "์ผ๋ฐ˜์ ์ธ", "์ „๋ฌธ์ ์ธ"], label="ํฌ์ŠคํŒ… ์Šคํƒ€์ผ", value="์นœ๊ทผํ•œ")
            
            # ์ถœ๋ ฅ ์˜์—ญ ์„ค์ • - ์›๋ฌธ ์Šคํฌ๋ฆฝํŠธ ๋ฐ ์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€
            script_output_v1_2 = gr.HTML(label="์›๋ฌธ ์Šคํฌ๋ฆฝํŠธ")
            blog_output_v1_2 = gr.HTML(label="์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€")

            # ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ /process_youtube_url ํ˜ธ์ถœ
            generate_blog_button_v1_2 = gr.Button("๋ธ”๋กœ๊ทธ ๊ธ€ ์ƒ์„ฑ")
            generate_blog_button_v1_2.click(process_youtube_url_v1_2, inputs=[youtube_url_input_v1_2, style_input_v1_2], outputs=[script_output_v1_2, blog_output_v1_2])

        # ๋„ค ๋ฒˆ์งธ ํƒญ: ์—ฌํ–‰ ๋ธ”๋กœ๊ทธ
        with gr.TabItem("์—ฌํ–‰ ๋ธ”๋กœ๊ทธ"):
            gr.Markdown("### YouTube URL๊ณผ ํฌ์ŠคํŒ… ์Šคํƒ€์ผ ์ž…๋ ฅ")
            with gr.Row():
                youtube_url_input_v2 = gr.Textbox(label="YouTube URL ์ž…๋ ฅ", placeholder="https://www.youtube.com/watch?v=example")
                style_input_v2 = gr.Radio(["์นœ๊ทผํ•œ", "์ผ๋ฐ˜์ ์ธ", "์ „๋ฌธ์ ์ธ"], label="ํฌ์ŠคํŒ… ์Šคํƒ€์ผ", value="์นœ๊ทผํ•œ")
            
            # ์ถœ๋ ฅ ์˜์—ญ ์„ค์ • - ์›๋ฌธ ์Šคํฌ๋ฆฝํŠธ ๋ฐ ์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€
            script_output_v2 = gr.HTML(label="์›๋ฌธ ์Šคํฌ๋ฆฝํŠธ")
            blog_output_v2 = gr.HTML(label="์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€")

            # ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ /process_youtube_url ํ˜ธ์ถœ
            generate_blog_button_v2 = gr.Button("๋ธ”๋กœ๊ทธ ๊ธ€ ์ƒ์„ฑ")
            generate_blog_button_v2.click(process_youtube_url_v2, inputs=[youtube_url_input_v2, style_input_v2], outputs=[script_output_v2, blog_output_v2])

        # ๋‹ค์„ฏ ๋ฒˆ์งธ ํƒญ: ์ƒํ’ˆํ›„๊ธฐ ๋ธ”๋กœ๊ทธ
        with gr.TabItem("์ƒํ’ˆํ›„๊ธฐ ๋ธ”๋กœ๊ทธ"):
            gr.Markdown("### YouTube URL๊ณผ ํฌ์ŠคํŒ… ์Šคํƒ€์ผ ์ž…๋ ฅ")
            with gr.Row():
                youtube_url_input_v3 = gr.Textbox(label="YouTube URL ์ž…๋ ฅ", placeholder="https://www.youtube.com/watch?v=example")
                style_input_v3 = gr.Radio(["์นœ๊ทผํ•œ", "์ผ๋ฐ˜์ ์ธ", "์ „๋ฌธ์ ์ธ"], label="ํฌ์ŠคํŒ… ์Šคํƒ€์ผ", value="์นœ๊ทผํ•œ")
            
            # ์ถœ๋ ฅ ์˜์—ญ ์„ค์ • - ์›๋ฌธ ์Šคํฌ๋ฆฝํŠธ ๋ฐ ์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€
            script_output_v3 = gr.HTML(label="์›๋ฌธ ์Šคํฌ๋ฆฝํŠธ")
            blog_output_v3 = gr.HTML(label="์ƒ์„ฑ๋œ ๋ธ”๋กœ๊ทธ ๊ธ€")

            # ๋ฒ„ํŠผ ํด๋ฆญ ์‹œ /process_youtube_url ํ˜ธ์ถœ
            generate_blog_button_v3 = gr.Button("๋ธ”๋กœ๊ทธ ๊ธ€ ์ƒ์„ฑ")
            generate_blog_button_v3.click(process_youtube_url_v3, inputs=[youtube_url_input_v3, style_input_v3], outputs=[script_output_v3, blog_output_v3])

# UI ์‹คํ–‰
if __name__ == "__main__":
    demo.launch()