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() |