Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,6 +7,8 @@ import base64
|
|
| 7 |
import mimetypes
|
| 8 |
import json
|
| 9 |
import io
|
|
|
|
|
|
|
| 10 |
|
| 11 |
import torch
|
| 12 |
from PIL import Image
|
|
@@ -122,7 +124,7 @@ def generate_by_google_genai(text, file_name, model="gemini-2.0-flash-exp"):
|
|
| 122 |
# ์์ ํ์ผ๋ก ์ด๋ฏธ์ง ๋ฐ์ ์ค๋น
|
| 123 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 124 |
temp_path = tmp.name
|
| 125 |
-
# ์๋ต
|
| 126 |
for chunk in client.models.generate_content_stream(
|
| 127 |
model=model,
|
| 128 |
contents=contents,
|
|
@@ -148,8 +150,28 @@ def generate_by_google_genai(text, file_name, model="gemini-2.0-flash-exp"):
|
|
| 148 |
return image_path, text_response
|
| 149 |
|
| 150 |
#######################################
|
| 151 |
-
# 3.
|
| 152 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
#######################################
|
| 154 |
|
| 155 |
def generate_initial_image(prompt, text, height, width, steps, scale, seed):
|
|
@@ -162,8 +184,7 @@ def generate_initial_image(prompt, text, height, width, steps, scale, seed):
|
|
| 162 |
combined_prompt = prompt.replace("<text>", text)
|
| 163 |
else:
|
| 164 |
combined_prompt = f"{prompt} with clear readable text that says '{text}'"
|
| 165 |
-
|
| 166 |
-
# ๋๋ฒ๊ทธ์ฉ: ์ต์ข
๋ค์ด๊ฐ๋ ํ๋กฌํํธ๋ฅผ ํ์ธ
|
| 167 |
print(f"[DEBUG] Final combined_prompt: {combined_prompt}")
|
| 168 |
|
| 169 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
|
@@ -183,47 +204,43 @@ def change_text_in_image(original_image, new_text):
|
|
| 183 |
"""
|
| 184 |
Google GenAI์ gemini ๋ชจ๋ธ์ ํตํด,
|
| 185 |
์
๋ก๋๋ ์ด๋ฏธ์ง ๋ด๋ถ์ ๋ฌธ๊ตฌ๋ฅผ `new_text`๋ก ๋ณ๊ฒฝํด์ฃผ๋ ํจ์.
|
|
|
|
| 186 |
"""
|
| 187 |
try:
|
| 188 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 189 |
original_path = tmp.name
|
| 190 |
original_image.save(original_path)
|
| 191 |
|
| 192 |
-
# Gemini ๋ชจ๋ธ ํธ์ถ
|
| 193 |
image_path, text_response = generate_by_google_genai(
|
| 194 |
text=f"Change the text in this image to: '{new_text}'",
|
| 195 |
file_name=original_path
|
| 196 |
)
|
| 197 |
|
| 198 |
if image_path:
|
| 199 |
-
# Gradio ๊ตฌ๋ฒ์ ์๋ decode_base64_to_image๊ฐ ์์ผ๋ฏ๋ก PIL๋ก ์ฒ๋ฆฌ
|
| 200 |
with open(image_path, "rb") as f:
|
| 201 |
image_data = f.read()
|
| 202 |
modified_img = Image.open(io.BytesIO(image_data))
|
| 203 |
return modified_img, ""
|
| 204 |
else:
|
| 205 |
-
# ์ด๋ฏธ์ง๊ฐ ์์ด ํ
์คํธ๋ง ๋ฐํ๋ ๊ฒฝ์ฐ
|
| 206 |
return None, text_response
|
| 207 |
|
| 208 |
except Exception as e:
|
| 209 |
raise gr.Error(f"Error: {e}")
|
| 210 |
|
| 211 |
#######################################
|
| 212 |
-
#
|
| 213 |
#######################################
|
| 214 |
|
| 215 |
-
with gr.Blocks(title="Flux + Google GenAI Text Replacement") as demo:
|
| 216 |
gr.Markdown(
|
| 217 |
"""
|
| 218 |
-
# Flux ๊ธฐ๋ฐ ์ด๋ฏธ์ง ์์ฑ + Google GenAI
|
| 219 |
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
`with clear readable text that says '<text>'`.
|
| 226 |
-
- Finally, you can optionally change the text again via Gemini.
|
| 227 |
|
| 228 |
---
|
| 229 |
"""
|
|
@@ -253,25 +270,47 @@ with gr.Blocks(title="Flux + Google GenAI Text Replacement") as demo:
|
|
| 253 |
generated_image = gr.Image(label="Generated Image (with text)", type="pil")
|
| 254 |
|
| 255 |
with gr.Column():
|
| 256 |
-
gr.Markdown(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
new_text_input = gr.Textbox(
|
| 258 |
-
label="์๋ก ๋ฐ๊ฟ ํ
์คํธ",
|
| 259 |
-
placeholder="์)
|
| 260 |
)
|
| 261 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 262 |
output_img = gr.Image(label="Modified Image", type="pil")
|
| 263 |
output_txt = gr.Textbox(label="(If only text returned)")
|
| 264 |
|
| 265 |
-
|
|
|
|
|
|
|
| 266 |
generate_btn.click(
|
| 267 |
fn=generate_initial_image,
|
| 268 |
inputs=[prompt_input, text_input, height, width, steps, scale, seed],
|
| 269 |
outputs=[generated_image]
|
| 270 |
)
|
| 271 |
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
fn=change_text_in_image,
|
| 274 |
-
inputs=[generated_image,
|
| 275 |
outputs=[output_img, output_txt]
|
| 276 |
)
|
| 277 |
|
|
|
|
| 7 |
import mimetypes
|
| 8 |
import json
|
| 9 |
import io
|
| 10 |
+
import random
|
| 11 |
+
import string
|
| 12 |
|
| 13 |
import torch
|
| 14 |
from PIL import Image
|
|
|
|
| 124 |
# ์์ ํ์ผ๋ก ์ด๋ฏธ์ง ๋ฐ์ ์ค๋น
|
| 125 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 126 |
temp_path = tmp.name
|
| 127 |
+
# ์๋ต ์คํธ๋ฆผ ์ฒ๋ฆฌ
|
| 128 |
for chunk in client.models.generate_content_stream(
|
| 129 |
model=model,
|
| 130 |
contents=contents,
|
|
|
|
| 150 |
return image_path, text_response
|
| 151 |
|
| 152 |
#######################################
|
| 153 |
+
# 3. ์์ ์ํ๋ฒณ ์์ฑ์ฉ ํจ์
|
| 154 |
+
#######################################
|
| 155 |
+
|
| 156 |
+
def generate_random_letters(length: int) -> str:
|
| 157 |
+
"""
|
| 158 |
+
length ๊ธธ์ด์ ์์์ ์ํ๋ฒณ ๋ฌธ์์ด์ ์์ฑํด ๋ฐํ.
|
| 159 |
+
๋๋ฌธ์+์๋ฌธ์(a-z, A-Z) ๋ฒ์์์ ๋๋ค ๋ฝ๊ธฐ.
|
| 160 |
+
"""
|
| 161 |
+
letters = string.ascii_uppercase + string.ascii_lowercase
|
| 162 |
+
return "".join(random.choice(letters) for _ in range(length))
|
| 163 |
+
|
| 164 |
+
def fill_text_input_with_random(new_text: str) -> str:
|
| 165 |
+
"""
|
| 166 |
+
"์๋ก ๋ฐ๊ฟ ํ
์คํธ"์ ๊ธธ์ด๋ฅผ ์ธ์ด, ๊ทธ ๊ธธ์ด๋งํผ ์์์ ์ํ๋ฒณ ๋ฌธ์์ด ์์ฑ ํ ๋ฐํ.
|
| 167 |
+
์ด ๋ฐํ๊ฐ์ด ๊ณง "์ด๋ฏธ์ง ์์ ๋ค์ด๊ฐ ํ
์คํธ" UI(=text_input)์ ํ์๋จ.
|
| 168 |
+
"""
|
| 169 |
+
length = len(new_text)
|
| 170 |
+
random_letters = generate_random_letters(length)
|
| 171 |
+
return random_letters
|
| 172 |
+
|
| 173 |
+
#######################################
|
| 174 |
+
# 4. Gradio ํจ์
|
| 175 |
#######################################
|
| 176 |
|
| 177 |
def generate_initial_image(prompt, text, height, width, steps, scale, seed):
|
|
|
|
| 184 |
combined_prompt = prompt.replace("<text>", text)
|
| 185 |
else:
|
| 186 |
combined_prompt = f"{prompt} with clear readable text that says '{text}'"
|
| 187 |
+
|
|
|
|
| 188 |
print(f"[DEBUG] Final combined_prompt: {combined_prompt}")
|
| 189 |
|
| 190 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
|
|
|
| 204 |
"""
|
| 205 |
Google GenAI์ gemini ๋ชจ๋ธ์ ํตํด,
|
| 206 |
์
๋ก๋๋ ์ด๋ฏธ์ง ๋ด๋ถ์ ๋ฌธ๊ตฌ๋ฅผ `new_text`๋ก ๋ณ๊ฒฝํด์ฃผ๋ ํจ์.
|
| 207 |
+
(์ฌ๊ธฐ์๋ new_text๊ฐ ์ด๋ฏธ ์์์ ์ํ๋ฒณ์ผ๋ก ์ฑ์์ง ์ํ๊ฐ ๋จ)
|
| 208 |
"""
|
| 209 |
try:
|
| 210 |
with tempfile.NamedTemporaryFile(suffix=".png", delete=False) as tmp:
|
| 211 |
original_path = tmp.name
|
| 212 |
original_image.save(original_path)
|
| 213 |
|
|
|
|
| 214 |
image_path, text_response = generate_by_google_genai(
|
| 215 |
text=f"Change the text in this image to: '{new_text}'",
|
| 216 |
file_name=original_path
|
| 217 |
)
|
| 218 |
|
| 219 |
if image_path:
|
|
|
|
| 220 |
with open(image_path, "rb") as f:
|
| 221 |
image_data = f.read()
|
| 222 |
modified_img = Image.open(io.BytesIO(image_data))
|
| 223 |
return modified_img, ""
|
| 224 |
else:
|
|
|
|
| 225 |
return None, text_response
|
| 226 |
|
| 227 |
except Exception as e:
|
| 228 |
raise gr.Error(f"Error: {e}")
|
| 229 |
|
| 230 |
#######################################
|
| 231 |
+
# 5. Gradio ์ธํฐํ์ด์ค ๊ตฌ์ฑ
|
| 232 |
#######################################
|
| 233 |
|
| 234 |
+
with gr.Blocks(title="Flux + Google GenAI + Random Text Replacement") as demo:
|
| 235 |
gr.Markdown(
|
| 236 |
"""
|
| 237 |
+
# Flux ๊ธฐ๋ฐ ์ด๋ฏธ์ง ์์ฑ + Google GenAI + ์์ ์ํ๋ฒณ ์นํ
|
| 238 |
|
| 239 |
+
**๊ธฐ๋ฅ ์์ฝ**
|
| 240 |
+
1) "์๋ก ๋ฐ๊ฟ ํ
์คํธ"์ ์
๋ ฅ๋ ๋ฌธ์์ด ๊ธธ์ด๋ฅผ ์ธก์
|
| 241 |
+
2) ๊ทธ ๊ธธ์ด๋งํผ **๋ฌด์์ ์ํ๋ฒณ**(๋์๋ฌธ์)์ผ๋ก ๊ตฌ์ฑ๋ ๋ฌธ์์ด์ ๋ง๋ค์ด
|
| 242 |
+
3) "์ด๋ฏธ์ง ์์ ๋ค์ด๊ฐ ํ
์คํธ" ์นธ์ ์๋์ผ๋ก ์
๋ ฅ (์ฆ, ์ค์ ๋ฐ๋ ํ
์คํธ๋ ์์ ํ ๋๋ค)
|
| 243 |
+
4) ๊ทธ ํ Google GenAI(gemini)๋ก ์ด๋ฏธ์ง ๋ฌธ์๋ฅผ ๊ต์ฒด
|
|
|
|
|
|
|
| 244 |
|
| 245 |
---
|
| 246 |
"""
|
|
|
|
| 270 |
generated_image = gr.Image(label="Generated Image (with text)", type="pil")
|
| 271 |
|
| 272 |
with gr.Column():
|
| 273 |
+
gr.Markdown(
|
| 274 |
+
"""
|
| 275 |
+
## 2) Step 2: "์๋ก ๋ฐ๊ฟ ํ
์คํธ" ๊ธธ์ด๋ฅผ ๊ธฐ์ค์ผ๋ก
|
| 276 |
+
์์ ์ํ๋ฒณ์ "์ด๋ฏธ์ง ์์ ๋ค์ด๊ฐ ํ
์คํธ"๋ก ์๋ ์ค์ โ ์ด๋ฏธ์ง ๊ต์ฒด
|
| 277 |
+
"""
|
| 278 |
+
)
|
| 279 |
new_text_input = gr.Textbox(
|
| 280 |
+
label="์๋ก ๋ฐ๊ฟ ํ
์คํธ (์
๋ ฅ๋ ๊ธธ์ด๋ฅผ ๊ธฐ์ค์ผ๋ก ๋ฌด์์ ์ํ๋ฒณ ์์ฑ)",
|
| 281 |
+
placeholder="์) Hello123 (๊ธธ์ด 8์ด๋ฉด, 8๊ธ์ ๋๋ค)"
|
| 282 |
)
|
| 283 |
+
|
| 284 |
+
# (A) ๋จผ์ "์๋ก ๋ฐ๊ฟ ํ
์คํธ" ๊ธ์์๋ฅผ ์ธ์ด, ๋ฌด์์ ์ํ๋ฒณ์ผ๋ก text_input ์ฑ์ฐ๊ธฐ
|
| 285 |
+
# (B) ๊ทธ๋ฐ ๋ค change_text_in_image() ์คํ
|
| 286 |
+
modify_btn = gr.Button("Generate Random Letters & Change Text in Image", variant="secondary")
|
| 287 |
+
|
| 288 |
+
# ์ต์ข
๊ฒฐ๊ณผ
|
| 289 |
output_img = gr.Image(label="Modified Image", type="pil")
|
| 290 |
output_txt = gr.Textbox(label="(If only text returned)")
|
| 291 |
|
| 292 |
+
###########################
|
| 293 |
+
# 1) Step1 ๋ฒํผ ์ก์
|
| 294 |
+
###########################
|
| 295 |
generate_btn.click(
|
| 296 |
fn=generate_initial_image,
|
| 297 |
inputs=[prompt_input, text_input, height, width, steps, scale, seed],
|
| 298 |
outputs=[generated_image]
|
| 299 |
)
|
| 300 |
|
| 301 |
+
###########################
|
| 302 |
+
# 2) Step2 ๋ฒํผ ์ก์
|
| 303 |
+
###########################
|
| 304 |
+
# (A) ๋ฌด์์ ์ํ๋ฒณ ์์ฑํด text_input์ ๋ฐ์
|
| 305 |
+
chain = modify_btn.click(
|
| 306 |
+
fn=fill_text_input_with_random,
|
| 307 |
+
inputs=[new_text_input],
|
| 308 |
+
outputs=[text_input]
|
| 309 |
+
)
|
| 310 |
+
# (B) ๋ฐฉ๊ธ ์
๋ฐ์ดํธ๋ text_input ๊ฐ์ ์ด์ฉํด, ์ด๋ฏธ์ง ํ
์คํธ ๊ต์ฒด
|
| 311 |
+
chain.then(
|
| 312 |
fn=change_text_in_image,
|
| 313 |
+
inputs=[generated_image, text_input],
|
| 314 |
outputs=[output_img, output_txt]
|
| 315 |
)
|
| 316 |
|