fvw3gsx3 / app.py
ssboost's picture
Update app.py
0bc13f7 verified
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()