Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,508 +1,311 @@
|
|
| 1 |
-
|
| 2 |
-
import os
|
| 3 |
-
import base64
|
| 4 |
-
import streamlit as st
|
| 5 |
from gradio_client import Client
|
|
|
|
| 6 |
from dotenv import load_dotenv
|
| 7 |
-
from pathlib import Path
|
| 8 |
-
import json
|
| 9 |
-
import hashlib
|
| 10 |
-
import time
|
| 11 |
-
from typing import Dict, Any
|
| 12 |
|
| 13 |
# Load environment variables
|
| 14 |
load_dotenv()
|
| 15 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
CACHE_DIR.mkdir(exist_ok=True)
|
| 20 |
-
|
| 21 |
-
# Cached example diagrams
|
| 22 |
-
CACHED_EXAMPLES = {
|
| 23 |
-
"literacy_mental": {
|
| 24 |
-
"title": "Literacy Mental Map",
|
| 25 |
-
"prompt": """A handrawn colorful mind map diagram, rugosity drawn lines, clear shapes, brain silhouette, text areas. must include the texts
|
| 26 |
-
LITERACY/MENTAL
|
| 27 |
-
βββ PEACE [Dove Icon]
|
| 28 |
-
βββ HEALTH [Vitruvian Man ~60px]
|
| 29 |
-
βββ CONNECT [Brain-Mind Connection Icon]
|
| 30 |
-
βββ INTELLIGENCE
|
| 31 |
-
β βββ EVERYTHING [Globe Icon ~50px]
|
| 32 |
-
βββ MEMORY
|
| 33 |
-
βββ READING [Book Icon ~40px]
|
| 34 |
-
βββ SPEED [Speedometer Icon]
|
| 35 |
-
βββ CREATIVITY
|
| 36 |
-
βββ INTELLIGENCE [Lightbulb + Infinity ~30px]""",
|
| 37 |
-
"width": 1024,
|
| 38 |
-
"height": 1024,
|
| 39 |
-
"seed": 1872187377,
|
| 40 |
-
"cache_path": "literacy_mental.png"
|
| 41 |
-
}
|
| 42 |
-
}
|
| 43 |
-
|
| 44 |
-
DIAGRAM_EXAMPLES = [
|
| 45 |
-
{
|
| 46 |
-
"title": "Project Management Flow",
|
| 47 |
-
"prompt": """A handrawn colorful mind map diagram, rugosity drawn lines, clear shapes, project management flow.
|
| 48 |
-
PROJECT MANAGEMENT
|
| 49 |
-
βββ INITIATION [Rocket Icon]
|
| 50 |
-
βββ PLANNING [Calendar Icon]
|
| 51 |
-
βββ EXECUTION [Gear Icon]
|
| 52 |
-
βββ MONITORING
|
| 53 |
-
β βββ CONTROL [Dashboard Icon]
|
| 54 |
-
βββ CLOSURE [Checkmark Icon]""",
|
| 55 |
-
"width": 1024,
|
| 56 |
-
"height": 1024
|
| 57 |
-
},
|
| 58 |
-
{
|
| 59 |
-
"title": "Digital Marketing Strategy",
|
| 60 |
-
"prompt": """A handrawn colorful mind map diagram, rugosity drawn lines, modern style, marketing concept.
|
| 61 |
-
DIGITAL MARKETING
|
| 62 |
-
βββ SEO [Magnifying Glass]
|
| 63 |
-
βββ SOCIAL MEDIA [Network Icon]
|
| 64 |
-
βββ CONTENT
|
| 65 |
-
β βββ BLOG [Document Icon]
|
| 66 |
-
β βββ VIDEO [Play Button]
|
| 67 |
-
βββ ANALYTICS [Graph Icon]""",
|
| 68 |
-
"width": 1024,
|
| 69 |
-
"height": 1024
|
| 70 |
-
},
|
| 71 |
-
{
|
| 72 |
-
"title": "Software Development Lifecycle",
|
| 73 |
-
"prompt": """A handrawn colorful mind map diagram, technical style, software development flow.
|
| 74 |
-
SDLC
|
| 75 |
-
βββ REQUIREMENTS [Document Icon]
|
| 76 |
-
βββ DESIGN [Blueprint Icon]
|
| 77 |
-
βββ DEVELOPMENT [Code Icon]
|
| 78 |
-
βββ TESTING
|
| 79 |
-
β βββ UNIT [Checkbox Icon]
|
| 80 |
-
β βββ INTEGRATION [Puzzle Icon]
|
| 81 |
-
βββ DEPLOYMENT [Rocket Icon]""",
|
| 82 |
-
"width": 1024,
|
| 83 |
-
"height": 1024
|
| 84 |
-
},
|
| 85 |
-
{
|
| 86 |
-
"title": "Environmental Sustainability",
|
| 87 |
-
"prompt": """A handrawn colorful mind map diagram, nature-inspired style, environmental concept.
|
| 88 |
-
ECO-FRIENDLY
|
| 89 |
-
βββ REDUCE [Minus Icon]
|
| 90 |
-
βββ REUSE [Cycle Icon]
|
| 91 |
-
βββ RECYCLE [Triangle Icon]
|
| 92 |
-
βββ ENERGY
|
| 93 |
-
βββ SOLAR [Sun Icon]
|
| 94 |
-
βββ WIND [Windmill Icon]""",
|
| 95 |
-
"width": 1024,
|
| 96 |
-
"height": 1024
|
| 97 |
-
},
|
| 98 |
{
|
| 99 |
-
"title": "
|
| 100 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 101 |
-
|
| 102 |
-
βββ
|
| 103 |
-
βββ
|
| 104 |
-
βββ
|
| 105 |
-
β
|
| 106 |
-
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
"width": 1024,
|
| 109 |
"height": 1024
|
| 110 |
},
|
| 111 |
{
|
| 112 |
-
"title": "
|
| 113 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 114 |
-
|
| 115 |
-
βββ
|
| 116 |
-
βββ
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
"width": 1024,
|
| 122 |
"height": 1024
|
| 123 |
},
|
| 124 |
{
|
| 125 |
-
"title": "
|
| 126 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 127 |
-
|
| 128 |
-
βββ
|
| 129 |
-
βββ
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
"width": 1024,
|
| 135 |
"height": 1024
|
| 136 |
},
|
| 137 |
{
|
| 138 |
-
"title": "
|
| 139 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 140 |
-
|
| 141 |
-
βββ
|
| 142 |
-
βββ
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
"width": 1024,
|
| 148 |
"height": 1024
|
| 149 |
},
|
| 150 |
{
|
| 151 |
-
"title": "
|
| 152 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 153 |
-
|
| 154 |
-
βββ
|
| 155 |
-
βββ
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
"width": 1024,
|
| 161 |
"height": 1024
|
| 162 |
},
|
| 163 |
{
|
| 164 |
-
"title": "
|
| 165 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 166 |
-
|
| 167 |
-
βββ
|
| 168 |
-
βββ
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 173 |
"width": 1024,
|
| 174 |
"height": 1024
|
| 175 |
},
|
| 176 |
{
|
| 177 |
-
"title": "
|
| 178 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 179 |
-
|
| 180 |
-
βββ
|
| 181 |
-
βββ
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
"width": 1024,
|
| 187 |
"height": 1024
|
| 188 |
},
|
| 189 |
{
|
| 190 |
-
"title": "
|
| 191 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 192 |
-
|
| 193 |
-
βββ
|
| 194 |
-
βββ
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
"width": 1024,
|
| 200 |
"height": 1024
|
| 201 |
},
|
| 202 |
{
|
| 203 |
-
"title": "
|
| 204 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 205 |
-
|
| 206 |
-
βββ
|
| 207 |
-
βββ
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
"width": 1024,
|
| 213 |
"height": 1024
|
| 214 |
},
|
| 215 |
{
|
| 216 |
-
"title": "
|
| 217 |
-
"prompt": """A handrawn colorful mind map diagram,
|
| 218 |
-
|
| 219 |
-
βββ
|
| 220 |
-
βββ
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
CONTENT
|
| 232 |
-
βββ TEXT [Document Icon]
|
| 233 |
-
βββ IMAGES [Camera Icon]
|
| 234 |
-
βββ VIDEO [Play Icon]
|
| 235 |
-
βββ AUDIO
|
| 236 |
-
βββ PODCAST [Microphone Icon]
|
| 237 |
-
βββ MUSIC [Note Icon]""",
|
| 238 |
-
"width": 1024,
|
| 239 |
-
"height": 1024
|
| 240 |
-
},
|
| 241 |
-
{
|
| 242 |
-
"title": "Risk Management",
|
| 243 |
-
"prompt": """A handrawn colorful mind map diagram, analytical style, risk assessment.
|
| 244 |
-
RISK
|
| 245 |
-
βββ IDENTIFY [Eye Icon]
|
| 246 |
-
βββ ANALYZE [Graph Icon]
|
| 247 |
-
βββ MITIGATE [Shield Icon]
|
| 248 |
-
βββ MONITOR
|
| 249 |
-
βββ TRACK [Chart Icon]
|
| 250 |
-
βββ REPORT [Document Icon]""",
|
| 251 |
-
"width": 1024,
|
| 252 |
-
"height": 1024
|
| 253 |
-
},
|
| 254 |
-
{
|
| 255 |
-
"title": "Quality Assurance",
|
| 256 |
-
"prompt": """A handrawn colorful mind map diagram, technical style, quality control.
|
| 257 |
-
QUALITY
|
| 258 |
-
βββ STANDARDS [Checkmark Icon]
|
| 259 |
-
βββ TESTING [Magnifier Icon]
|
| 260 |
-
βββ INSPECTION [Eye Icon]
|
| 261 |
-
βββ IMPROVEMENT
|
| 262 |
-
βββ FEEDBACK [Chat Icon]
|
| 263 |
-
βββ ACTION [Gear Icon]""",
|
| 264 |
-
"width": 1024,
|
| 265 |
-
"height": 1024
|
| 266 |
-
},
|
| 267 |
-
{
|
| 268 |
-
"title": "Communication Channels",
|
| 269 |
-
"prompt": """A handrawn colorful mind map diagram, network style, communication methods.
|
| 270 |
-
COMMUNICATION
|
| 271 |
-
βββ EMAIL [Envelope Icon]
|
| 272 |
-
βββ CHAT [Message Icon]
|
| 273 |
-
βββ PHONE [Phone Icon]
|
| 274 |
-
βββ MEETINGS
|
| 275 |
-
βββ VIRTUAL [Screen Icon]
|
| 276 |
-
βββ IN-PERSON [Group Icon]""",
|
| 277 |
-
"width": 1024,
|
| 278 |
-
"height": 1024
|
| 279 |
-
},
|
| 280 |
-
{
|
| 281 |
-
"title": "Career Development",
|
| 282 |
-
"prompt": """A handrawn colorful mind map diagram, professional style, career growth.
|
| 283 |
-
CAREER
|
| 284 |
-
βββ SKILLS [Tools Icon]
|
| 285 |
-
βββ EXPERIENCE [Clock Icon]
|
| 286 |
-
βββ NETWORK [Connect Icon]
|
| 287 |
-
βββ GOALS
|
| 288 |
-
βββ SHORT [Flag Icon]
|
| 289 |
-
βββ LONG [Mountain Icon]""",
|
| 290 |
-
"width": 1024,
|
| 291 |
-
"height": 1024
|
| 292 |
-
},
|
| 293 |
-
{
|
| 294 |
-
"title": "Problem Solving",
|
| 295 |
-
"prompt": """A handrawn colorful mind map diagram, analytical style, solution process.
|
| 296 |
-
PROBLEM
|
| 297 |
-
βββ IDENTIFY [Eye Icon]
|
| 298 |
-
βββ ANALYZE [Brain Icon]
|
| 299 |
-
βββ SOLVE [Key Icon]
|
| 300 |
-
βββ VERIFY
|
| 301 |
-
βββ TEST [Checkbox Icon]
|
| 302 |
-
βββ REVIEW [Magnifier Icon]""",
|
| 303 |
"width": 1024,
|
| 304 |
"height": 1024
|
| 305 |
}
|
| 306 |
]
|
| 307 |
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
class DiagramCache:
|
| 316 |
-
def __init__(self, cache_dir: Path):
|
| 317 |
-
self.cache_dir = cache_dir
|
| 318 |
-
self.cache_dir.mkdir(exist_ok=True)
|
| 319 |
-
self._load_cache()
|
| 320 |
-
|
| 321 |
-
def _load_cache(self):
|
| 322 |
-
"""Load existing cache entries"""
|
| 323 |
-
self.cache_index = {}
|
| 324 |
-
if (self.cache_dir / "cache_index.json").exists():
|
| 325 |
-
with open(self.cache_dir / "cache_index.json", "r") as f:
|
| 326 |
-
self.cache_index = json.load(f)
|
| 327 |
-
|
| 328 |
-
def _save_cache_index(self):
|
| 329 |
-
"""Save cache index to disk"""
|
| 330 |
-
with open(self.cache_dir / "cache_index.json", "w") as f:
|
| 331 |
-
json.dump(self.cache_index, f)
|
| 332 |
-
|
| 333 |
-
def _get_cache_key(self, params: Dict[str, Any]) -> str:
|
| 334 |
-
"""Generate cache key from parameters"""
|
| 335 |
-
param_str = json.dumps(params, sort_keys=True)
|
| 336 |
-
return hashlib.md5(param_str.encode()).hexdigest()
|
| 337 |
-
|
| 338 |
-
def get(self, params: Dict[str, Any]) -> Path:
|
| 339 |
-
"""Get cached result if exists"""
|
| 340 |
-
cache_key = self._get_cache_key(params)
|
| 341 |
-
cache_info = self.cache_index.get(cache_key)
|
| 342 |
-
if cache_info:
|
| 343 |
-
cache_path = self.cache_dir / cache_info["filename"]
|
| 344 |
-
if cache_path.exists():
|
| 345 |
-
return cache_path
|
| 346 |
-
return None
|
| 347 |
-
|
| 348 |
-
def put(self, params: Dict[str, Any], result_path: Path):
|
| 349 |
-
"""Store result in cache"""
|
| 350 |
-
cache_key = self._get_cache_key(params)
|
| 351 |
-
filename = f"{cache_key}{result_path.suffix}"
|
| 352 |
-
cache_path = self.cache_dir / filename
|
| 353 |
-
|
| 354 |
-
# Copy result to cache
|
| 355 |
-
with open(result_path, "rb") as src, open(cache_path, "wb") as dst:
|
| 356 |
-
dst.write(src.read())
|
| 357 |
-
|
| 358 |
-
# Update index
|
| 359 |
-
self.cache_index[cache_key] = {
|
| 360 |
-
"filename": filename,
|
| 361 |
-
"timestamp": time.time(),
|
| 362 |
-
"params": params
|
| 363 |
-
}
|
| 364 |
-
self._save_cache_index()
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
# Initialize cache
|
| 368 |
-
diagram_cache = DiagramCache(CACHE_DIR)
|
| 369 |
-
|
| 370 |
-
@st.cache_data
|
| 371 |
-
def generate_cached_example(example_id: str) -> str:
|
| 372 |
-
"""Generate and cache example diagram"""
|
| 373 |
-
example = CACHED_EXAMPLES[example_id]
|
| 374 |
-
client = Client("black-forest-labs/FLUX.1-schnell")
|
| 375 |
-
|
| 376 |
-
# Check cache first
|
| 377 |
-
cache_path = diagram_cache.get(example)
|
| 378 |
-
if cache_path:
|
| 379 |
-
with open(cache_path, "rb") as f:
|
| 380 |
-
return base64.b64encode(f.read()).decode()
|
| 381 |
-
|
| 382 |
-
# Generate new image
|
| 383 |
-
result = client.predict(
|
| 384 |
-
prompt=example["prompt"],
|
| 385 |
-
seed=example["seed"],
|
| 386 |
-
randomize_seed=False,
|
| 387 |
-
width=example["width"],
|
| 388 |
-
height=example["height"],
|
| 389 |
-
num_inference_steps=4,
|
| 390 |
-
api_name="/infer"
|
| 391 |
-
)
|
| 392 |
-
|
| 393 |
-
# Cache the result
|
| 394 |
-
diagram_cache.put(example, Path(result))
|
| 395 |
-
|
| 396 |
-
with open(result, "rb") as f:
|
| 397 |
-
return base64.b64encode(f.read()).decode()
|
| 398 |
-
|
| 399 |
-
def generate_diagram(prompt: str, width: int, height: int, seed: int = None) -> str:
|
| 400 |
-
"""Generate a new diagram"""
|
| 401 |
-
client = Client("black-forest-labs/FLUX.1-schnell")
|
| 402 |
-
params = {
|
| 403 |
-
"prompt": prompt,
|
| 404 |
-
"seed": seed if seed else 1872187377,
|
| 405 |
-
"width": width,
|
| 406 |
-
"height": height
|
| 407 |
-
}
|
| 408 |
-
|
| 409 |
-
# Check cache first
|
| 410 |
-
cache_path = diagram_cache.get(params)
|
| 411 |
-
if cache_path:
|
| 412 |
-
with open(cache_path, "rb") as f:
|
| 413 |
-
return base64.b64encode(f.read()).decode()
|
| 414 |
-
|
| 415 |
-
# Generate new image
|
| 416 |
try:
|
|
|
|
| 417 |
result = client.predict(
|
| 418 |
-
prompt
|
| 419 |
-
seed
|
| 420 |
-
|
| 421 |
-
width
|
| 422 |
-
height
|
| 423 |
-
|
| 424 |
api_name="/infer"
|
| 425 |
)
|
| 426 |
-
|
| 427 |
-
# Cache the result
|
| 428 |
-
diagram_cache.put(params, Path(result))
|
| 429 |
-
|
| 430 |
-
with open(result, "rb") as f:
|
| 431 |
-
return base64.b64encode(f.read()).decode()
|
| 432 |
except Exception as e:
|
| 433 |
-
|
| 434 |
-
return None
|
| 435 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 436 |
|
| 437 |
-
|
| 438 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 439 |
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
)
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
height,
|
| 478 |
-
4, # num_inference_steps
|
| 479 |
-
api_name="/infer"
|
| 480 |
-
)
|
| 481 |
-
st.session_state.generated_images[prompt] = result
|
| 482 |
-
st.image(result, caption="Generated Diagram", use_column_width=True)
|
| 483 |
-
except Exception as e:
|
| 484 |
-
st.error(f"Error generating diagram: {str(e)}")
|
| 485 |
-
|
| 486 |
-
with col2:
|
| 487 |
-
st.subheader("Tips for Better Results")
|
| 488 |
-
st.markdown("""
|
| 489 |
-
- Use clear hierarchical structures
|
| 490 |
-
- Include icon descriptions in brackets
|
| 491 |
-
- Keep text concise and meaningful
|
| 492 |
-
- Use consistent formatting
|
| 493 |
-
""")
|
| 494 |
-
|
| 495 |
-
st.subheader("Template Structure")
|
| 496 |
-
st.code("""
|
| 497 |
-
MAIN TOPIC
|
| 498 |
-
βββ SUBTOPIC 1 [Icon]
|
| 499 |
-
βββ SUBTOPIC 2 [Icon]
|
| 500 |
-
βββ SUBTOPIC 3
|
| 501 |
-
βββ DETAIL 1 [Icon]
|
| 502 |
-
βββ DETAIL 2 [Icon]
|
| 503 |
-
""")
|
| 504 |
|
|
|
|
| 505 |
if __name__ == "__main__":
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
|
|
|
|
|
|
|
|
|
| 2 |
from gradio_client import Client
|
| 3 |
+
import os
|
| 4 |
from dotenv import load_dotenv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
# Load environment variables
|
| 7 |
load_dotenv()
|
| 8 |
HF_TOKEN = os.getenv("HF_TOKEN")
|
| 9 |
|
| 10 |
+
# Enhanced examples with more detailed prompts and specific styling
|
| 11 |
+
EXAMPLES = [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
{
|
| 13 |
+
"title": "Knowledge Tree",
|
| 14 |
+
"prompt": """A handrawn colorful mind map diagram, educational style, vibrant colors, clear hierarchy, golden ratio layout.
|
| 15 |
+
KNOWLEDGE
|
| 16 |
+
βββ ACQUISITION [Brain with Lightning ~60px]
|
| 17 |
+
β βββ READING [Open Book with Glow]
|
| 18 |
+
β βββ PRACTICE [Hands-on Tools]
|
| 19 |
+
β βββ OBSERVATION [Eye with Magnifier]
|
| 20 |
+
βββ PROCESSING [Gear Network ~50px]
|
| 21 |
+
β βββ ANALYSIS [Graph Trending Up]
|
| 22 |
+
β βββ SYNTHESIS [Puzzle Pieces]
|
| 23 |
+
βββ RETENTION [Memory Chip ~45px]
|
| 24 |
+
β βββ SHORT-TERM [Quick Flash]
|
| 25 |
+
β βββ LONG-TERM [Solid Archive]
|
| 26 |
+
βββ APPLICATION
|
| 27 |
+
βββ CREATION [Artist Palette]
|
| 28 |
+
βββ INNOVATION [Lightbulb Constellation]""",
|
| 29 |
"width": 1024,
|
| 30 |
"height": 1024
|
| 31 |
},
|
| 32 |
{
|
| 33 |
+
"title": "Digital Transformation",
|
| 34 |
+
"prompt": """A handrawn colorful mind map diagram, tech-focused style, neon accents, circuit board patterns.
|
| 35 |
+
DIGITAL TRANSFORM
|
| 36 |
+
βββ CLOUD [Cloud with Data Rain ~55px]
|
| 37 |
+
β βββ STORAGE [Database Cluster]
|
| 38 |
+
β βββ COMPUTING [Server Array]
|
| 39 |
+
βββ AUTOMATION [Robot Arm ~50px]
|
| 40 |
+
β βββ WORKFLOWS [Flowchart]
|
| 41 |
+
β βββ AI/ML [Neural Network]
|
| 42 |
+
βββ SECURITY [Shield Matrix ~45px]
|
| 43 |
+
β βββ ENCRYPTION [Lock Code]
|
| 44 |
+
β βββ MONITORING [Radar Screen]
|
| 45 |
+
βββ INTEGRATION
|
| 46 |
+
βββ APIS [Puzzle Connect]
|
| 47 |
+
βββ MICROSERVICES [Building Blocks]""",
|
| 48 |
"width": 1024,
|
| 49 |
"height": 1024
|
| 50 |
},
|
| 51 |
{
|
| 52 |
+
"title": "Creative Process",
|
| 53 |
+
"prompt": """A handrawn colorful mind map diagram, artistic style, watercolor effects, flowing connections.
|
| 54 |
+
CREATIVITY
|
| 55 |
+
βββ INSPIRATION [Constellation Stars ~60px]
|
| 56 |
+
β βββ NATURE [Organic Patterns]
|
| 57 |
+
β βββ CULTURE [Global Icons]
|
| 58 |
+
βββ IDEATION [Floating Bubbles ~50px]
|
| 59 |
+
β βββ BRAINSTORM [Thunder Cloud]
|
| 60 |
+
β βββ REFINEMENT [Diamond Polish]
|
| 61 |
+
βββ EXECUTION [Artist Tools ~45px]
|
| 62 |
+
β βββ TECHNIQUE [Skilled Hands]
|
| 63 |
+
β βββ MEDIUM [Palette Mix]
|
| 64 |
+
βββ PRESENTATION
|
| 65 |
+
βββ GALLERY [Frame Display]
|
| 66 |
+
βββ FEEDBACK [Echo Ripples]""",
|
| 67 |
"width": 1024,
|
| 68 |
"height": 1024
|
| 69 |
},
|
| 70 |
{
|
| 71 |
+
"title": "Future Cities",
|
| 72 |
+
"prompt": """A handrawn colorful mind map diagram, futuristic style, holographic elements, sustainable themes.
|
| 73 |
+
SMART CITY
|
| 74 |
+
βββ MOBILITY [Hover Transport ~60px]
|
| 75 |
+
β βββ AUTONOMOUS [Self-Driving]
|
| 76 |
+
β βββ CONNECTED [Network Grid]
|
| 77 |
+
βββ ENERGY [Solar Crystal ~55px]
|
| 78 |
+
β βββ RENEWABLE [Green Power]
|
| 79 |
+
β βββ STORAGE [Battery Hub]
|
| 80 |
+
βββ LIVING [Eco Building ~50px]
|
| 81 |
+
β βββ VERTICAL [Sky Gardens]
|
| 82 |
+
β βββ COMMUNITY [People Connect]
|
| 83 |
+
βββ INFRASTRUCTURE
|
| 84 |
+
βββ AI GRID [Neural City]
|
| 85 |
+
βββ ECO SYSTEM [Nature Tech]""",
|
| 86 |
"width": 1024,
|
| 87 |
"height": 1024
|
| 88 |
},
|
| 89 |
{
|
| 90 |
+
"title": "Health Evolution",
|
| 91 |
+
"prompt": """A handrawn colorful mind map diagram, medical style, DNA helix patterns, wellness focus.
|
| 92 |
+
HEALTH 3.0
|
| 93 |
+
βββ PREVENTION [Shield DNA ~60px]
|
| 94 |
+
β βββ LIFESTYLE [Activity Pulse]
|
| 95 |
+
β βββ MONITORING [Health Watch]
|
| 96 |
+
βββ TREATMENT [Caduceus Tech ~55px]
|
| 97 |
+
β βββ PERSONALIZED [DNA Code]
|
| 98 |
+
β βββ REGENERATIVE [Cell Renew]
|
| 99 |
+
βββ ENHANCEMENT [Upgrade Spiral ~50px]
|
| 100 |
+
β βββ COGNITIVE [Brain Boost]
|
| 101 |
+
β βββ PHYSICAL [Body Optimize]
|
| 102 |
+
βββ INTEGRATION
|
| 103 |
+
βββ AI HEALTH [Smart Doctor]
|
| 104 |
+
βββ COMMUNITY [Global Care]""",
|
| 105 |
"width": 1024,
|
| 106 |
"height": 1024
|
| 107 |
},
|
| 108 |
{
|
| 109 |
+
"title": "Space Exploration",
|
| 110 |
+
"prompt": """A handrawn colorful mind map diagram, cosmic style, star field background, planetary elements.
|
| 111 |
+
SPACE FRONTIER
|
| 112 |
+
βββ DISCOVERY [Telescope Array ~60px]
|
| 113 |
+
β βββ MAPPING [Star Charts]
|
| 114 |
+
β βββ ANALYSIS [Data Stream]
|
| 115 |
+
βββ TRAVEL [Rocket Launch ~55px]
|
| 116 |
+
β βββ PROPULSION [Energy Core]
|
| 117 |
+
β βββ NAVIGATION [Space Map]
|
| 118 |
+
βββ COLONIZATION [Dome City ~50px]
|
| 119 |
+
β βββ HABITATS [Life Sphere]
|
| 120 |
+
β βββ RESOURCES [Mine Extract]
|
| 121 |
+
βββ RESEARCH
|
| 122 |
+
βββ ASTROBIOLOGY [Life Search]
|
| 123 |
+
βββ PHYSICS [Space Time]""",
|
| 124 |
"width": 1024,
|
| 125 |
"height": 1024
|
| 126 |
},
|
| 127 |
{
|
| 128 |
+
"title": "Ocean Innovation",
|
| 129 |
+
"prompt": """A handrawn colorful mind map diagram, marine style, wave patterns, aqua themes.
|
| 130 |
+
OCEAN TECH
|
| 131 |
+
βββ EXPLORATION [Deep Submersible ~60px]
|
| 132 |
+
β βββ MAPPING [Sonar Wave]
|
| 133 |
+
β βββ RESEARCH [Lab Bubble]
|
| 134 |
+
βββ CONSERVATION [Marine Life ~55px]
|
| 135 |
+
β βββ PROTECTION [Reef Shield]
|
| 136 |
+
β βββ RESTORATION [Growth Core]
|
| 137 |
+
βββ HARVESTING [Sustainable Net ~50px]
|
| 138 |
+
β βββ ENERGY [Wave Power]
|
| 139 |
+
β βββ RESOURCES [Bio Extract]
|
| 140 |
+
βββ MONITORING
|
| 141 |
+
βββ AI SYSTEMS [Smart Sensors]
|
| 142 |
+
βββ ECOLOGY [Life Web]""",
|
| 143 |
"width": 1024,
|
| 144 |
"height": 1024
|
| 145 |
},
|
| 146 |
{
|
| 147 |
+
"title": "Quantum Computing",
|
| 148 |
+
"prompt": """A handrawn colorful mind map diagram, quantum style, wave-particle duality, matrix patterns.
|
| 149 |
+
QUANTUM TECH
|
| 150 |
+
βββ COMPUTATION [Qubit Matrix ~60px]
|
| 151 |
+
β βββ PROCESSING [Wave Function]
|
| 152 |
+
β βββ ALGORITHMS [Code Quantum]
|
| 153 |
+
βββ APPLICATIONS [Use Cases ~55px]
|
| 154 |
+
β βββ SIMULATION [Model World]
|
| 155 |
+
β βββ OPTIMIZATION [Peak Find]
|
| 156 |
+
βββ INFRASTRUCTURE [Q-Hardware ~50px]
|
| 157 |
+
β βββ CONTROL [Pulse Shape]
|
| 158 |
+
β βββ COOLING [Zero Point]
|
| 159 |
+
βββ DEVELOPMENT
|
| 160 |
+
βββ SOFTWARE [Q-Code Web]
|
| 161 |
+
βββ INTEGRATION [Classical Bridge]""",
|
| 162 |
"width": 1024,
|
| 163 |
"height": 1024
|
| 164 |
},
|
| 165 |
{
|
| 166 |
+
"title": "Bio Engineering",
|
| 167 |
+
"prompt": """A handrawn colorful mind map diagram, biological style, DNA patterns, organic flow.
|
| 168 |
+
BIOTECH
|
| 169 |
+
βββ GENETICS [DNA Helix ~60px]
|
| 170 |
+
β βββ EDITING [CRISPR Tool]
|
| 171 |
+
β βββ SYNTHESIS [Gene Build]
|
| 172 |
+
βββ APPLICATIONS [Lab Array ~55px]
|
| 173 |
+
β βββ MEDICINE [Heal Cell]
|
| 174 |
+
β βββ AGRICULTURE [Grow Plus]
|
| 175 |
+
βββ PLATFORMS [Bio Factory ~50px]
|
| 176 |
+
β βββ SENSORS [Live Detect]
|
| 177 |
+
β βββ PROCESSORS [Cell Compute]
|
| 178 |
+
βββ INTEGRATION
|
| 179 |
+
βββ AI BIOLOGY [Smart Life]
|
| 180 |
+
βββ ECOSYSTEM [Nature Net]""",
|
| 181 |
"width": 1024,
|
| 182 |
"height": 1024
|
| 183 |
},
|
| 184 |
{
|
| 185 |
+
"title": "AI Evolution",
|
| 186 |
+
"prompt": """A handrawn colorful mind map diagram, neural network style, digital patterns, intelligence flow.
|
| 187 |
+
AI FUTURE
|
| 188 |
+
βββ COGNITION [Brain Network ~60px]
|
| 189 |
+
β βββ LEARNING [Growth Path]
|
| 190 |
+
β βββ REASONING [Logic Tree]
|
| 191 |
+
βββ PERCEPTION [Sensor Array ~55px]
|
| 192 |
+
β βββ VISION [Eye Matrix]
|
| 193 |
+
β βββ LANGUAGE [Word Web]
|
| 194 |
+
βββ INTERACTION [Connect Hub ~50px]
|
| 195 |
+
β βββ HUMAN [Bridge Link]
|
| 196 |
+
β βββ MACHINE [Code Path]
|
| 197 |
+
βββ EVOLUTION
|
| 198 |
+
βββ CONSCIOUSNESS [Mind Spark]
|
| 199 |
+
βββ CREATIVITY [Art Core]""",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
"width": 1024,
|
| 201 |
"height": 1024
|
| 202 |
}
|
| 203 |
]
|
| 204 |
|
| 205 |
+
def generate_diagram(prompt, width=1024, height=1024):
|
| 206 |
+
"""Generate a diagram using FLUX AI"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 207 |
try:
|
| 208 |
+
client = Client("black-forest-labs/FLUX.1-schnell", hf_token=HF_TOKEN)
|
| 209 |
result = client.predict(
|
| 210 |
+
prompt,
|
| 211 |
+
1872187377, # seed
|
| 212 |
+
False, # randomize_seed
|
| 213 |
+
width,
|
| 214 |
+
height,
|
| 215 |
+
4, # num_inference_steps
|
| 216 |
api_name="/infer"
|
| 217 |
)
|
| 218 |
+
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
except Exception as e:
|
| 220 |
+
raise gr.Error(f"Error generating diagram: {str(e)}")
|
|
|
|
| 221 |
|
| 222 |
+
# Convert examples to Gradio format
|
| 223 |
+
GRADIO_EXAMPLES = [
|
| 224 |
+
[example["prompt"], example["width"], example["height"]]
|
| 225 |
+
for example in EXAMPLES
|
| 226 |
+
]
|
| 227 |
|
| 228 |
+
# Create Gradio interface
|
| 229 |
+
demo = gr.Interface(
|
| 230 |
+
fn=generate_diagram,
|
| 231 |
+
inputs=[
|
| 232 |
+
gr.Textbox(
|
| 233 |
+
label="Diagram Prompt",
|
| 234 |
+
placeholder="Enter your diagram structure...",
|
| 235 |
+
lines=10
|
| 236 |
+
),
|
| 237 |
+
gr.Slider(
|
| 238 |
+
label="Width",
|
| 239 |
+
minimum=512,
|
| 240 |
+
maximum=2048,
|
| 241 |
+
step=128,
|
| 242 |
+
value=1024
|
| 243 |
+
),
|
| 244 |
+
gr.Slider(
|
| 245 |
+
label="Height",
|
| 246 |
+
minimum=512,
|
| 247 |
+
maximum=2048,
|
| 248 |
+
step=128,
|
| 249 |
+
value=1024
|
| 250 |
+
)
|
| 251 |
+
],
|
| 252 |
+
outputs=gr.Image(type="filepath", label="Generated Diagram"),
|
| 253 |
+
title="π¨ FLUX Diagram Generator",
|
| 254 |
+
description="""Generate beautiful hand-drawn style diagrams using FLUX AI.
|
| 255 |
+
Create intricate mind maps with custom themes and detailed iconography.""",
|
| 256 |
+
article="""
|
| 257 |
+
### Tips for Better Results
|
| 258 |
+
- Use descriptive style hints (e.g., "vibrant colors", "flowing connections")
|
| 259 |
+
- Include specific icon sizes (e.g., "~60px")
|
| 260 |
+
- Add theme-specific patterns (e.g., "circuit board patterns", "wave patterns")
|
| 261 |
+
- Use consistent hierarchy and formatting
|
| 262 |
+
- Combine both functional and decorative elements
|
| 263 |
|
| 264 |
+
### Template Structure
|
| 265 |
+
```
|
| 266 |
+
MAIN TOPIC
|
| 267 |
+
βββ PRIMARY [Icon ~60px]
|
| 268 |
+
β βββ SUB-A [Specific Icon]
|
| 269 |
+
β βββ SUB-B [Detailed Icon]
|
| 270 |
+
βββ SECONDARY [Icon ~55px]
|
| 271 |
+
β βββ SUB-C [Themed Icon]
|
| 272 |
+
β βββ SUB-D [Custom Icon]
|
| 273 |
+
βββ TERTIARY
|
| 274 |
+
βββ SUB-E [Dynamic Icon]
|
| 275 |
+
βββ SUB-F [Unique Icon]
|
| 276 |
+
```
|
| 277 |
+
""",
|
| 278 |
+
examples=GRADIO_EXAMPLES,
|
| 279 |
+
cache_examples=True,
|
| 280 |
+
theme=gr.themes.Soft(),
|
| 281 |
+
css="""
|
| 282 |
+
.gradio-container {
|
| 283 |
+
font-family: 'Inter', sans-serif;
|
| 284 |
+
}
|
| 285 |
+
.gr-prose {
|
| 286 |
+
max-width: 850px;
|
| 287 |
+
margin: auto;
|
| 288 |
+
}
|
| 289 |
+
.gr-prose code {
|
| 290 |
+
background-color: #f3f4f6;
|
| 291 |
+
padding: 0.2em 0.4em;
|
| 292 |
+
border-radius: 0.3em;
|
| 293 |
+
font-size: 0.95em;
|
| 294 |
+
}
|
| 295 |
+
.gr-prose h3 {
|
| 296 |
+
margin-top: 1.5em;
|
| 297 |
+
color: #2563eb;
|
| 298 |
+
}
|
| 299 |
+
"""
|
| 300 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
|
| 302 |
+
# Launch app
|
| 303 |
if __name__ == "__main__":
|
| 304 |
+
# Set HF space configs
|
| 305 |
+
demo.queue(max_size=15) # Increased queue size
|
| 306 |
+
demo.launch(
|
| 307 |
+
share=False,
|
| 308 |
+
show_error=True,
|
| 309 |
+
server_name="0.0.0.0",
|
| 310 |
+
server_port=7860,
|
| 311 |
+
)
|