Update app.py
Browse files
app.py
CHANGED
@@ -12,10 +12,29 @@ import torch
|
|
12 |
from diffusers import DiffusionPipeline
|
13 |
from PIL import Image
|
14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
# ===== OpenAI ์ค์ =====
|
16 |
from openai import OpenAI
|
17 |
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
# ===== ํ๋กฌํํธ ์ฆ๊ฐ์ฉ ์คํ์ผ ํ๋ฆฌ์
=====
|
21 |
STYLE_PRESETS = {
|
@@ -27,18 +46,26 @@ STYLE_PRESETS = {
|
|
27 |
}
|
28 |
|
29 |
# ===== ์ ์ฅ ํด๋ =====
|
30 |
-
SAVE_DIR = "saved_images"
|
31 |
if not os.path.exists(SAVE_DIR):
|
32 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
33 |
|
34 |
# ===== ๋๋ฐ์ด์ค & ๋ชจ๋ธ ๋ก๋ =====
|
35 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
36 |
-
|
37 |
-
adapter_id = "seawolf2357/kim-korea" # ํน์ ์ ์น์ธ์ ํ์ตํ LoRA ๋ชจ๋ธ
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
MAX_SEED = np.iinfo(np.int32).max
|
44 |
MAX_IMAGE_SIZE = 1024
|
@@ -55,11 +82,15 @@ def openai_translate(text: str, retries: int = 3) -> str:
|
|
55 |
"""ํ๊ธ์ ์์ด๋ก ๋ฒ์ญ (OpenAI GPT-4.1-mini ์ฌ์ฉ). ์์ด ์
๋ ฅ์ด๋ฉด ๊ทธ๋๋ก ๋ฐํ."""
|
56 |
if not is_korean(text):
|
57 |
return text
|
|
|
|
|
|
|
|
|
58 |
|
59 |
for attempt in range(retries):
|
60 |
try:
|
61 |
res = client.chat.completions.create(
|
62 |
-
model="gpt-4.1-mini
|
63 |
messages=[
|
64 |
{
|
65 |
"role": "system",
|
@@ -71,7 +102,7 @@ def openai_translate(text: str, retries: int = 3) -> str:
|
|
71 |
max_tokens=256,
|
72 |
)
|
73 |
return res.choices[0].message.content.strip()
|
74 |
-
except
|
75 |
print(f"[translate] attempt {attempt + 1} failed: {e}")
|
76 |
time.sleep(2)
|
77 |
return text # ๋ฒ์ญ ์คํจ ์ ์๋ฌธ ๊ทธ๋๋ก
|
@@ -104,6 +135,9 @@ def save_generated_image(image: Image.Image, prompt: str) -> str:
|
|
104 |
# ===== Diffusion ํธ์ถ =====
|
105 |
|
106 |
def run_pipeline(prompt: str, seed: int, width: int, height: int, guidance_scale: float, num_steps: int, lora_scale: float):
|
|
|
|
|
|
|
107 |
generator = torch.Generator(device=device).manual_seed(int(seed))
|
108 |
result = pipeline(
|
109 |
prompt=prompt,
|
@@ -131,19 +165,27 @@ def generate_image(
|
|
131 |
lora_scale: float = 1.0,
|
132 |
progress=None,
|
133 |
):
|
134 |
-
|
135 |
-
|
|
|
136 |
|
137 |
-
|
138 |
-
|
|
|
139 |
|
140 |
-
|
141 |
-
|
142 |
|
143 |
-
|
144 |
-
|
145 |
|
146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
147 |
|
148 |
# ===== ์์ ํ๋กฌํํธ (ํ๊ตญ์ด/์์ด ํผ์ฉ ํ์ฉ) =====
|
149 |
|
@@ -154,12 +196,13 @@ examples = [
|
|
154 |
"Mr. KIM์ด ๋ถ๋น๋ ๊ฑฐ๋ฆฌ์์ ์ฌ์ฑ ์๋ฏผ๋ค๊ณผ ๋ฐ๋ปํ๊ฒ ์
์ํ๋ ๏ฟฝ๏ฟฝ์ต, ์ฌ์ฑ ์ ๊ถ์๋ค์ ๋ํ ์ง์ ํ ๊ด์ฌ๊ณผ ์ํต์ ๋ณด์ฌ์ฃผ๊ณ ์๋ค.",
|
155 |
"Mr. KIM์ด ์ ๊ฑฐ ์ ์ธ์ฅ์์ ์งํ์ ์ ํฅํด ์๊ฐ๋ฝ์ผ๋ก ๊ฐ๋ฆฌํค๋ฉฐ ์๊ฐ์ ์ฃผ๋ ์ ์ค์ฒ๋ฅผ ์ทจํ๊ณ ์๊ณ , ์ฌ์ฑ๋ค๊ณผ ์์ด๋ค์ด ๋ฐ์๋ฅผ ์น๊ณ ์๋ค.",
|
156 |
"Mr. KIM์ด ์ง์ญ ํ์ฌ์ ์ฐธ์ฌํ์ฌ ์ด์ ์ ์ผ๋ก ์์ํ๋ ์ฌ์ฑ ์ง์ง์๋ค์๊ฒ ๋๋ฌ์ธ์ฌ ์๋ ๋ชจ์ต.",
|
157 |
-
"Mr. KIM visiting a local market, engaging in friendly conversation with female vendors and shopkeepers.
|
158 |
-
"Mr. KIM walking through a university campus, discussing education policies with female students and professors.
|
159 |
-
"Mr. KIM delivering a powerful speech in front of a large crowd with confident gestures and determined expression.
|
160 |
"Mr. KIM in a dynamic interview setting, passionately outlining his visions for the future.",
|
161 |
-
"Mr. KIM preparing for an important debate, surrounded by paperwork, looking focused and resolute.
|
162 |
]
|
|
|
163 |
# ===== ์ปค์คํ
CSS (๋ถ์ ํค ์ ์ง) =====
|
164 |
custom_css = """
|
165 |
:root {
|
@@ -183,63 +226,68 @@ button:hover{transform:translateY(-2px); box-shadow:0 5px 15px rgba(0,0,0,.1);}
|
|
183 |
"""
|
184 |
|
185 |
# ===== Gradio UI =====
|
186 |
-
|
187 |
-
gr.
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
gr.
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
with gr.
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
gr.
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
|
239 |
-
|
240 |
-
|
241 |
-
|
242 |
-
|
243 |
-
|
244 |
-
demo
|
245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
12 |
from diffusers import DiffusionPipeline
|
13 |
from PIL import Image
|
14 |
|
15 |
+
# ===== Fix: Import spaces for Hugging Face Spaces =====
|
16 |
+
try:
|
17 |
+
import spaces
|
18 |
+
HF_SPACES = True
|
19 |
+
except ImportError:
|
20 |
+
# If running locally, create a dummy decorator
|
21 |
+
def spaces_gpu_decorator(duration=60):
|
22 |
+
def decorator(func):
|
23 |
+
return func
|
24 |
+
return decorator
|
25 |
+
spaces = type('spaces', (), {'GPU': spaces_gpu_decorator})()
|
26 |
+
HF_SPACES = False
|
27 |
+
print("Warning: Running without Hugging Face Spaces GPU allocation")
|
28 |
+
|
29 |
# ===== OpenAI ์ค์ =====
|
30 |
from openai import OpenAI
|
31 |
|
32 |
+
# Add error handling for API key
|
33 |
+
try:
|
34 |
+
client = OpenAI(api_key=os.getenv("LLM_API"))
|
35 |
+
except Exception as e:
|
36 |
+
print(f"Warning: OpenAI client initialization failed: {e}")
|
37 |
+
client = None
|
38 |
|
39 |
# ===== ํ๋กฌํํธ ์ฆ๊ฐ์ฉ ์คํ์ผ ํ๋ฆฌ์
=====
|
40 |
STYLE_PRESETS = {
|
|
|
46 |
}
|
47 |
|
48 |
# ===== ์ ์ฅ ํด๋ =====
|
49 |
+
SAVE_DIR = "saved_images"
|
50 |
if not os.path.exists(SAVE_DIR):
|
51 |
os.makedirs(SAVE_DIR, exist_ok=True)
|
52 |
|
53 |
# ===== ๋๋ฐ์ด์ค & ๋ชจ๋ธ ๋ก๋ =====
|
54 |
device = "cuda" if torch.cuda.is_available() else "cpu"
|
55 |
+
print(f"Using device: {device}")
|
|
|
56 |
|
57 |
+
repo_id = "black-forest-labs/FLUX.1-dev"
|
58 |
+
adapter_id = "seawolf2357/kim-korea"
|
59 |
+
|
60 |
+
# Add error handling for model loading
|
61 |
+
try:
|
62 |
+
pipeline = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.bfloat16)
|
63 |
+
pipeline.load_lora_weights(adapter_id)
|
64 |
+
pipeline = pipeline.to(device)
|
65 |
+
print("Model loaded successfully")
|
66 |
+
except Exception as e:
|
67 |
+
print(f"Error loading model: {e}")
|
68 |
+
pipeline = None
|
69 |
|
70 |
MAX_SEED = np.iinfo(np.int32).max
|
71 |
MAX_IMAGE_SIZE = 1024
|
|
|
82 |
"""ํ๊ธ์ ์์ด๋ก ๋ฒ์ญ (OpenAI GPT-4.1-mini ์ฌ์ฉ). ์์ด ์
๋ ฅ์ด๋ฉด ๊ทธ๋๋ก ๋ฐํ."""
|
83 |
if not is_korean(text):
|
84 |
return text
|
85 |
+
|
86 |
+
if client is None:
|
87 |
+
print("Warning: OpenAI client not available, returning original text")
|
88 |
+
return text
|
89 |
|
90 |
for attempt in range(retries):
|
91 |
try:
|
92 |
res = client.chat.completions.create(
|
93 |
+
model="gpt-4o-mini", # Fixed: gpt-4.1-mini doesn't exist, use gpt-4o-mini
|
94 |
messages=[
|
95 |
{
|
96 |
"role": "system",
|
|
|
102 |
max_tokens=256,
|
103 |
)
|
104 |
return res.choices[0].message.content.strip()
|
105 |
+
except Exception as e:
|
106 |
print(f"[translate] attempt {attempt + 1} failed: {e}")
|
107 |
time.sleep(2)
|
108 |
return text # ๋ฒ์ญ ์คํจ ์ ์๋ฌธ ๊ทธ๋๋ก
|
|
|
135 |
# ===== Diffusion ํธ์ถ =====
|
136 |
|
137 |
def run_pipeline(prompt: str, seed: int, width: int, height: int, guidance_scale: float, num_steps: int, lora_scale: float):
|
138 |
+
if pipeline is None:
|
139 |
+
raise ValueError("Model pipeline not loaded")
|
140 |
+
|
141 |
generator = torch.Generator(device=device).manual_seed(int(seed))
|
142 |
result = pipeline(
|
143 |
prompt=prompt,
|
|
|
165 |
lora_scale: float = 1.0,
|
166 |
progress=None,
|
167 |
):
|
168 |
+
try:
|
169 |
+
if randomize_seed:
|
170 |
+
seed = random.randint(0, MAX_SEED)
|
171 |
|
172 |
+
# 1) ๋ฒ์ญ + ์ฆ๊ฐ
|
173 |
+
final_prompt = prepare_prompt(user_prompt, style_key)
|
174 |
+
print(f"Final prompt: {final_prompt}")
|
175 |
|
176 |
+
# 2) ํ์ดํ๋ผ์ธ ํธ์ถ
|
177 |
+
image = run_pipeline(final_prompt, seed, width, height, guidance_scale, num_inference_steps, lora_scale)
|
178 |
|
179 |
+
# 3) ์ ์ฅ
|
180 |
+
save_generated_image(image, final_prompt)
|
181 |
|
182 |
+
return image, seed
|
183 |
+
|
184 |
+
except Exception as e:
|
185 |
+
print(f"Error generating image: {e}")
|
186 |
+
# Return a placeholder or error message
|
187 |
+
error_image = Image.new('RGB', (width, height), color='red')
|
188 |
+
return error_image, seed
|
189 |
|
190 |
# ===== ์์ ํ๋กฌํํธ (ํ๊ตญ์ด/์์ด ํผ์ฉ ํ์ฉ) =====
|
191 |
|
|
|
196 |
"Mr. KIM์ด ๋ถ๋น๋ ๊ฑฐ๋ฆฌ์์ ์ฌ์ฑ ์๋ฏผ๋ค๊ณผ ๋ฐ๋ปํ๊ฒ ์
์ํ๋ ๏ฟฝ๏ฟฝ์ต, ์ฌ์ฑ ์ ๊ถ์๋ค์ ๋ํ ์ง์ ํ ๊ด์ฌ๊ณผ ์ํต์ ๋ณด์ฌ์ฃผ๊ณ ์๋ค.",
|
197 |
"Mr. KIM์ด ์ ๊ฑฐ ์ ์ธ์ฅ์์ ์งํ์ ์ ํฅํด ์๊ฐ๋ฝ์ผ๋ก ๊ฐ๋ฆฌํค๋ฉฐ ์๊ฐ์ ์ฃผ๋ ์ ์ค์ฒ๋ฅผ ์ทจํ๊ณ ์๊ณ , ์ฌ์ฑ๋ค๊ณผ ์์ด๋ค์ด ๋ฐ์๋ฅผ ์น๊ณ ์๋ค.",
|
198 |
"Mr. KIM์ด ์ง์ญ ํ์ฌ์ ์ฐธ์ฌํ์ฌ ์ด์ ์ ์ผ๋ก ์์ํ๋ ์ฌ์ฑ ์ง์ง์๋ค์๊ฒ ๋๋ฌ์ธ์ฌ ์๋ ๋ชจ์ต.",
|
199 |
+
"Mr. KIM visiting a local market, engaging in friendly conversation with female vendors and shopkeepers.",
|
200 |
+
"Mr. KIM walking through a university campus, discussing education policies with female students and professors.",
|
201 |
+
"Mr. KIM delivering a powerful speech in front of a large crowd with confident gestures and determined expression.",
|
202 |
"Mr. KIM in a dynamic interview setting, passionately outlining his visions for the future.",
|
203 |
+
"Mr. KIM preparing for an important debate, surrounded by paperwork, looking focused and resolute.",
|
204 |
]
|
205 |
+
|
206 |
# ===== ์ปค์คํ
CSS (๋ถ์ ํค ์ ์ง) =====
|
207 |
custom_css = """
|
208 |
:root {
|
|
|
226 |
"""
|
227 |
|
228 |
# ===== Gradio UI =====
|
229 |
+
def create_interface():
|
230 |
+
with gr.Blocks(css=custom_css, analytics_enabled=False) as demo:
|
231 |
+
gr.HTML('<div class="title">Mr. KIM in KOREA</div>')
|
232 |
+
gr.HTML('<div class="collection-link"><a href="https://huggingface.co/collections/openfree/painting-art-ai-681453484ec15ef5978bbeb1" target="_blank">Visit the LoRA Model Collection</a></div>')
|
233 |
+
|
234 |
+
with gr.Group(elem_classes="model-description"):
|
235 |
+
gr.HTML("""
|
236 |
+
<p>
|
237 |
+
๋ณธ ๋ชจ๋ธ์ ์ฐ๊ตฌ ๋ชฉ์ ์ผ๋ก ํน์ ์ธ์ ์ผ๊ตด๊ณผ ์ธ๋ชจ๋ฅผ ํ์ตํ LoRA ๋ชจ๋ธ์
๋๋ค.<br>
|
238 |
+
๋ชฉ์ ์ธ์ ์ฉ๋๋ก ๋ฌด๋จ ์ฌ์ฉ ์๋๋ก ์ ์ํด ์ฃผ์ธ๏ฟฝ๏ฟฝ.<br>
|
239 |
+
(์์ prompt ์ฌ์ฉ ์ ๋ฐ๋์ 'kim'์ ํฌํจํ์ฌ์ผ ์ต์ ์ ๊ฒฐ๊ณผ๋ฅผ ์ป์ ์ ์์ต๋๋ค.)
|
240 |
+
</p>
|
241 |
+
""")
|
242 |
+
|
243 |
+
# ===== ๋ฉ์ธ ์
๋ ฅ =====
|
244 |
+
with gr.Column():
|
245 |
+
with gr.Row(elem_classes="input-container"):
|
246 |
+
user_prompt = gr.Text(label="Prompt", max_lines=1, value=examples[0])
|
247 |
+
style_select = gr.Radio(label="Style Preset", choices=list(STYLE_PRESETS.keys()), value="None", interactive=True)
|
248 |
+
run_button = gr.Button("Generate", variant="primary")
|
249 |
+
|
250 |
+
result_image = gr.Image(label="Generated Image")
|
251 |
+
seed_output = gr.Number(label="Seed")
|
252 |
+
|
253 |
+
# ===== ๊ณ ๊ธ ์ค์ =====
|
254 |
+
with gr.Accordion("Advanced Settings", open=False, elem_classes="advanced-settings"):
|
255 |
+
seed = gr.Slider(label="Seed", minimum=0, maximum=MAX_SEED, step=1, value=42)
|
256 |
+
randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
|
257 |
+
with gr.Row():
|
258 |
+
width = gr.Slider(label="Width", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=1024)
|
259 |
+
height = gr.Slider(label="Height", minimum=256, maximum=MAX_IMAGE_SIZE, step=32, value=768)
|
260 |
+
with gr.Row():
|
261 |
+
guidance_scale = gr.Slider(label="Guidance scale", minimum=0.0, maximum=10.0, step=0.1, value=3.5)
|
262 |
+
num_inference_steps = gr.Slider(label="Inference steps", minimum=1, maximum=50, step=1, value=30)
|
263 |
+
lora_scale = gr.Slider(label="LoRA scale", minimum=0.0, maximum=1.0, step=0.1, value=1.0)
|
264 |
+
|
265 |
+
# ===== ์์ ์์ญ =====
|
266 |
+
with gr.Group(elem_classes="example-region"):
|
267 |
+
gr.Markdown("### Examples")
|
268 |
+
gr.Examples(examples=examples, inputs=user_prompt, cache_examples=False)
|
269 |
+
|
270 |
+
# ===== ์ด๋ฒคํธ =====
|
271 |
+
run_button.click(
|
272 |
+
fn=generate_image,
|
273 |
+
inputs=[
|
274 |
+
user_prompt,
|
275 |
+
style_select,
|
276 |
+
seed,
|
277 |
+
randomize_seed,
|
278 |
+
width,
|
279 |
+
height,
|
280 |
+
guidance_scale,
|
281 |
+
num_inference_steps,
|
282 |
+
lora_scale,
|
283 |
+
],
|
284 |
+
outputs=[result_image, seed_output],
|
285 |
+
)
|
286 |
+
|
287 |
+
return demo
|
288 |
+
|
289 |
+
# ===== ์ ํ๋ฆฌ์ผ์ด์
์คํ =====
|
290 |
+
if __name__ == "__main__":
|
291 |
+
demo = create_interface()
|
292 |
+
demo.queue()
|
293 |
+
demo.launch()
|