|
import gradio as gr |
|
import numpy as np |
|
import random |
|
import torch |
|
from diffusers import DiffusionPipeline |
|
import spaces |
|
|
|
dtype = torch.bfloat16 |
|
device = "cuda" if torch.cuda.is_available() else "cpu" |
|
|
|
|
|
pipe = DiffusionPipeline.from_pretrained( |
|
"black-forest-labs/FLUX.1-schnell", |
|
torch_dtype=dtype |
|
).to(device) |
|
|
|
MAX_SEED = np.iinfo(np.int32).max |
|
MAX_IMAGE_SIZE = 2048 |
|
|
|
|
|
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 |
|
] |
|
|
|
@spaces.GPU() |
|
def infer(prompt, seed=42, randomize_seed=False, width=1024, height=1024, num_inference_steps=4, progress=gr.Progress(track_tqdm=True)): |
|
if randomize_seed: |
|
seed = random.randint(0, MAX_SEED) |
|
generator = torch.Generator().manual_seed(seed) |
|
image = pipe( |
|
prompt=prompt, |
|
width=width, |
|
height=height, |
|
num_inference_steps=num_inference_steps, |
|
generator=generator, |
|
guidance_scale=0.0 |
|
).images[0] |
|
return image, seed |
|
|
|
|
|
css = """ |
|
.container { |
|
display: flex; |
|
flex-direction: row; |
|
height: 100%; |
|
} |
|
|
|
.input-column { |
|
flex: 1; |
|
padding: 20px; |
|
border-right: 2px solid #eee; |
|
max-width: 800px; /* ์ฆ๊ฐ๋ ์ต๋ ๋๋น */ |
|
} |
|
|
|
.examples-column { |
|
flex: 1; |
|
padding: 20px; |
|
overflow-y: auto; |
|
background: #f7f7f7; |
|
} |
|
|
|
.title { |
|
text-align: center; |
|
color: #2a2a2a; |
|
padding: 20px; |
|
font-size: 2.5em; |
|
font-weight: bold; |
|
background: linear-gradient(90deg, #f0f0f0 0%, #ffffff 100%); |
|
border-bottom: 3px solid #ddd; |
|
margin-bottom: 30px; |
|
} |
|
|
|
.subtitle { |
|
text-align: center; |
|
color: #666; |
|
margin-bottom: 30px; |
|
} |
|
|
|
.input-box { |
|
background: white; |
|
padding: 20px; |
|
border-radius: 10px; |
|
box-shadow: 0 2px 10px rgba(0,0,0,0.1); |
|
margin-bottom: 20px; |
|
width: 100%; /* ์ ์ฒด ๋๋น ์ฌ์ฉ */ |
|
} |
|
|
|
/* ํ
์คํธ ์
๋ ฅ ๋ฐ์ค ์คํ์ผ */ |
|
.input-box textarea { |
|
width: 100% !important; /* ๊ฐ์ ๋๋น 100% */ |
|
min-width: 600px !important; /* ์ต์ ๋๋น ์ค์ */ |
|
font-size: 14px !important; |
|
line-height: 1.5 !important; |
|
padding: 12px !important; |
|
} |
|
|
|
.example-card { |
|
background: white; |
|
padding: 15px; |
|
margin: 10px 0; |
|
border-radius: 8px; |
|
box-shadow: 0 2px 5px rgba(0,0,0,0.05); |
|
} |
|
|
|
.example-title { |
|
font-weight: bold; |
|
color: #2a2a2a; |
|
margin-bottom: 10px; |
|
} |
|
|
|
/* ๋ ์ด์์ ์กฐ์ */ |
|
.contain { |
|
max-width: 1400px !important; /* ์ ์ฒด ์ปจํ
์ด๋ ๋๋น ์ฆ๊ฐ */ |
|
margin: 0 auto !important; |
|
} |
|
|
|
/* ์
๋ ฅ ์์ญ ์กฐ์ */ |
|
.input-area { |
|
flex: 2 !important; /* ์
๋ ฅ ์์ญ ๋น์จ ์ฆ๊ฐ */ |
|
} |
|
|
|
/* ์์ ์์ญ ์กฐ์ */ |
|
.examples-area { |
|
flex: 1 !important; |
|
} |
|
""" |
|
|
|
|
|
|
|
with gr.Blocks(css=css) as demo: |
|
gr.Markdown( |
|
""" |
|
<div class="title">FLUX Diagram Generator</div> |
|
<div class="subtitle">Create beautiful hand-drawn style diagrams using FLUX AI</div> |
|
""") |
|
|
|
gr.HTML("""<a href="https://visitorbadge.io/status?path=https%3A%2F%2Faiqcamp-diagram.hf.space"> |
|
<img src="https://api.visitorbadge.io/api/visitors?path=https%3A%2F%2Faiqcamp-diagram.hf.space&countColor=%23263759" /> |
|
</a>""") |
|
|
|
|
|
|
|
with gr.Row(equal_height=True) as main_row: |
|
|
|
with gr.Column(elem_id="input-column", scale=2): |
|
with gr.Group(elem_classes="input-box"): |
|
prompt = gr.Text( |
|
label="Diagram Prompt", |
|
placeholder="Enter your diagram structure...", |
|
lines=10, |
|
elem_classes="prompt-input" |
|
) |
|
|
|
run_button = gr.Button("Generate Diagram", variant="primary") |
|
result = gr.Image(label="Generated Diagram") |
|
|
|
with gr.Accordion("Advanced Settings", open=False): |
|
seed = gr.Slider( |
|
label="Seed", |
|
minimum=0, |
|
maximum=MAX_SEED, |
|
step=1, |
|
value=0, |
|
) |
|
randomize_seed = gr.Checkbox(label="Randomize seed", value=True) |
|
|
|
with gr.Row(): |
|
width = gr.Slider( |
|
label="Width", |
|
minimum=256, |
|
maximum=MAX_IMAGE_SIZE, |
|
step=32, |
|
value=1024, |
|
) |
|
height = gr.Slider( |
|
label="Height", |
|
minimum=256, |
|
maximum=MAX_IMAGE_SIZE, |
|
step=32, |
|
value=1024, |
|
) |
|
|
|
num_inference_steps = gr.Slider( |
|
label="Number of inference steps", |
|
minimum=1, |
|
maximum=50, |
|
step=1, |
|
value=4, |
|
) |
|
|
|
|
|
with gr.Column(elem_id="examples-column", scale=1): |
|
gr.Markdown("### Example Diagrams") |
|
for example in EXAMPLES: |
|
with gr.Group(elem_classes="example-card"): |
|
gr.Markdown(f"#### {example['title']}") |
|
gr.Markdown(f"```\n{example['prompt']}\n```") |
|
|
|
def create_example_handler(ex): |
|
def handler(): |
|
return { |
|
prompt: ex["prompt"], |
|
width: ex["width"], |
|
height: ex["height"] |
|
} |
|
return handler |
|
|
|
gr.Button("Use This Example", size="sm").click( |
|
fn=create_example_handler(example), |
|
outputs=[prompt, width, height] |
|
) |
|
|
|
gr.on( |
|
triggers=[run_button.click, prompt.submit], |
|
fn=infer, |
|
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 |
|
) |