Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,8 +31,8 @@ os.environ["TRANSFORMERS_CACHE"] = CACHE_PATH
|
|
| 31 |
os.environ["HF_HUB_CACHE"] = CACHE_PATH
|
| 32 |
os.environ["HF_HOME"] = CACHE_PATH
|
| 33 |
|
| 34 |
-
# Google GenAI
|
| 35 |
-
#
|
| 36 |
|
| 37 |
# ์์
์๊ฐ ์ธก์ ์ ์ํ ๊ฐ๋จํ ํ์ด๋จธ ํด๋์ค
|
| 38 |
class timer:
|
|
@@ -82,13 +82,21 @@ def generate_by_google_genai(text, file_name, model="gemini-2.0-flash-exp"):
|
|
| 82 |
- file_name: ์๋ณธ ์ด๋ฏธ์ง(์: .png) ๊ฒฝ๋ก
|
| 83 |
- model: ์ฌ์ฉํ gemini ๋ชจ๋ธ ์ด๋ฆ
|
| 84 |
"""
|
| 85 |
-
# 1)
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
#
|
| 89 |
files = [client.files.upload(file=file_name)]
|
| 90 |
|
| 91 |
-
#
|
| 92 |
contents = [
|
| 93 |
types.Content(
|
| 94 |
role="user",
|
|
@@ -102,7 +110,7 @@ def generate_by_google_genai(text, file_name, model="gemini-2.0-flash-exp"):
|
|
| 102 |
),
|
| 103 |
]
|
| 104 |
|
| 105 |
-
#
|
| 106 |
generate_content_config = types.GenerateContentConfig(
|
| 107 |
temperature=1,
|
| 108 |
top_p=0.95,
|
|
@@ -133,18 +141,21 @@ def generate_by_google_genai(text, file_name, model="gemini-2.0-flash-exp"):
|
|
| 133 |
save_binary_file(temp_path, candidate.inline_data.data)
|
| 134 |
print(f"File of mime type {candidate.inline_data.mime_type} saved to: {temp_path}")
|
| 135 |
image_path = temp_path
|
| 136 |
-
|
|
|
|
| 137 |
else:
|
| 138 |
-
#
|
| 139 |
text_response += chunk.text + "\n"
|
| 140 |
|
| 141 |
-
#
|
| 142 |
del files
|
| 143 |
|
| 144 |
return image_path, text_response
|
| 145 |
|
| 146 |
#######################################
|
| 147 |
-
# 3. Gradio ํจ์:
|
|
|
|
|
|
|
| 148 |
#######################################
|
| 149 |
|
| 150 |
def generate_initial_image(prompt, text, height, width, steps, scale, seed):
|
|
@@ -153,10 +164,7 @@ def generate_initial_image(prompt, text, height, width, steps, scale, seed):
|
|
| 153 |
prompt: ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ/์ฅ๋ฉด/์คํ์ผ ๋ฌ์ฌ๋ฅผ ์ํ ํ๋กฌํํธ
|
| 154 |
text: ์ค์ ๋ก ์ด๋ฏธ์ง์ ๋ค์ด๊ฐ์ผ ํ ๋ฌธ๊ตฌ(์: "์๋
ํ์ธ์", "Hello world" ๋ฑ)
|
| 155 |
"""
|
| 156 |
-
#
|
| 157 |
-
# Diffusion ๋ชจ๋ธ์ ๋ฐ๋ผ ์ ๋ฐ์๋์ง ์์ ์๋ ์์ผ๋, ๊ตฌ์ฒด์ ์ผ๋ก ๊ธฐ์ฌํ ์๋ก ์ ๋ฆฌ.
|
| 158 |
-
# ์: "A poster with large bold Korean text that says '์๋
ํ์ธ์' in red color ..."
|
| 159 |
-
# ์ฌ๊ธฐ์๋ ๊ฐ๋จํ prompt ๋ค์ ํ
์คํธ ์ฝ์
์์๋ฅผ ๋ณด์ฌ์ค
|
| 160 |
combined_prompt = f"{prompt} with clear readable text that says '{text}'"
|
| 161 |
|
| 162 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
|
@@ -200,6 +208,7 @@ def change_text_in_image(original_image, new_text):
|
|
| 200 |
return None, text_response
|
| 201 |
|
| 202 |
except Exception as e:
|
|
|
|
| 203 |
raise gr.Error(f"Error: {e}")
|
| 204 |
|
| 205 |
|
|
@@ -212,12 +221,10 @@ with gr.Blocks(title="Flux + Google GenAI Text Replacement") as demo:
|
|
| 212 |
"""
|
| 213 |
# Flux ๊ธฐ๋ฐ ์ด๋ฏธ์ง ์์ฑ + Google GenAI๋ฅผ ํตํ ํ
์คํธ ๋ณํ
|
| 214 |
**์ด ๋ฐ๋ชจ๋ ์๋ ๋ ๋จ๊ณ๋ฅผ ๋ณด์ฌ์ค๋๋ค.**
|
| 215 |
-
|
| 216 |
1) **Diffusion ๋ชจ๋ธ(FluxPipeline)์ ์ด์ฉํด** ์ด๋ฏธ์ง ์์ฑ.
|
| 217 |
- ์ด๋, ์ฌ์ฉ์๊ฐ ์ง์ ํ ํ
์คํธ๋ฅผ ์ด๋ฏธ์ง ์์ ํ์ํ๋๋ก ์๋ํฉ๋๋ค.
|
| 218 |
2) **์์ฑ๋ ์ด๋ฏธ์ง๋ฅผ Google GenAI(gemini) ๋ชจ๋ธ์ ์ ๋ฌ**ํ์ฌ,
|
| 219 |
- ์ด๋ฏธ์ง ๋ด ํ
์คํธ ๋ถ๋ถ๋ง ๋ค๋ฅธ ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ.
|
| 220 |
-
|
| 221 |
---
|
| 222 |
"""
|
| 223 |
)
|
|
@@ -273,9 +280,5 @@ with gr.Blocks(title="Flux + Google GenAI Text Replacement") as demo:
|
|
| 273 |
outputs=[output_img, output_txt]
|
| 274 |
)
|
| 275 |
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
# demo.queue(concurrency_count=1, max_size=20).launch()
|
| 279 |
-
|
| 280 |
-
# ์ด๋ ๊ฒ ๋ณ๏ฟฝ๏ฟฝ๏ฟฝ
|
| 281 |
-
demo.launch(max_threads=20) # ์ ์ฒด ์์
์ ์ ์ค์
|
|
|
|
| 31 |
os.environ["HF_HUB_CACHE"] = CACHE_PATH
|
| 32 |
os.environ["HF_HOME"] = CACHE_PATH
|
| 33 |
|
| 34 |
+
# (์์) Google GenAI ์ฌ์ฉ:
|
| 35 |
+
# export GAPI_TOKEN="<YOUR_GOOGLE_GENAI_API_KEY>"
|
| 36 |
|
| 37 |
# ์์
์๊ฐ ์ธก์ ์ ์ํ ๊ฐ๋จํ ํ์ด๋จธ ํด๋์ค
|
| 38 |
class timer:
|
|
|
|
| 82 |
- file_name: ์๋ณธ ์ด๋ฏธ์ง(์: .png) ๊ฒฝ๋ก
|
| 83 |
- model: ์ฌ์ฉํ gemini ๋ชจ๋ธ ์ด๋ฆ
|
| 84 |
"""
|
| 85 |
+
# (1) ํ๊ฒฝ ๋ณ์์์ API ํค ๊ฐ์ ธ์ค๊ธฐ (ํ์)
|
| 86 |
+
api_key = os.getenv("GAPI_TOKEN", None)
|
| 87 |
+
if not api_key:
|
| 88 |
+
raise ValueError(
|
| 89 |
+
"GAPI_TOKEN ํ๊ฒฝ ๋ณ์๊ฐ ์ค์ ๋์ง ์์์ต๋๋ค. "
|
| 90 |
+
"Google GenAI API๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ GAPI_TOKEN์ด ํ์ํฉ๋๋ค."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# (2) Google Client ์ด๊ธฐํ
|
| 94 |
+
client = genai.Client(api_key=api_key)
|
| 95 |
|
| 96 |
+
# (3) ์ด๋ฏธ์ง ์
๋ก๋
|
| 97 |
files = [client.files.upload(file=file_name)]
|
| 98 |
|
| 99 |
+
# (4) gemini์ ์ ๋ฌํ Content ์ค๋น (์ด๋ฏธ์ง + ํ๋กฌํํธ)
|
| 100 |
contents = [
|
| 101 |
types.Content(
|
| 102 |
role="user",
|
|
|
|
| 110 |
),
|
| 111 |
]
|
| 112 |
|
| 113 |
+
# (5) ์์ฑ/๋ณํ ์ค์
|
| 114 |
generate_content_config = types.GenerateContentConfig(
|
| 115 |
temperature=1,
|
| 116 |
top_p=0.95,
|
|
|
|
| 141 |
save_binary_file(temp_path, candidate.inline_data.data)
|
| 142 |
print(f"File of mime type {candidate.inline_data.mime_type} saved to: {temp_path}")
|
| 143 |
image_path = temp_path
|
| 144 |
+
# ์ด๋ฏธ์ง๊ฐ ์์ฑ๋๋ฉด ๋ณดํต ์ฌ๊ธฐ์ break
|
| 145 |
+
break
|
| 146 |
else:
|
| 147 |
+
# ์ด๋ฏธ์ง ์์ด ํ
์คํธ๋ง ๋ฐํ๋๋ ๊ฒฝ์ฐ(ํฌ๋ฐ) ๋์
|
| 148 |
text_response += chunk.text + "\n"
|
| 149 |
|
| 150 |
+
# ์
๋ก๋ํ File ๊ฐ์ฒด ์ ๊ฑฐ
|
| 151 |
del files
|
| 152 |
|
| 153 |
return image_path, text_response
|
| 154 |
|
| 155 |
#######################################
|
| 156 |
+
# 3. Gradio ํจ์:
|
| 157 |
+
# (1) FLUX๋ก ์ด๋ฏธ์ง ์์ฑ ->
|
| 158 |
+
# (2) Google GenAI๋ก ํ
์คํธ ๊ต์ฒด
|
| 159 |
#######################################
|
| 160 |
|
| 161 |
def generate_initial_image(prompt, text, height, width, steps, scale, seed):
|
|
|
|
| 164 |
prompt: ์ด๋ฏธ์ง ๋ฐฐ๊ฒฝ/์ฅ๋ฉด/์คํ์ผ ๋ฌ์ฌ๋ฅผ ์ํ ํ๋กฌํํธ
|
| 165 |
text: ์ค์ ๋ก ์ด๋ฏธ์ง์ ๋ค์ด๊ฐ์ผ ํ ๋ฌธ๊ตฌ(์: "์๋
ํ์ธ์", "Hello world" ๋ฑ)
|
| 166 |
"""
|
| 167 |
+
# Diffusion ๋ชจ๋ธ์ "์ด๋ฏธ์ง ์์ ํ
์คํธ๋ฅผ ํ์ํ๋ผ"๋ ์์ฒญ์ ๋ฃ๊ธฐ ์ํด
|
|
|
|
|
|
|
|
|
|
| 168 |
combined_prompt = f"{prompt} with clear readable text that says '{text}'"
|
| 169 |
|
| 170 |
with torch.inference_mode(), torch.autocast("cuda", dtype=torch.bfloat16), timer("inference"):
|
|
|
|
| 208 |
return None, text_response
|
| 209 |
|
| 210 |
except Exception as e:
|
| 211 |
+
# Gradio ์๋ฌ ํ์๋ฅผ ์ํด gr.Error๋ฅผ ์ฌ์ฉ
|
| 212 |
raise gr.Error(f"Error: {e}")
|
| 213 |
|
| 214 |
|
|
|
|
| 221 |
"""
|
| 222 |
# Flux ๊ธฐ๋ฐ ์ด๋ฏธ์ง ์์ฑ + Google GenAI๋ฅผ ํตํ ํ
์คํธ ๋ณํ
|
| 223 |
**์ด ๋ฐ๋ชจ๋ ์๋ ๋ ๋จ๊ณ๋ฅผ ๋ณด์ฌ์ค๋๋ค.**
|
|
|
|
| 224 |
1) **Diffusion ๋ชจ๋ธ(FluxPipeline)์ ์ด์ฉํด** ์ด๋ฏธ์ง ์์ฑ.
|
| 225 |
- ์ด๋, ์ฌ์ฉ์๊ฐ ์ง์ ํ ํ
์คํธ๋ฅผ ์ด๋ฏธ์ง ์์ ํ์ํ๋๋ก ์๋ํฉ๋๋ค.
|
| 226 |
2) **์์ฑ๋ ์ด๋ฏธ์ง๋ฅผ Google GenAI(gemini) ๋ชจ๋ธ์ ์ ๋ฌ**ํ์ฌ,
|
| 227 |
- ์ด๋ฏธ์ง ๋ด ํ
์คํธ ๋ถ๋ถ๋ง ๋ค๋ฅธ ๋ฌธ์์ด๋ก ๋ณ๊ฒฝ.
|
|
|
|
| 228 |
---
|
| 229 |
"""
|
| 230 |
)
|
|
|
|
| 280 |
outputs=[output_img, output_txt]
|
| 281 |
)
|
| 282 |
|
| 283 |
+
# Gradio ์คํ
|
| 284 |
+
demo.launch(max_threads=20)
|
|
|
|
|
|
|
|
|
|
|
|