File size: 9,112 Bytes
e640a83 | 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 | import os
from datetime import datetime, timedelta
import gradio as gr
def simulate_file_naming(name_option, additional_name, add_date, add_time):
# ๊ณ ์ ๋ ์๋ณธ ํ์ผ๋ช
original_filename = "sample.jpg"
original_name, ext = os.path.splitext(original_filename)
ext = ext.lstrip(".") if ext else "bin"
# ์ต์
์ ๋ฐ๋ฅธ ํ์ผ๋ช
๊ธฐ๋ณธ ๋ถ๋ถ ๊ฒฐ์
if name_option == "ํ์ผ๋ช
๋ณ๊ฒฝ":
final_base = additional_name.strip() if additional_name.strip() else "ํ์ผ"
option_text = f"ํ์ผ๋ช
๋ณ๊ฒฝ ({additional_name.strip() if additional_name.strip() else '๊ธฐ๋ณธ๊ฐ'})"
elif name_option == "ํ์ผ๋ช
์ ์ง":
final_base = original_name
option_text = "ํ์ผ๋ช
์ ์ง"
elif name_option == "ํ์ผ๋ช
์ ์ง + ์ถ๊ฐ๋ช
":
final_base = original_name + (f"_{additional_name.strip()}" if additional_name.strip() else "")
option_text = f"ํ์ผ๋ช
์ ์ง + ์ถ๊ฐ๋ช
({additional_name.strip() if additional_name.strip() else '์์'})"
elif name_option == "์ถ๊ฐ๋ช
+ ํ์ผ๋ช
์ ์ง":
final_base = (f"{additional_name.strip()}_" if additional_name.strip() else "") + original_name
option_text = f"์ถ๊ฐ๋ช
+ ํ์ผ๋ช
์ ์ง ({additional_name.strip() if additional_name.strip() else '์์'})"
else:
final_base = original_name
option_text = "๋ฏธ์ ํ"
# ๋ ์ง ๋ฐ ์๊ฐ stamp (ํ๊ตญ ๊ธฐ์ค)
now = datetime.utcnow() + timedelta(hours=9)
date_str = now.strftime('%Y%m%d') if add_date else ""
time_str = now.strftime('%H%M%S') if add_time else ""
timestamp = ""
additional_info = []
if add_date:
additional_info.append("๋ ์ง ์ ์ฉ")
timestamp += f"_{date_str}"
if add_time:
additional_info.append("์๊ฐ ์ ์ฉ")
timestamp += f"_{time_str}"
# ์ต์ข
ํ์ผ๋ช
๊ตฌ์ฑ (ํ์ฅ์ ์ ์ง)
final_file_name = f"{final_base}{timestamp}.{ext}"
# ์์ธ ๊ฒฐ๊ณผ ๋ฉ์์ง ๊ตฌ์ฑ
detail_msg = (
f"์๋ณธ์ด๋ฆ : {original_filename}\n"
f"์ต์
์ ํ : {option_text}\n"
f"์ถ๊ฐ : {', '.join(additional_info) if additional_info else '์์'}\n"
f"๊ฒฐ๊ณผ๋ช
: {final_file_name}"
)
return detail_msg, final_file_name
# ์ฝ๋์ถ๋ ฅ์ ํ์ํ ์ ์ฒด ์ฝ๋ ๋ฌธ์์ด (ํค๋๋ ์ ๊ฑฐ)
code_str = '''
์ ์ ํ๋ ํญ๋ชฉ๋ถ๋ถ๊ณผ ์
๋ก๋ ๋ ํ์ผ๋ช
์ ๊ธฐ๋ฐ์ผ๋ก ์๋ ์ฝ๋๋ฅผ ํ์ฉํ์ฌ ๋ฐ๋ก ์ ์ฉํ ์ ์๋ ์ฝ๋๋ก ๋ณ๊ฒฝํ์ธ์.
ํ์ผ๋ช
์ ๋ฐ๊พธ๋ ๊ธฐ๋ฅ์ ์ฝ๋ ์ธ ๋ชจ๋ ์ญ์ ํ์ธ์.
import os
import tempfile
from datetime import datetime, timedelta
import gradio as gr
from PIL import Image
def save_file(file_data, final_file_name):
"""
ํ์ผ ๋ฐ์ดํฐ๋ฅผ ์์ ํด๋์ ์ ์ฅํ๊ณ ์ต์ข
ํ์ผ ๊ฒฝ๋ก๋ฅผ ๋ฐํ.
final_file_name์๋ ํ์ฅ์ ํฌํจ.
"""
temp_file_path = os.path.join(tempfile.gettempdir(), final_file_name)
# ์
๋ก๋๋ ํ์ผ์ด ํ์ผ ๊ฒฝ๋ก์ธ ๊ฒฝ์ฐ
if isinstance(file_data, str) and os.path.exists(file_data):
with open(file_data, "rb") as f:
data = f.read()
with open(temp_file_path, "wb") as f:
f.write(data)
# PIL ์ด๋ฏธ์ง ๊ฐ์ฒด์ธ ๊ฒฝ์ฐ
elif hasattr(file_data, "save"):
ext = os.path.splitext(final_file_name)[1].lstrip(".")
pil_format = "JPEG" if ext.lower() == "jpg" else ext.upper()
file_data.save(temp_file_path, format=pil_format)
# bytes ๋ฐ์ดํฐ์ธ ๊ฒฝ์ฐ
elif isinstance(file_data, bytes):
with open(temp_file_path, "wb") as f:
f.write(file_data)
# ๋ฌธ์์ด ๋ฐ์ดํฐ์ธ ๊ฒฝ์ฐ (์: ํ
์คํธ)
elif isinstance(file_data, str):
with open(temp_file_path, "w", encoding="utf-8") as f:
f.write(file_data)
else:
raise ValueError("์ง์๋์ง ์๋ ํ์ผ ํ์์
๋๋ค.")
return temp_file_path
def process_file(file, name_option, additional_name, add_date, add_time):
"""
์
๋ก๋๋ ํ์ผ์ ๋ฐ์ ๋ผ๋์ค ์ต์
์ ๋ฐ๋ผ ํ์ผ๋ช
์ ์ฌ๊ตฌ์ฑํ ํ ์ ์ฅํฉ๋๋ค.
Parameters:
file: ์
๋ก๋๋ ํ์ผ (gr.File ๋ฑ, ํ์ผ ๊ฒฝ๋ก ๋๋ ํ์ผ ๊ฐ์ฒด)
name_option: ๋ผ๋์ค ๋ฒํผ ์ ํ๊ฐ (๋ค ๊ฐ์ง ์ต์
์ค ํ๋)
additional_name: ์ต์
์ ๋ฐ๋ฅธ ์ถ๊ฐ ์
๋ ฅ(์ถ๊ฐ๋ช
๋๋ ๋ณ๊ฒฝํ ํ์ผ๋ช
)
add_date: ๋ ์ง ์ ์ฉ ์ฌ๋ถ (Boolean)
add_time: ์๊ฐ ์ ์ฉ ์ฌ๋ถ (Boolean)
๋ฐํ๊ฐ:
๋ณ๊ฒฝ๋ ํ์ผ๋ช
์ ๊ฐ์ง ๋ค์ด๋ก๋ ๊ฐ๋ฅํ ํ์ผ ๊ฒฝ๋ก
"""
if file is None:
return None
# ์๋ณธ ํ์ผ๋ช
๊ณผ ํ์ฅ์ ์ถ์ถ
if isinstance(file, str) and os.path.exists(file):
original_filename = os.path.basename(file)
elif hasattr(file, "name"):
original_filename = os.path.basename(file.name)
else:
original_filename = "ํ์ผ.bin"
original_name, ext = os.path.splitext(original_filename)
ext = ext.lstrip(".") if ext else "bin"
# ๋ผ๋์ค ์ต์
์ ๋ฐ๋ผ ์ต์ข
๊ธฐ๋ณธ ํ์ผ๋ช
๊ฒฐ์
if name_option == "ํ์ผ๋ช
๋ณ๊ฒฝ":
# ์
๋ ฅ๋ ์ ํ์ผ๋ช
์ฌ์ฉ (์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ "ํ์ผ")
final_base = additional_name.strip() if additional_name.strip() else "ํ์ผ"
elif name_option == "ํ์ผ๋ช
์ ์ง":
final_base = original_name
elif name_option == "ํ์ผ๋ช
์ ์ง + ์ถ๊ฐ๋ช
":
final_base = original_name + (f"_{additional_name.strip()}" if additional_name.strip() else "")
elif name_option == "์ถ๊ฐ๋ช
+ ํ์ผ๋ช
์ ์ง":
final_base = (f"{additional_name.strip()}_" if additional_name.strip() else "") + original_name
else:
final_base = original_name
# ๋ ์ง ๋ฐ ์๊ฐ stamp (ํ๊ตญ ๊ธฐ์ค)
now = datetime.utcnow() + timedelta(hours=9)
date_str = now.strftime('%Y%m%d') if add_date else ""
time_str = now.strftime('%H%M%S') if add_time else ""
timestamp = ""
if date_str and time_str:
timestamp = f"_{date_str}_{time_str}"
elif date_str:
timestamp = f"_{date_str}"
elif time_str:
timestamp = f"_{time_str}"
# ์ต์ข
ํ์ผ๋ช
๊ตฌ์ฑ (์ ๋์ฌ ์ ๊ฑฐ)
final_file_name = f"{final_base}{timestamp}.{ext}"
return save_file(file, final_file_name)
iface = gr.Interface(
fn=process_file,
inputs=[
gr.File(label="ํ์ผ ์
๋ก๋"),
gr.Radio(
choices=["ํ์ผ๋ช
๋ณ๊ฒฝ", "ํ์ผ๋ช
์ ์ง", "ํ์ผ๋ช
์ ์ง + ์ถ๊ฐ๋ช
", "์ถ๊ฐ๋ช
+ ํ์ผ๋ช
์ ์ง"],
label="ํ์ผ๋ช
์ต์
"
),
gr.Text(label="์ถ๊ฐ๋ช
/๋ณ๊ฒฝ ํ์ผ๋ช
์
๋ ฅ", placeholder="์ต์
์ ๋ฐ๋ผ ์
๋ ฅํ์ธ์"),
gr.Checkbox(label="๋ ์ง ์ ์ฉ", value=True),
gr.Checkbox(label="์๊ฐ ์ ์ฉ", value=True)
],
outputs=gr.File(label="๋ค์ด๋ก๋ ํ์ผ"),
title="ํ์ผ๋ช
์ต์
์ ๋ฐ๋ฅธ ํ์ผ๋ช
๋ณ๊ฒฝ ๋ฐ ๋ค์ด๋ก๋",
description=(
"์
๋ก๋๋ ํ์ผ์ ํ์ผ๋ช
์ ์๋ ์ต์
์ ๋ฐ๋ผ ๋ณ๊ฒฝํฉ๋๋ค.\n"
"์ต์
1(ํ์ผ๋ช
๋ณ๊ฒฝ): ์
๋ ฅํ ์ด๋ฆ์ผ๋ก ํ์ผ๋ช
๋ณ๊ฒฝ\n"
"์ต์
2(ํ์ผ๋ช
์ ์ง): ์๋ณธ ํ์ผ๋ช
์ ์ง\n"
"์ต์
3(ํ์ผ๋ช
์ ์ง + ์ถ๊ฐ๋ช
): ์๋ณธ ํ์ผ๋ช
๋ค์ ์ถ๊ฐ๋ช
๋ถ์ฐฉ\n"
"์ต์
4(์ถ๊ฐ๋ช
+ ํ์ผ๋ช
์ ์ง): ์ถ๊ฐ๋ช
์ ์๋ณธ ํ์ผ๋ช
์์ ๋ถ์ฐฉ\n"
"๋ ์ง/์๊ฐ ์ฒดํฌ ์ ํด๋น ์ ๋ณด๊ฐ ํ์ผ๋ช
๋ค์ ์ถ๊ฐ๋ฉ๋๋ค."
)
)
if __name__ == "__main__":
iface.launch()'''
def update_all(name_option, additional_name, add_date, add_time):
detail_msg, final_file_name = simulate_file_naming(name_option, additional_name, add_date, add_time)
# ์ฝ๋์ถ๋ ฅ ์์ญ ์ต์๋จ์ "[ํ์ผ๋ช
๋ณ๊ฒฝ ์ฝ๋์์]"์ ํ ์นธ ๋์ด ํ ์ ํ์์ ๊ฒฐ๊ณผ(detail_msg)์ ์ ์ฒด ์ฝ๋ ์ถ๋ ฅ
code_result = f"[ํ์ผ๋ช
๋ณ๊ฒฝ ์ฝ๋์์]\n\n{detail_msg}\n\n{code_str}"
return final_file_name, code_result
with gr.Blocks() as demo:
with gr.Group():
gr.Markdown("### ์๋ณธ ํ์ผ๋ช
: sample.jpg")
with gr.Row():
name_option = gr.Radio(
choices=["ํ์ผ๋ช
๋ณ๊ฒฝ", "ํ์ผ๋ช
์ ์ง", "ํ์ผ๋ช
์ ์ง + ์ถ๊ฐ๋ช
", "์ถ๊ฐ๋ช
+ ํ์ผ๋ช
์ ์ง"],
label="ํ์ผ๋ช
์ต์
",
value="ํ์ผ๋ช
๋ณ๊ฒฝ"
)
additional_name = gr.Text(
label="์ถ๊ฐ๋ช
/๋ณ๊ฒฝ ํ์ผ๋ช
์
๋ ฅ",
placeholder="์ต์
์ ๋ฐ๋ผ ์
๋ ฅํ์ธ์"
)
with gr.Row():
add_date = gr.Checkbox(label="๋ ์ง ์ ์ฉ", value=True)
add_time = gr.Checkbox(label="์๊ฐ ์ ์ฉ", value=True)
with gr.Row():
final_filename_output = gr.Text(label="ํ์ผ๋ช
๊ฒฐ๊ณผ", lines=2)
with gr.Row():
code_output = gr.Text(label="์ฝ๋์ถ๋ ฅ", lines=20)
inputs = [name_option, additional_name, add_date, add_time]
outputs = [final_filename_output, code_output]
for comp in inputs:
comp.change(update_all, inputs=inputs, outputs=outputs)
demo.launch() |