Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,82 +1,32 @@
|
|
| 1 |
-
import
|
| 2 |
-
from gradio_client import Client
|
| 3 |
import os
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
import warnings
|
| 6 |
|
| 7 |
# ๊ฒฝ๊ณ ๋ฉ์์ง ์จ๊ธฐ๊ธฐ
|
| 8 |
warnings.filterwarnings('ignore', category=UserWarning)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
|
|
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
try:
|
| 17 |
-
# API ํธ์ถ์ ํตํด ์ด๋ฏธ์ง ์์ฑ
|
| 18 |
-
result = gr.Image.update(value=None) # ์ด๊ธฐ ์ํ๋ ๋น ์ด๋ฏธ์ง
|
| 19 |
-
|
| 20 |
-
# Hugging Face API ํธ์ถ
|
| 21 |
-
client = Client(
|
| 22 |
-
"https://black-forest-labs-flux-1-schnell.hf.space", # API ์๋ํฌ์ธํธ ์ง์ ์ง์
|
| 23 |
-
hf_token=HF_TOKEN,
|
| 24 |
-
)
|
| 25 |
-
|
| 26 |
-
# ์ด๋ฏธ์ง ์์ฑ
|
| 27 |
-
result = client.predict(
|
| 28 |
-
prompt,
|
| 29 |
-
1872187377, # seed
|
| 30 |
-
False, # randomize_seed
|
| 31 |
-
width,
|
| 32 |
-
height,
|
| 33 |
-
4, # num_inference_steps
|
| 34 |
-
api_name="/infer"
|
| 35 |
-
)
|
| 36 |
-
return result
|
| 37 |
-
except Exception as e:
|
| 38 |
-
raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
KNOWLEDGE
|
| 45 |
-
โโโ ACQUISITION [Brain with Lightning ~60px]
|
| 46 |
-
โ โโโ READING [Open Book with Glow]
|
| 47 |
-
โ โโโ PRACTICE [Hands-on Tools]
|
| 48 |
-
โโโ APPLICATION
|
| 49 |
-
โโโ CREATION [Artist Palette]
|
| 50 |
-
โโโ INNOVATION [Lightbulb]""",
|
| 51 |
-
1024,
|
| 52 |
-
1024
|
| 53 |
-
],
|
| 54 |
-
[
|
| 55 |
-
"""A handrawn colorful mind map diagram, tech-focused style, neon accents.
|
| 56 |
-
DIGITAL TRANSFORM
|
| 57 |
-
โโโ CLOUD [Cloud with Data ~55px]
|
| 58 |
-
โ โโโ STORAGE [Database]
|
| 59 |
-
โ โโโ COMPUTING [Server]
|
| 60 |
-
โโโ SECURITY
|
| 61 |
-
โโโ ENCRYPTION [Lock]
|
| 62 |
-
โโโ MONITORING [Radar]""",
|
| 63 |
-
1024,
|
| 64 |
-
1024
|
| 65 |
-
],
|
| 66 |
-
[
|
| 67 |
-
"""A handrawn colorful mind map diagram, creative style, flowing design.
|
| 68 |
-
INNOVATION
|
| 69 |
-
โโโ IDEATION [Lightbulb ~60px]
|
| 70 |
-
โ โโโ RESEARCH [Magnifier]
|
| 71 |
-
โ โโโ BRAINSTORM [Brain]
|
| 72 |
-
โโโ EXECUTION
|
| 73 |
-
โโโ PROTOTYPE [Tools]
|
| 74 |
-
โโโ TEST [Checklist]""",
|
| 75 |
-
1024,
|
| 76 |
-
1024
|
| 77 |
-
]
|
| 78 |
-
]
|
| 79 |
|
|
|
|
|
|
|
| 80 |
|
| 81 |
# Enhanced examples with more detailed prompts and specific styling
|
| 82 |
EXAMPLES = [
|
|
@@ -273,15 +223,41 @@ EXAMPLES = [
|
|
| 273 |
}
|
| 274 |
]
|
| 275 |
|
| 276 |
-
|
| 277 |
-
|
| 278 |
# Convert examples to Gradio format
|
| 279 |
GRADIO_EXAMPLES = [
|
| 280 |
[example["prompt"], example["width"], example["height"]]
|
| 281 |
for example in EXAMPLES
|
| 282 |
]
|
| 283 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 284 |
|
|
|
|
| 285 |
demo = gr.Interface(
|
| 286 |
fn=generate_diagram,
|
| 287 |
inputs=[
|
|
@@ -293,14 +269,14 @@ demo = gr.Interface(
|
|
| 293 |
gr.Slider(
|
| 294 |
label="๋๋น",
|
| 295 |
minimum=512,
|
| 296 |
-
maximum=
|
| 297 |
step=128,
|
| 298 |
value=1024
|
| 299 |
),
|
| 300 |
gr.Slider(
|
| 301 |
label="๋์ด",
|
| 302 |
minimum=512,
|
| 303 |
-
maximum=
|
| 304 |
step=128,
|
| 305 |
value=1024
|
| 306 |
)
|
|
@@ -319,9 +295,9 @@ demo = gr.Interface(
|
|
| 319 |
cache_examples=True
|
| 320 |
)
|
| 321 |
|
| 322 |
-
# ์ฑ ์คํ
|
| 323 |
if __name__ == "__main__":
|
| 324 |
-
demo.queue()
|
| 325 |
demo.launch(
|
| 326 |
server_name="0.0.0.0",
|
| 327 |
server_port=7860,
|
|
|
|
| 1 |
+
import random
|
|
|
|
| 2 |
import os
|
| 3 |
+
import uuid
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
+
from diffusers import DiffusionPipeline
|
| 9 |
+
from PIL import Image
|
| 10 |
import warnings
|
| 11 |
|
| 12 |
# ๊ฒฝ๊ณ ๋ฉ์์ง ์จ๊ธฐ๊ธฐ
|
| 13 |
warnings.filterwarnings('ignore', category=UserWarning)
|
| 14 |
|
| 15 |
+
# ์ ์ฅ ๋๋ ํ ๋ฆฌ ์์ฑ
|
| 16 |
+
SAVE_DIR = "saved_images"
|
| 17 |
+
if not os.path.exists(SAVE_DIR):
|
| 18 |
+
os.makedirs(SAVE_DIR, exist_ok=True)
|
| 19 |
|
| 20 |
+
# ์ฅ์น ์ค์
|
| 21 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# ๋ชจ๋ธ ๋ก๋
|
| 24 |
+
repo_id = "black-forest-labs/FLUX.1-schnell"
|
| 25 |
+
pipeline = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float32)
|
| 26 |
+
pipeline = pipeline.to(device)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
MAX_SEED = np.iinfo(np.int32).max
|
| 29 |
+
MAX_IMAGE_SIZE = 2048
|
| 30 |
|
| 31 |
# Enhanced examples with more detailed prompts and specific styling
|
| 32 |
EXAMPLES = [
|
|
|
|
| 223 |
}
|
| 224 |
]
|
| 225 |
|
|
|
|
|
|
|
| 226 |
# Convert examples to Gradio format
|
| 227 |
GRADIO_EXAMPLES = [
|
| 228 |
[example["prompt"], example["width"], example["height"]]
|
| 229 |
for example in EXAMPLES
|
| 230 |
]
|
| 231 |
|
| 232 |
+
def generate_diagram(prompt, width=1024, height=1024):
|
| 233 |
+
"""FLUX AI๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ด์ด๊ทธ๋จ ์์ฑ"""
|
| 234 |
+
try:
|
| 235 |
+
# ์๋ ์ค์
|
| 236 |
+
seed = random.randint(0, MAX_SEED)
|
| 237 |
+
generator = torch.Generator(device=device).manual_seed(seed)
|
| 238 |
+
|
| 239 |
+
# ์ด๋ฏธ์ง ์์ฑ
|
| 240 |
+
image = pipeline(
|
| 241 |
+
prompt=prompt,
|
| 242 |
+
width=width,
|
| 243 |
+
height=height,
|
| 244 |
+
num_inference_steps=4,
|
| 245 |
+
generator=generator,
|
| 246 |
+
).images[0]
|
| 247 |
+
|
| 248 |
+
# ์ด๋ฏธ์ง ์ ์ฅ
|
| 249 |
+
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 250 |
+
unique_id = str(uuid.uuid4())[:8]
|
| 251 |
+
filename = f"diagram_{timestamp}_{unique_id}.png"
|
| 252 |
+
save_path = os.path.join(SAVE_DIR, filename)
|
| 253 |
+
image.save(save_path)
|
| 254 |
+
|
| 255 |
+
return image
|
| 256 |
+
|
| 257 |
+
except Exception as e:
|
| 258 |
+
raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}")
|
| 259 |
|
| 260 |
+
# Gradio ์ธํฐํ์ด์ค ์์ฑ
|
| 261 |
demo = gr.Interface(
|
| 262 |
fn=generate_diagram,
|
| 263 |
inputs=[
|
|
|
|
| 269 |
gr.Slider(
|
| 270 |
label="๋๋น",
|
| 271 |
minimum=512,
|
| 272 |
+
maximum=MAX_IMAGE_SIZE,
|
| 273 |
step=128,
|
| 274 |
value=1024
|
| 275 |
),
|
| 276 |
gr.Slider(
|
| 277 |
label="๋์ด",
|
| 278 |
minimum=512,
|
| 279 |
+
maximum=MAX_IMAGE_SIZE,
|
| 280 |
step=128,
|
| 281 |
value=1024
|
| 282 |
)
|
|
|
|
| 295 |
cache_examples=True
|
| 296 |
)
|
| 297 |
|
| 298 |
+
# ์ฑ ์คํ
|
| 299 |
if __name__ == "__main__":
|
| 300 |
+
demo.queue()
|
| 301 |
demo.launch(
|
| 302 |
server_name="0.0.0.0",
|
| 303 |
server_port=7860,
|