Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,187 +1,144 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
| 3 |
-
from google.genai import types
|
| 4 |
import os
|
| 5 |
from typing import Optional, List
|
| 6 |
from huggingface_hub import whoami
|
| 7 |
-
from PIL import Image
|
|
|
|
| 8 |
from io import BytesIO
|
| 9 |
import tempfile
|
| 10 |
|
| 11 |
-
# ---
|
| 12 |
-
|
| 13 |
-
GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
|
| 14 |
-
GOOGLE_API_KEY = os.getenv("GOOGLE_API_KEY")
|
| 15 |
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
)
|
| 24 |
-
|
| 25 |
-
# Note: Gemini models don't directly generate images - they analyze/describe them
|
| 26 |
-
# For image generation, you'd need to use a different API like Imagen
|
| 27 |
-
# This code is updated to work with text generation about images
|
| 28 |
-
GEMINI_MODEL_NAME = 'gemini-2.0-flash-exp' # Updated model name
|
| 29 |
-
|
| 30 |
-
def verify_pro_status(token: Optional[gr.OAuthToken]) -> bool:
|
| 31 |
-
"""Verifies if the user is a Hugging Face PRO user or part of an enterprise org."""
|
| 32 |
if not token:
|
| 33 |
return False
|
| 34 |
try:
|
| 35 |
user_info = whoami(token=token.token)
|
| 36 |
-
if user_info
|
| 37 |
-
return True
|
| 38 |
-
orgs = user_info.get("orgs", [])
|
| 39 |
-
if any(org.get("isEnterprise", False) for org in orgs):
|
| 40 |
-
return True
|
| 41 |
-
return False
|
| 42 |
except Exception as e:
|
| 43 |
-
print(f"Could not verify user's
|
| 44 |
return False
|
| 45 |
|
| 46 |
-
def _extract_image_data_from_response(response) -> Optional[bytes]:
|
| 47 |
-
"""Helper to extract image data from the model's response."""
|
| 48 |
-
# Debug: Print response structure
|
| 49 |
-
print(f"Response type: {type(response)}")
|
| 50 |
-
|
| 51 |
-
# Note: Gemini doesn't generate images directly
|
| 52 |
-
# This would need to be replaced with actual image generation API
|
| 53 |
-
# For now, return None to indicate no image was generated
|
| 54 |
-
|
| 55 |
-
if hasattr(response, 'text'):
|
| 56 |
-
print(f"Response text: {response.text[:200] if response.text else 'Empty'}")
|
| 57 |
-
|
| 58 |
-
return None
|
| 59 |
-
|
| 60 |
def run_single_image_logic(prompt: str, image_path: Optional[str] = None, progress=gr.Progress()) -> str:
|
| 61 |
-
"""Handles text or image
|
| 62 |
try:
|
| 63 |
progress(0.2, desc="๐จ ์ค๋น ์ค...")
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
if image_path:
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# Text-only prompt
|
| 73 |
-
contents.append(f"Describe an image concept for: {prompt}")
|
| 74 |
-
|
| 75 |
progress(0.5, desc="โจ ์์ฑ ์ค...")
|
| 76 |
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
contents=contents,
|
| 83 |
-
)
|
| 84 |
-
except Exception as api_error:
|
| 85 |
-
# Fallback: try with just the contents as a simple string/list
|
| 86 |
-
print(f"First attempt failed: {api_error}")
|
| 87 |
-
if image_path:
|
| 88 |
-
# For image input, we need to handle it differently
|
| 89 |
-
# The API might expect a different format
|
| 90 |
-
raise gr.Error("์ด๋ฏธ์ง ์
๋ ฅ์ ํ์ฌ ์ง์๋์ง ์์ต๋๋ค. Gemini API๋ ์ด๋ฏธ์ง ์์ฑ์ด ์๋ ๋ถ์์ฉ์
๋๋ค.")
|
| 91 |
-
else:
|
| 92 |
-
# For text-only, try a simpler approach
|
| 93 |
-
response = client.models.generate_content(
|
| 94 |
-
model=GEMINI_MODEL_NAME,
|
| 95 |
-
contents=prompt
|
| 96 |
-
)
|
| 97 |
|
| 98 |
progress(0.8, desc="๐ผ๏ธ ๋ง๋ฌด๋ฆฌ ์ค...")
|
| 99 |
|
| 100 |
-
#
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
# Add text to explain the situation
|
| 112 |
-
text = "Gemini API๋ ์ด๋ฏธ์ง ์์ฑ์ ์ง์ํ์ง ์์ต๋๋ค.\n\n"
|
| 113 |
-
text += "์์ฑ๋ ์ค๋ช
:\n" + description[:200] + "..."
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
draw.text((20, y_position), line, fill='black')
|
| 119 |
-
y_position += 30
|
| 120 |
|
| 121 |
-
# Save
|
|
|
|
| 122 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile:
|
| 123 |
img.save(tmpfile.name)
|
| 124 |
progress(1.0, desc="โ
์๋ฃ!")
|
| 125 |
return tmpfile.name
|
| 126 |
else:
|
| 127 |
-
raise ValueError("
|
| 128 |
|
| 129 |
except Exception as e:
|
| 130 |
print(f"Error details: {e}")
|
| 131 |
print(f"Error type: {type(e)}")
|
| 132 |
-
raise gr.Error(f"
|
| 133 |
-
|
| 134 |
|
| 135 |
def run_multi_image_logic(prompt: str, images: List[str], progress=gr.Progress()) -> str:
|
| 136 |
"""
|
| 137 |
-
Handles multi-image
|
| 138 |
"""
|
| 139 |
if not images:
|
| 140 |
raise gr.Error("'์ฌ๋ฌ ์ด๋ฏธ์ง' ํญ์์ ์ต์ ํ ๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")
|
| 141 |
|
| 142 |
try:
|
| 143 |
progress(0.2, desc="๐จ ์ด๋ฏธ์ง ์ค๋น ์ค...")
|
| 144 |
-
contents = []
|
| 145 |
-
for image_path in images:
|
| 146 |
-
if isinstance(image_path, (list, tuple)):
|
| 147 |
-
image_path = image_path[0]
|
| 148 |
-
contents.append(Image.open(image_path))
|
| 149 |
-
contents.append(f"Analyze these images based on: {prompt}")
|
| 150 |
-
|
| 151 |
-
progress(0.5, desc="โจ ๋ถ์ ์ค...")
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 157 |
)
|
| 158 |
|
| 159 |
progress(0.8, desc="๐ผ๏ธ ๋ง๋ฌด๋ฆฌ ์ค...")
|
| 160 |
|
| 161 |
-
#
|
| 162 |
-
if
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 168 |
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
y_position += 30
|
| 173 |
|
|
|
|
|
|
|
| 174 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile:
|
| 175 |
img.save(tmpfile.name)
|
| 176 |
progress(1.0, desc="โ
์๋ฃ!")
|
| 177 |
return tmpfile.name
|
| 178 |
else:
|
| 179 |
-
raise ValueError("
|
| 180 |
|
| 181 |
except Exception as e:
|
| 182 |
print(f"Multi-image error details: {e}")
|
| 183 |
-
raise gr.Error(f"
|
| 184 |
-
|
| 185 |
|
| 186 |
# --- Gradio App UI ---
|
| 187 |
css = '''
|
|
@@ -320,8 +277,8 @@ button.selected {
|
|
| 320 |
background: #1f2937;
|
| 321 |
}
|
| 322 |
|
| 323 |
-
/*
|
| 324 |
-
.
|
| 325 |
background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
|
| 326 |
border-radius: 1rem;
|
| 327 |
padding: 2rem;
|
|
@@ -329,7 +286,7 @@ button.selected {
|
|
| 329 |
border: 2px solid #f59e0b;
|
| 330 |
}
|
| 331 |
|
| 332 |
-
.dark .
|
| 333 |
background: linear-gradient(135deg, #7c2d12 0%, #92400e 100%);
|
| 334 |
border-color: #f59e0b;
|
| 335 |
}
|
|
@@ -365,38 +322,23 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 365 |
๐ Real Nano Banana
|
| 366 |
</h1>
|
| 367 |
<p class="header-subtitle">
|
| 368 |
-
Google
|
| 369 |
</p>
|
| 370 |
</div>
|
| 371 |
''')
|
| 372 |
|
| 373 |
-
#
|
| 374 |
gr.HTML('''
|
| 375 |
-
<div style="background: #
|
| 376 |
-
border-left: 4px solid #ef4444;">
|
| 377 |
-
<p style="margin: 0; color: #991b1b; font-weight: 600;">
|
| 378 |
-
โ ๏ธ ์ฐธ๊ณ : Gemini API๋ ์ด๋ฏธ์ง ์์ฑ์ด ์๋ ๋ถ์์ ์ ๊ณตํฉ๋๋ค.
|
| 379 |
-
์ค์ ์ด๋ฏธ์ง ์์ฑ์ ์ํ์๋ฉด DALL-E, Midjourney, Stable Diffusion ๋ฑ์ ์ฌ์ฉํ์ธ์.
|
| 380 |
-
</p>
|
| 381 |
-
</div>
|
| 382 |
-
''')
|
| 383 |
-
|
| 384 |
-
# Pro User Notice
|
| 385 |
-
gr.HTML('''
|
| 386 |
-
<div style="background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
|
| 387 |
border-radius: 0.5rem; padding: 1rem; margin-bottom: 1.5rem;
|
| 388 |
-
border-left: 4px solid #
|
| 389 |
-
<p style="margin: 0; color: #
|
| 390 |
-
|
| 391 |
-
<a href="https://huggingface.co/pro" target="_blank"
|
| 392 |
-
style="color: #dc2626; text-decoration: underline;">
|
| 393 |
-
PRO ๊ตฌ๋
ํ๊ธฐ
|
| 394 |
-
</a>
|
| 395 |
</p>
|
| 396 |
</div>
|
| 397 |
''')
|
| 398 |
|
| 399 |
-
|
| 400 |
main_interface = gr.Column(visible=False, elem_classes="main-container")
|
| 401 |
|
| 402 |
with main_interface:
|
|
@@ -412,12 +354,12 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 412 |
with gr.TabItem("๐ผ๏ธ ๋จ์ผ ์ด๋ฏธ์ง", id="single") as single_tab:
|
| 413 |
image_input = gr.Image(
|
| 414 |
type="filepath",
|
| 415 |
-
label="์
๋ ฅ ์ด๋ฏธ์ง",
|
| 416 |
elem_classes="image-input"
|
| 417 |
)
|
| 418 |
gr.HTML('''
|
| 419 |
<p style="text-align: center; color: #6b7280; font-size: 0.9rem; margin-top: 0.5rem;">
|
| 420 |
-
๐ก
|
| 421 |
</p>
|
| 422 |
''')
|
| 423 |
|
|
@@ -437,15 +379,15 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 437 |
gr.HTML('<h3>โ๏ธ ํ๋กฌํํธ</h3>')
|
| 438 |
prompt_input = gr.Textbox(
|
| 439 |
label="",
|
| 440 |
-
info="AI์๊ฒ ์ํ๋
|
| 441 |
-
placeholder="์:
|
| 442 |
lines=3,
|
| 443 |
elem_classes="prompt-input"
|
| 444 |
)
|
| 445 |
|
| 446 |
# Generate Button
|
| 447 |
generate_button = gr.Button(
|
| 448 |
-
"๐
|
| 449 |
variant="primary",
|
| 450 |
elem_classes="generate-btn"
|
| 451 |
)
|
|
@@ -454,11 +396,13 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 454 |
with gr.Accordion("๐ก ์์ ํ๋กฌํํธ", open=False):
|
| 455 |
gr.Examples(
|
| 456 |
examples=[
|
| 457 |
-
["
|
| 458 |
-
["
|
| 459 |
-
["
|
| 460 |
-
["
|
| 461 |
-
["
|
|
|
|
|
|
|
| 462 |
],
|
| 463 |
inputs=prompt_input
|
| 464 |
)
|
|
@@ -467,7 +411,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 467 |
|
| 468 |
with gr.Column(scale=1):
|
| 469 |
gr.HTML('<div class="card">')
|
| 470 |
-
gr.HTML('<h3 style="margin-top: 0;">๐จ
|
| 471 |
|
| 472 |
output_image = gr.Image(
|
| 473 |
label="",
|
|
@@ -476,7 +420,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 476 |
)
|
| 477 |
|
| 478 |
use_image_button = gr.Button(
|
| 479 |
-
"โป๏ธ ์ด ์ด๋ฏธ์ง๋ฅผ ๋ค์
|
| 480 |
elem_classes="use-btn",
|
| 481 |
visible=False
|
| 482 |
)
|
|
@@ -487,8 +431,9 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 487 |
<h4 style="margin-top: 0; color: #0369a1;">๐ก ํ</h4>
|
| 488 |
<ul style="margin: 0; padding-left: 1.5rem; color: #0c4a6e;">
|
| 489 |
<li>๊ตฌ์ฒด์ ์ด๊ณ ์์ธํ ํ๋กฌํํธ๋ฅผ ์ฌ์ฉํ์ธ์</li>
|
| 490 |
-
<li
|
| 491 |
-
<li>๋ค์ค ์ด๋ฏธ์ง ๋ชจ๋๋ก ์ฌ๋ฌ ์ด๋ฏธ์ง๋ฅผ
|
|
|
|
| 492 |
</ul>
|
| 493 |
</div>
|
| 494 |
''')
|
|
@@ -500,7 +445,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 500 |
<div style="text-align: center; margin-top: 2rem; padding: 1rem;
|
| 501 |
border-top: 1px solid #e5e7eb;">
|
| 502 |
<p style="color: #6b7280;">
|
| 503 |
-
Made with ๐
|
| 504 |
</p>
|
| 505 |
</div>
|
| 506 |
''')
|
|
@@ -515,8 +460,8 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 515 |
active_tab: str,
|
| 516 |
oauth_token: Optional[gr.OAuthToken] = None,
|
| 517 |
):
|
| 518 |
-
if not
|
| 519 |
-
raise gr.Error("
|
| 520 |
if not prompt:
|
| 521 |
raise gr.Error("ํ๋กฌํํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.")
|
| 522 |
if active_tab == "multiple" and multi_images:
|
|
@@ -547,36 +492,32 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 547 |
):
|
| 548 |
if not profile:
|
| 549 |
return gr.update(visible=False), gr.update(visible=False)
|
| 550 |
-
if
|
| 551 |
return gr.update(visible=True), gr.update(visible=False)
|
| 552 |
else:
|
| 553 |
message = '''
|
| 554 |
-
<div class="
|
| 555 |
-
<h2
|
| 556 |
<p style="font-size: 1.1rem; margin: 1rem 0;">
|
| 557 |
-
์ด
|
| 558 |
</p>
|
| 559 |
<p style="margin: 1rem 0;">
|
| 560 |
-
|
| 561 |
</p>
|
| 562 |
<ul style="text-align: left; display: inline-block; margin: 1rem 0;">
|
| 563 |
-
<li>๐ Google
|
| 564 |
-
<li>โก ๋น ๋ฅธ ์ด๋ฏธ์ง
|
| 565 |
-
<li>๐จ
|
| 566 |
-
<li>๐ง ๋ค์ค ์ด๋ฏธ์ง
|
| 567 |
</ul>
|
| 568 |
-
<
|
| 569 |
-
|
| 570 |
-
|
| 571 |
-
color: white; text-decoration: none; border-radius: 0.5rem;
|
| 572 |
-
font-weight: bold; font-size: 1.1rem;">
|
| 573 |
-
๐ ์ง๊ธ PRO ๋ฉค๋ฒ ๋๊ธฐ
|
| 574 |
-
</a>
|
| 575 |
</div>
|
| 576 |
'''
|
| 577 |
return gr.update(visible=False), gr.update(visible=True, value=message)
|
| 578 |
|
| 579 |
-
demo.load(control_access, inputs=None, outputs=[main_interface,
|
| 580 |
|
| 581 |
if __name__ == "__main__":
|
| 582 |
demo.queue(max_size=None, default_concurrency_limit=None)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import replicate
|
|
|
|
| 3 |
import os
|
| 4 |
from typing import Optional, List
|
| 5 |
from huggingface_hub import whoami
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import requests
|
| 8 |
from io import BytesIO
|
| 9 |
import tempfile
|
| 10 |
|
| 11 |
+
# --- Replicate API Configuration ---
|
| 12 |
+
REPLICATE_API_TOKEN = os.getenv("REPLICATE_API_TOKEN")
|
|
|
|
|
|
|
| 13 |
|
| 14 |
+
if not REPLICATE_API_TOKEN:
|
| 15 |
+
raise ValueError("REPLICATE_API_TOKEN environment variable is not set.")
|
| 16 |
|
| 17 |
+
# Initialize Replicate client
|
| 18 |
+
os.environ["REPLICATE_API_TOKEN"] = REPLICATE_API_TOKEN
|
| 19 |
|
| 20 |
+
def verify_login_status(token: Optional[gr.OAuthToken]) -> bool:
|
| 21 |
+
"""Verifies if the user is logged in to Hugging Face."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
if not token:
|
| 23 |
return False
|
| 24 |
try:
|
| 25 |
user_info = whoami(token=token.token)
|
| 26 |
+
return True if user_info else False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
+
print(f"Could not verify user's login status: {e}")
|
| 29 |
return False
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
def run_single_image_logic(prompt: str, image_path: Optional[str] = None, progress=gr.Progress()) -> str:
|
| 32 |
+
"""Handles text-to-image or single image-to-image using Replicate's Nano Banana."""
|
| 33 |
try:
|
| 34 |
progress(0.2, desc="๐จ ์ค๋น ์ค...")
|
| 35 |
|
| 36 |
+
# Prepare input for Replicate API
|
| 37 |
+
input_data = {
|
| 38 |
+
"prompt": prompt
|
| 39 |
+
}
|
| 40 |
+
|
| 41 |
+
# If there's an input image, add it to the input
|
| 42 |
if image_path:
|
| 43 |
+
# Upload the local image file and get a URL
|
| 44 |
+
# For Replicate, we need to provide URLs, not local paths
|
| 45 |
+
# We'll read the file and create a temporary URL or use file directly
|
| 46 |
+
input_data["image_input"] = [image_path]
|
| 47 |
+
|
|
|
|
|
|
|
|
|
|
| 48 |
progress(0.5, desc="โจ ์์ฑ ์ค...")
|
| 49 |
|
| 50 |
+
# Run the model on Replicate
|
| 51 |
+
output = replicate.run(
|
| 52 |
+
"google/nano-banana",
|
| 53 |
+
input=input_data
|
| 54 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
progress(0.8, desc="๐ผ๏ธ ๋ง๋ฌด๋ฆฌ ์ค...")
|
| 57 |
|
| 58 |
+
# Handle the output
|
| 59 |
+
if output:
|
| 60 |
+
# If output is a FileOutput object, get the URL
|
| 61 |
+
if hasattr(output, 'url'):
|
| 62 |
+
image_url = output.url()
|
| 63 |
+
elif isinstance(output, str):
|
| 64 |
+
image_url = output
|
| 65 |
+
elif isinstance(output, list) and len(output) > 0:
|
| 66 |
+
image_url = output[0]
|
| 67 |
+
else:
|
| 68 |
+
raise ValueError("Unexpected output format from Replicate")
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# Download the image from URL
|
| 71 |
+
response = requests.get(image_url)
|
| 72 |
+
response.raise_for_status()
|
|
|
|
|
|
|
| 73 |
|
| 74 |
+
# Save to temporary file
|
| 75 |
+
img = Image.open(BytesIO(response.content))
|
| 76 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile:
|
| 77 |
img.save(tmpfile.name)
|
| 78 |
progress(1.0, desc="โ
์๋ฃ!")
|
| 79 |
return tmpfile.name
|
| 80 |
else:
|
| 81 |
+
raise ValueError("No output received from Replicate API")
|
| 82 |
|
| 83 |
except Exception as e:
|
| 84 |
print(f"Error details: {e}")
|
| 85 |
print(f"Error type: {type(e)}")
|
| 86 |
+
raise gr.Error(f"์ด๋ฏธ์ง ์์ฑ ์คํจ: {e}")
|
|
|
|
| 87 |
|
| 88 |
def run_multi_image_logic(prompt: str, images: List[str], progress=gr.Progress()) -> str:
|
| 89 |
"""
|
| 90 |
+
Handles multi-image editing by sending a list of images and a prompt.
|
| 91 |
"""
|
| 92 |
if not images:
|
| 93 |
raise gr.Error("'์ฌ๋ฌ ์ด๋ฏธ์ง' ํญ์์ ์ต์ ํ ๊ฐ์ ์ด๋ฏธ์ง๋ฅผ ์
๋ก๋ํด์ฃผ์ธ์.")
|
| 94 |
|
| 95 |
try:
|
| 96 |
progress(0.2, desc="๐จ ์ด๋ฏธ์ง ์ค๋น ์ค...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# Prepare input for Replicate API with multiple images
|
| 99 |
+
input_data = {
|
| 100 |
+
"prompt": prompt,
|
| 101 |
+
"image_input": images # Pass the list of image paths directly
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
progress(0.5, desc="โจ ์์ฑ ์ค...")
|
| 105 |
+
|
| 106 |
+
# Run the model on Replicate
|
| 107 |
+
output = replicate.run(
|
| 108 |
+
"google/nano-banana",
|
| 109 |
+
input=input_data
|
| 110 |
)
|
| 111 |
|
| 112 |
progress(0.8, desc="๐ผ๏ธ ๋ง๋ฌด๋ฆฌ ์ค...")
|
| 113 |
|
| 114 |
+
# Handle the output
|
| 115 |
+
if output:
|
| 116 |
+
# If output is a FileOutput object, get the URL
|
| 117 |
+
if hasattr(output, 'url'):
|
| 118 |
+
image_url = output.url()
|
| 119 |
+
elif isinstance(output, str):
|
| 120 |
+
image_url = output
|
| 121 |
+
elif isinstance(output, list) and len(output) > 0:
|
| 122 |
+
image_url = output[0]
|
| 123 |
+
else:
|
| 124 |
+
raise ValueError("Unexpected output format from Replicate")
|
| 125 |
|
| 126 |
+
# Download the image from URL
|
| 127 |
+
response = requests.get(image_url)
|
| 128 |
+
response.raise_for_status()
|
|
|
|
| 129 |
|
| 130 |
+
# Save to temporary file
|
| 131 |
+
img = Image.open(BytesIO(response.content))
|
| 132 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".png") as tmpfile:
|
| 133 |
img.save(tmpfile.name)
|
| 134 |
progress(1.0, desc="โ
์๋ฃ!")
|
| 135 |
return tmpfile.name
|
| 136 |
else:
|
| 137 |
+
raise ValueError("No output received from Replicate API")
|
| 138 |
|
| 139 |
except Exception as e:
|
| 140 |
print(f"Multi-image error details: {e}")
|
| 141 |
+
raise gr.Error(f"์ด๋ฏธ์ง ์์ฑ ์คํจ: {e}")
|
|
|
|
| 142 |
|
| 143 |
# --- Gradio App UI ---
|
| 144 |
css = '''
|
|
|
|
| 277 |
background: #1f2937;
|
| 278 |
}
|
| 279 |
|
| 280 |
+
/* Login Message Styling */
|
| 281 |
+
.login-message {
|
| 282 |
background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
|
| 283 |
border-radius: 1rem;
|
| 284 |
padding: 2rem;
|
|
|
|
| 286 |
border: 2px solid #f59e0b;
|
| 287 |
}
|
| 288 |
|
| 289 |
+
.dark .login-message {
|
| 290 |
background: linear-gradient(135deg, #7c2d12 0%, #92400e 100%);
|
| 291 |
border-color: #f59e0b;
|
| 292 |
}
|
|
|
|
| 322 |
๐ Real Nano Banana
|
| 323 |
</h1>
|
| 324 |
<p class="header-subtitle">
|
| 325 |
+
Google Nano Banana๋ก ๊ตฌ๋๋๋ AI ์ด๋ฏธ์ง ์์ฑ๊ธฐ
|
| 326 |
</p>
|
| 327 |
</div>
|
| 328 |
''')
|
| 329 |
|
| 330 |
+
# Login Notice
|
| 331 |
gr.HTML('''
|
| 332 |
+
<div style="background: linear-gradient(135deg, #e0f2fe 0%, #bae6fd 100%);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 333 |
border-radius: 0.5rem; padding: 1rem; margin-bottom: 1.5rem;
|
| 334 |
+
border-left: 4px solid #0284c7;">
|
| 335 |
+
<p style="margin: 0; color: #075985; font-weight: 600;">
|
| 336 |
+
๐ ์ด ์๋น์ค๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด Hugging Face ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํด์ฃผ์ธ์.
|
|
|
|
|
|
|
|
|
|
|
|
|
| 337 |
</p>
|
| 338 |
</div>
|
| 339 |
''')
|
| 340 |
|
| 341 |
+
login_message = gr.Markdown(visible=False)
|
| 342 |
main_interface = gr.Column(visible=False, elem_classes="main-container")
|
| 343 |
|
| 344 |
with main_interface:
|
|
|
|
| 354 |
with gr.TabItem("๐ผ๏ธ ๋จ์ผ ์ด๋ฏธ์ง", id="single") as single_tab:
|
| 355 |
image_input = gr.Image(
|
| 356 |
type="filepath",
|
| 357 |
+
label="์
๋ ฅ ์ด๋ฏธ์ง (์ ํ์ฌํญ)",
|
| 358 |
elem_classes="image-input"
|
| 359 |
)
|
| 360 |
gr.HTML('''
|
| 361 |
<p style="text-align: center; color: #6b7280; font-size: 0.9rem; margin-top: 0.5rem;">
|
| 362 |
+
๐ก ํ
์คํธโ์ด๋ฏธ์ง ์์ฑ์ ๋น์๋์ธ์
|
| 363 |
</p>
|
| 364 |
''')
|
| 365 |
|
|
|
|
| 379 |
gr.HTML('<h3>โ๏ธ ํ๋กฌํํธ</h3>')
|
| 380 |
prompt_input = gr.Textbox(
|
| 381 |
label="",
|
| 382 |
+
info="AI์๊ฒ ์ํ๋ ์ด๋ฏธ์ง๋ฅผ ์ค๋ช
ํ์ธ์",
|
| 383 |
+
placeholder="์: ๋ง์์ด ๋ณด์ด๋ ํผ์, ์ฐ์ฃผ๋ฅผ ๋ฐฐ๊ฒฝ์ผ๋ก ํ ๊ณ ์์ด, ๋ฏธ๋์ ์ธ ๋์ ํ๊ฒฝ...",
|
| 384 |
lines=3,
|
| 385 |
elem_classes="prompt-input"
|
| 386 |
)
|
| 387 |
|
| 388 |
# Generate Button
|
| 389 |
generate_button = gr.Button(
|
| 390 |
+
"๐ ์์ฑํ๊ธฐ",
|
| 391 |
variant="primary",
|
| 392 |
elem_classes="generate-btn"
|
| 393 |
)
|
|
|
|
| 396 |
with gr.Accordion("๐ก ์์ ํ๋กฌํํธ", open=False):
|
| 397 |
gr.Examples(
|
| 398 |
examples=[
|
| 399 |
+
["A delicious looking pizza with melting cheese"],
|
| 400 |
+
["A cat in a spacesuit walking on the moon surface"],
|
| 401 |
+
["Cyberpunk city at night with neon lights"],
|
| 402 |
+
["Japanese garden with cherry blossoms in spring"],
|
| 403 |
+
["Fantasy wizard tower in a magical world"],
|
| 404 |
+
["Make the scene more dramatic and cinematic"],
|
| 405 |
+
["Transform this into a watercolor painting style"],
|
| 406 |
],
|
| 407 |
inputs=prompt_input
|
| 408 |
)
|
|
|
|
| 411 |
|
| 412 |
with gr.Column(scale=1):
|
| 413 |
gr.HTML('<div class="card">')
|
| 414 |
+
gr.HTML('<h3 style="margin-top: 0;">๐จ ์์ฑ ๊ฒฐ๊ณผ</h3>')
|
| 415 |
|
| 416 |
output_image = gr.Image(
|
| 417 |
label="",
|
|
|
|
| 420 |
)
|
| 421 |
|
| 422 |
use_image_button = gr.Button(
|
| 423 |
+
"โป๏ธ ์ด ์ด๋ฏธ์ง๋ฅผ ๋ค์ ํธ์ง์ ์ฌ์ฉ",
|
| 424 |
elem_classes="use-btn",
|
| 425 |
visible=False
|
| 426 |
)
|
|
|
|
| 431 |
<h4 style="margin-top: 0; color: #0369a1;">๐ก ํ</h4>
|
| 432 |
<ul style="margin: 0; padding-left: 1.5rem; color: #0c4a6e;">
|
| 433 |
<li>๊ตฌ์ฒด์ ์ด๊ณ ์์ธํ ํ๋กฌํํธ๋ฅผ ์ฌ์ฉํ์ธ์</li>
|
| 434 |
+
<li>์์ฑ๋ ์ด๋ฏธ์ง๋ฅผ ์ฌ์ฌ์ฉํ์ฌ ๋ฐ๋ณต์ ์ผ๋ก ๊ฐ์ ํ ์ ์์ต๋๋ค</li>
|
| 435 |
+
<li>๋ค์ค ์ด๋ฏธ์ง ๋ชจ๋๋ก ์ฌ๋ฌ ์ฐธ์กฐ ์ด๋ฏธ์ง๋ฅผ ๊ฒฐํฉํ ์ ์์ต๋๋ค</li>
|
| 436 |
+
<li>์์ด ํ๋กฌํํธ๊ฐ ๋ ์ข์ ๊ฒฐ๊ณผ๋ฅผ ์์ฑํฉ๋๋ค</li>
|
| 437 |
</ul>
|
| 438 |
</div>
|
| 439 |
''')
|
|
|
|
| 445 |
<div style="text-align: center; margin-top: 2rem; padding: 1rem;
|
| 446 |
border-top: 1px solid #e5e7eb;">
|
| 447 |
<p style="color: #6b7280;">
|
| 448 |
+
Made with ๐ using Replicate API | Powered by Google Nano Banana
|
| 449 |
</p>
|
| 450 |
</div>
|
| 451 |
''')
|
|
|
|
| 460 |
active_tab: str,
|
| 461 |
oauth_token: Optional[gr.OAuthToken] = None,
|
| 462 |
):
|
| 463 |
+
if not verify_login_status(oauth_token):
|
| 464 |
+
raise gr.Error("๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค. ์๋จ์ 'Sign in with Hugging Face' ๋ฒํผ์ ํด๋ฆญํด์ฃผ์ธ์.")
|
| 465 |
if not prompt:
|
| 466 |
raise gr.Error("ํ๋กฌํํธ๋ฅผ ์
๋ ฅํด์ฃผ์ธ์.")
|
| 467 |
if active_tab == "multiple" and multi_images:
|
|
|
|
| 492 |
):
|
| 493 |
if not profile:
|
| 494 |
return gr.update(visible=False), gr.update(visible=False)
|
| 495 |
+
if verify_login_status(oauth_token):
|
| 496 |
return gr.update(visible=True), gr.update(visible=False)
|
| 497 |
else:
|
| 498 |
message = '''
|
| 499 |
+
<div class="login-message">
|
| 500 |
+
<h2>๐ ๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค</h2>
|
| 501 |
<p style="font-size: 1.1rem; margin: 1rem 0;">
|
| 502 |
+
์ด AI ์ด๋ฏธ์ง ์์ฑ ๋๊ตฌ๋ฅผ ์ฌ์ฉํ๋ ค๋ฉด Hugging Face ๊ณ์ ์ผ๋ก ๋ก๊ทธ์ธํด์ฃผ์ธ์.
|
| 503 |
</p>
|
| 504 |
<p style="margin: 1rem 0;">
|
| 505 |
+
๋ก๊ทธ์ธํ๋ฉด ๋ค์ ๊ธฐ๋ฅ์ ์ฌ์ฉ๏ฟฝ๏ฟฝ๏ฟฝ ์ ์์ต๋๋ค:
|
| 506 |
</p>
|
| 507 |
<ul style="text-align: left; display: inline-block; margin: 1rem 0;">
|
| 508 |
+
<li>๐ Google Nano Banana๋ฅผ ํตํ ๊ณ ํ์ง ์ด๋ฏธ์ง ์์ฑ</li>
|
| 509 |
+
<li>โก ๋น ๋ฅธ ์ด๋ฏธ์ง ์์ฑ ๋ฐ ํธ์ง</li>
|
| 510 |
+
<li>๐จ ํ
์คํธ๋ฅผ ์ด๋ฏธ์ง๋ก ๋ณํ</li>
|
| 511 |
+
<li>๐ง ๋ค์ค ์ด๋ฏธ์ง ํธ์ง ๋ฐ ๊ฒฐํฉ</li>
|
| 512 |
</ul>
|
| 513 |
+
<p style="margin-top: 1.5rem; font-weight: bold;">
|
| 514 |
+
์๋จ์ "Sign in with Hugging Face" ๋ฒํผ์ ํด๋ฆญํ์ฌ ์์ํ์ธ์!
|
| 515 |
+
</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
| 516 |
</div>
|
| 517 |
'''
|
| 518 |
return gr.update(visible=False), gr.update(visible=True, value=message)
|
| 519 |
|
| 520 |
+
demo.load(control_access, inputs=None, outputs=[main_interface, login_message])
|
| 521 |
|
| 522 |
if __name__ == "__main__":
|
| 523 |
demo.queue(max_size=None, default_concurrency_limit=None)
|