|
|
import gradio as gr |
|
|
import numpy as np |
|
|
import random |
|
|
import torch |
|
|
from diffusers import DiffusionPipeline |
|
|
import warnings |
|
|
import os |
|
|
from datetime import datetime |
|
|
import uuid |
|
|
|
|
|
|
|
|
warnings.filterwarnings('ignore', category=UserWarning) |
|
|
|
|
|
|
|
|
SAVE_DIR = "saved_images" |
|
|
if not os.path.exists(SAVE_DIR): |
|
|
os.makedirs(SAVE_DIR, exist_ok=True) |
|
|
|
|
|
|
|
|
dtype = torch.float32 if torch.cuda.is_available() else torch.float32 |
|
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
|
|
|
pipe = DiffusionPipeline.from_pretrained( |
|
|
"black-forest-labs/FLUX.1-schnell", |
|
|
torch_dtype=dtype, |
|
|
device_map="balanced" if torch.cuda.is_available() else None, |
|
|
use_safetensors=True |
|
|
).to(device) |
|
|
|
|
|
|
|
|
pipe.enable_attention_slicing() |
|
|
if device == "cpu": |
|
|
pipe.enable_sequential_cpu_offload() |
|
|
|
|
|
MAX_SEED = np.iinfo(np.int32).max |
|
|
MAX_IMAGE_SIZE = 2048 |
|
|
|
|
|
def generate_diagram(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4): |
|
|
"""FLUX AI๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ด์ด๊ทธ๋จ ์์ฑ""" |
|
|
try: |
|
|
if randomize_seed: |
|
|
seed = random.randint(0, MAX_SEED) |
|
|
generator = torch.Generator(device=device).manual_seed(seed) |
|
|
|
|
|
with torch.no_grad(): |
|
|
image = pipe( |
|
|
prompt=prompt, |
|
|
width=width, |
|
|
height=height, |
|
|
num_inference_steps=num_inference_steps, |
|
|
generator=generator, |
|
|
guidance_scale=0.0 |
|
|
).images[0] |
|
|
|
|
|
|
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
|
|
unique_id = str(uuid.uuid4())[:8] |
|
|
filename = f"diagram_{timestamp}_{unique_id}.png" |
|
|
save_path = os.path.join(SAVE_DIR, filename) |
|
|
image.save(save_path) |
|
|
|
|
|
return image, seed |
|
|
|
|
|
except Exception as e: |
|
|
raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}") |
|
|
finally: |
|
|
if torch.cuda.is_available(): |
|
|
torch.cuda.empty_cache() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
EXAMPLES = [ |
|
|
{ |
|
|
"title": "Knowledge Tree", |
|
|
"prompt": """A handrawn colorful mind map diagram, educational style, vibrant colors, clear hierarchy, golden ratio layout. |
|
|
KNOWLEDGE |
|
|
โโโ ACQUISITION [Brain with Lightning ~60px] |
|
|
โ โโโ READING [Open Book with Glow] |
|
|
โ โโโ PRACTICE [Hands-on Tools] |
|
|
โ โโโ OBSERVATION [Eye with Magnifier] |
|
|
โโโ PROCESSING [Gear Network ~50px] |
|
|
โ โโโ ANALYSIS [Graph Trending Up] |
|
|
โ โโโ SYNTHESIS [Puzzle Pieces] |
|
|
โโโ RETENTION [Memory Chip ~45px] |
|
|
โ โโโ SHORT-TERM [Quick Flash] |
|
|
โ โโโ LONG-TERM [Solid Archive] |
|
|
โโโ APPLICATION |
|
|
โโโ CREATION [Artist Palette] |
|
|
โโโ INNOVATION [Lightbulb Constellation]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
}, |
|
|
{ |
|
|
"title": "Digital Transformation", |
|
|
"prompt": """A handrawn colorful mind map diagram, tech-focused style, neon accents, circuit board patterns. |
|
|
DIGITAL TRANSFORM |
|
|
โโโ CLOUD [Cloud with Data Rain ~55px] |
|
|
โ โโโ STORAGE [Database Cluster] |
|
|
โ โโโ COMPUTING [Server Array] |
|
|
โโโ AUTOMATION [Robot Arm ~50px] |
|
|
โ โโโ WORKFLOWS [Flowchart] |
|
|
โ โโโ AI/ML [Neural Network] |
|
|
โโโ SECURITY [Shield Matrix ~45px] |
|
|
โ โโโ ENCRYPTION [Lock Code] |
|
|
โ โโโ MONITORING [Radar Screen] |
|
|
โโโ INTEGRATION |
|
|
โโโ APIS [Puzzle Connect] |
|
|
โโโ MICROSERVICES [Building Blocks]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
}, |
|
|
{ |
|
|
"title": "Creative Process", |
|
|
"prompt": """A handrawn colorful mind map diagram, artistic style, watercolor effects, flowing connections. |
|
|
CREATIVITY |
|
|
โโโ INSPIRATION [Constellation Stars ~60px] |
|
|
โ โโโ NATURE [Organic Patterns] |
|
|
โ โโโ CULTURE [Global Icons] |
|
|
โโโ IDEATION [Floating Bubbles ~50px] |
|
|
โ โโโ BRAINSTORM [Thunder Cloud] |
|
|
โ โโโ REFINEMENT [Diamond Polish] |
|
|
โโโ EXECUTION [Artist Tools ~45px] |
|
|
โ โโโ TECHNIQUE [Skilled Hands] |
|
|
โ โโโ MEDIUM [Palette Mix] |
|
|
โโโ PRESENTATION |
|
|
โโโ GALLERY [Frame Display] |
|
|
โโโ FEEDBACK [Echo Ripples]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
}, |
|
|
{ |
|
|
"title": "Future Cities", |
|
|
"prompt": """A handrawn colorful mind map diagram, futuristic style, holographic elements, sustainable themes. |
|
|
SMART CITY |
|
|
โโโ MOBILITY [Hover Transport ~60px] |
|
|
โ โโโ AUTONOMOUS [Self-Driving] |
|
|
โ โโโ CONNECTED [Network Grid] |
|
|
โโโ ENERGY [Solar Crystal ~55px] |
|
|
โ โโโ RENEWABLE [Green Power] |
|
|
โ โโโ STORAGE [Battery Hub] |
|
|
โโโ LIVING [Eco Building ~50px] |
|
|
โ โโโ VERTICAL [Sky Gardens] |
|
|
โ โโโ COMMUNITY [People Connect] |
|
|
โโโ INFRASTRUCTURE |
|
|
โโโ AI GRID [Neural City] |
|
|
โโโ ECO SYSTEM [Nature Tech]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
}, |
|
|
{ |
|
|
"title": "Health Evolution", |
|
|
"prompt": """A handrawn colorful mind map diagram, medical style, DNA helix patterns, wellness focus. |
|
|
HEALTH 3.0 |
|
|
โโโ PREVENTION [Shield DNA ~60px] |
|
|
โ โโโ LIFESTYLE [Activity Pulse] |
|
|
โ โโโ MONITORING [Health Watch] |
|
|
โโโ TREATMENT [Caduceus Tech ~55px] |
|
|
โ โโโ PERSONALIZED [DNA Code] |
|
|
โ โโโ REGENERATIVE [Cell Renew] |
|
|
โโโ ENHANCEMENT [Upgrade Spiral ~50px] |
|
|
โ โโโ COGNITIVE [Brain Boost] |
|
|
โ โโโ PHYSICAL [Body Optimize] |
|
|
โโโ INTEGRATION |
|
|
โโโ AI HEALTH [Smart Doctor] |
|
|
โโโ COMMUNITY [Global Care]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
}, |
|
|
{ |
|
|
"title": "Space Exploration", |
|
|
"prompt": """A handrawn colorful mind map diagram, cosmic style, star field background, planetary elements. |
|
|
SPACE FRONTIER |
|
|
โโโ DISCOVERY [Telescope Array ~60px] |
|
|
โ โโโ MAPPING [Star Charts] |
|
|
โ โโโ ANALYSIS [Data Stream] |
|
|
โโโ TRAVEL [Rocket Launch ~55px] |
|
|
โ โโโ PROPULSION [Energy Core] |
|
|
โ โโโ NAVIGATION [Space Map] |
|
|
โโโ COLONIZATION [Dome City ~50px] |
|
|
โ โโโ HABITATS [Life Sphere] |
|
|
โ โโโ RESOURCES [Mine Extract] |
|
|
โโโ RESEARCH |
|
|
โโโ ASTROBIOLOGY [Life Search] |
|
|
โโโ PHYSICS [Space Time]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
}, |
|
|
{ |
|
|
"title": "Ocean Innovation", |
|
|
"prompt": """A handrawn colorful mind map diagram, marine style, wave patterns, aqua themes. |
|
|
OCEAN TECH |
|
|
โโโ EXPLORATION [Deep Submersible ~60px] |
|
|
โ โโโ MAPPING [Sonar Wave] |
|
|
โ โโโ RESEARCH [Lab Bubble] |
|
|
โโโ CONSERVATION [Marine Life ~55px] |
|
|
โ โโโ PROTECTION [Reef Shield] |
|
|
โ โโโ RESTORATION [Growth Core] |
|
|
โโโ HARVESTING [Sustainable Net ~50px] |
|
|
โ โโโ ENERGY [Wave Power] |
|
|
โ โโโ RESOURCES [Bio Extract] |
|
|
โโโ MONITORING |
|
|
โโโ AI SYSTEMS [Smart Sensors] |
|
|
โโโ ECOLOGY [Life Web]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
}, |
|
|
{ |
|
|
"title": "Quantum Computing", |
|
|
"prompt": """A handrawn colorful mind map diagram, quantum style, wave-particle duality, matrix patterns. |
|
|
QUANTUM TECH |
|
|
โโโ COMPUTATION [Qubit Matrix ~60px] |
|
|
โ โโโ PROCESSING [Wave Function] |
|
|
โ โโโ ALGORITHMS [Code Quantum] |
|
|
โโโ APPLICATIONS [Use Cases ~55px] |
|
|
โ โโโ SIMULATION [Model World] |
|
|
โ โโโ OPTIMIZATION [Peak Find] |
|
|
โโโ INFRASTRUCTURE [Q-Hardware ~50px] |
|
|
โ โโโ CONTROL [Pulse Shape] |
|
|
โ โโโ COOLING [Zero Point] |
|
|
โโโ DEVELOPMENT |
|
|
โโโ SOFTWARE [Q-Code Web] |
|
|
โโโ INTEGRATION [Classical Bridge]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
}, |
|
|
{ |
|
|
"title": "Bio Engineering", |
|
|
"prompt": """A handrawn colorful mind map diagram, biological style, DNA patterns, organic flow. |
|
|
BIOTECH |
|
|
โโโ GENETICS [DNA Helix ~60px] |
|
|
โ โโโ EDITING [CRISPR Tool] |
|
|
โ โโโ SYNTHESIS [Gene Build] |
|
|
โโโ APPLICATIONS [Lab Array ~55px] |
|
|
โ โโโ MEDICINE [Heal Cell] |
|
|
โ โโโ AGRICULTURE [Grow Plus] |
|
|
โโโ PLATFORMS [Bio Factory ~50px] |
|
|
โ โโโ SENSORS [Live Detect] |
|
|
โ โโโ PROCESSORS [Cell Compute] |
|
|
โโโ INTEGRATION |
|
|
โโโ AI BIOLOGY [Smart Life] |
|
|
โโโ ECOSYSTEM [Nature Net]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
}, |
|
|
{ |
|
|
"title": "AI Evolution", |
|
|
"prompt": """A handrawn colorful mind map diagram, neural network style, digital patterns, intelligence flow. |
|
|
AI FUTURE |
|
|
โโโ COGNITION [Brain Network ~60px] |
|
|
โ โโโ LEARNING [Growth Path] |
|
|
โ โโโ REASONING [Logic Tree] |
|
|
โโโ PERCEPTION [Sensor Array ~55px] |
|
|
โ โโโ VISION [Eye Matrix] |
|
|
โ โโโ LANGUAGE [Word Web] |
|
|
โโโ INTERACTION [Connect Hub ~50px] |
|
|
โ โโโ HUMAN [Bridge Link] |
|
|
โ โโโ MACHINE [Code Path] |
|
|
โโโ EVOLUTION |
|
|
โโโ CONSCIOUSNESS [Mind Spark] |
|
|
โโโ CREATIVITY [Art Core]""", |
|
|
"width": 1024, |
|
|
"height": 1024 |
|
|
} |
|
|
] |
|
|
|
|
|
|
|
|
GRADIO_EXAMPLES = [ |
|
|
[example["prompt"], example["width"], example["height"]] |
|
|
for example in EXAMPLES |
|
|
] |
|
|
|
|
|
def generate_diagram(prompt, width=1024, height=1024): |
|
|
"""FLUX AI๋ฅผ ์ฌ์ฉํ์ฌ ๋ค์ด์ด๊ทธ๋จ ์์ฑ""" |
|
|
try: |
|
|
|
|
|
seed = random.randint(0, MAX_SEED) |
|
|
generator = torch.Generator(device=device).manual_seed(seed) |
|
|
|
|
|
|
|
|
image = pipeline( |
|
|
prompt=prompt, |
|
|
width=width, |
|
|
height=height, |
|
|
num_inference_steps=4, |
|
|
generator=generator, |
|
|
).images[0] |
|
|
|
|
|
|
|
|
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S") |
|
|
unique_id = str(uuid.uuid4())[:8] |
|
|
filename = f"diagram_{timestamp}_{unique_id}.png" |
|
|
save_path = os.path.join(SAVE_DIR, filename) |
|
|
image.save(save_path) |
|
|
|
|
|
return image |
|
|
|
|
|
except Exception as e: |
|
|
raise gr.Error(f"๋ค์ด์ด๊ทธ๋จ ์์ฑ ์ค ์ค๋ฅ ๋ฐ์: {str(e)}") |
|
|
|
|
|
|
|
|
css=""" |
|
|
#col-container { |
|
|
margin: 0 auto; |
|
|
max-width: 520px; |
|
|
} |
|
|
""" |
|
|
|
|
|
|
|
|
with gr.Blocks(css=css) as demo: |
|
|
with gr.Column(elem_id="col-container"): |
|
|
gr.Markdown("""# FLUX ๋ค์ด์ด๊ทธ๋จ ์์ฑ๊ธฐ |
|
|
FLUX AI๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฆ๋ค์ด ์๊ทธ๋ฆผ ์คํ์ผ์ ๋ค์ด์ด๊ทธ๋จ์ ์์ฑํฉ๋๋ค |
|
|
""") |
|
|
|
|
|
with gr.Row(): |
|
|
prompt = gr.Text( |
|
|
label="ํ๋กฌํํธ", |
|
|
show_label=False, |
|
|
max_lines=1, |
|
|
placeholder="๋ค์ด์ด๊ทธ๋จ ๊ตฌ์กฐ๋ฅผ ์
๋ ฅํ์ธ์...", |
|
|
container=False, |
|
|
) |
|
|
run_button = gr.Button("์์ฑ", scale=0) |
|
|
|
|
|
result = gr.Image(label="์์ฑ๋ ๋ค์ด์ด๊ทธ๋จ", show_label=False) |
|
|
|
|
|
with gr.Accordion("๊ณ ๊ธ ์ค์ ", open=False): |
|
|
seed = gr.Slider( |
|
|
label="์๋", |
|
|
minimum=0, |
|
|
maximum=MAX_SEED, |
|
|
step=1, |
|
|
value=0, |
|
|
) |
|
|
|
|
|
randomize_seed = gr.Checkbox(label="๋๋ค ์๋", value=True) |
|
|
|
|
|
with gr.Row(): |
|
|
width = gr.Slider( |
|
|
label="๋๋น", |
|
|
minimum=256, |
|
|
maximum=MAX_IMAGE_SIZE, |
|
|
step=32, |
|
|
value=1024, |
|
|
) |
|
|
|
|
|
height = gr.Slider( |
|
|
label="๋์ด", |
|
|
minimum=256, |
|
|
maximum=MAX_IMAGE_SIZE, |
|
|
step=32, |
|
|
value=1024, |
|
|
) |
|
|
|
|
|
num_inference_steps = gr.Slider( |
|
|
label="์ถ๋ก ๋จ๊ณ ์", |
|
|
minimum=1, |
|
|
maximum=50, |
|
|
step=1, |
|
|
value=4, |
|
|
) |
|
|
|
|
|
|
|
|
gr.Examples( |
|
|
examples=EXAMPLES, |
|
|
fn=generate_diagram, |
|
|
inputs=[prompt], |
|
|
outputs=[result, seed], |
|
|
cache_examples=True |
|
|
) |
|
|
|
|
|
|
|
|
gr.on( |
|
|
triggers=[run_button.click, prompt.submit], |
|
|
fn=generate_diagram, |
|
|
inputs=[prompt, seed, randomize_seed, width, height, num_inference_steps], |
|
|
outputs=[result, seed] |
|
|
) |
|
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
demo.queue() |
|
|
demo.launch( |
|
|
server_name="0.0.0.0", |
|
|
server_port=7860, |
|
|
share=False, |
|
|
show_error=True, |
|
|
debug=True |
|
|
) |