diagram / app.py
aiqcamp's picture
Update app.py
0da9692 verified
raw
history blame
14.7 kB
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
# Enhanced examples with more detailed prompts and specific styling
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
}
]
# Convert examples to Gradio format
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 ์Šคํƒ€์ผ ์ˆ˜์ •
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;
}
"""
# Gradio ์ธํ„ฐํŽ˜์ด์Šค ์ˆ˜์ •
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): # scale ๊ฐ’ ์ฆ๊ฐ€
with gr.Group(elem_classes="input-box"):
prompt = gr.Text(
label="Diagram Prompt",
placeholder="Enter your diagram structure...",
lines=10, # ์ค„ ์ˆ˜ ์ฆ๊ฐ€
elem_classes="prompt-input" # CSS ํด๋ž˜์Šค ์ถ”๊ฐ€
)
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
)