openfree commited on
Commit
0710314
ยท
verified ยท
1 Parent(s): 4423868

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -60
app.py CHANGED
@@ -114,6 +114,7 @@ def initialize_ai_image_api():
114
  ##############################################################################
115
  # AI Image Generation API Configuration - FLUX API
116
  ##############################################################################
 
117
  FLUX_API_URL = "http://211.233.58.201:7896"
118
  FLUX_API_ENABLED = False
119
  flux_api_client = None
@@ -616,70 +617,104 @@ def generate_diagram_locally(json_data: str, diagram_type: str, output_format: s
616
  logger.error(f"Failed to generate diagram locally: {e}")
617
  return None
618
 
 
619
  ##############################################################################
620
- # FLUX Image Generation Functions
621
  ##############################################################################
622
- def generate_flux_prompt(title: str, content: str) -> str:
623
- """์Šฌ๋ผ์ด๋“œ ๋‚ด์šฉ์„ ๊ธฐ๋ฐ˜์œผ๋กœ FLUX ์ด๋ฏธ์ง€ ํ”„๋กฌํ”„ํŠธ ์ƒ์„ฑ"""
624
-
625
- # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ AFTER (๊ฐ•ํ™” ๋ฒ„์ „) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
626
- system_prompt = """You are a senior information-visualisation designer crafting prompts for the FLUX model.
627
- Your task: turn ONE PowerPoint slide (title + exactly five bullet points) into a **diagram-style graphic** that mirrors the slideโ€™s logic, hierarchy and wording.
628
-
629
- Strict rules
630
- 1. Auto-select the ideal diagram type:
631
- โ€ข sequential verbs / stages โ†’ Flowchart / Timeline
632
- โ€ข branching themes โ†’ Mind-map / Radial
633
- โ€ข numeric comparisons โ†’ Flat Infographic with bars, pies or lines
634
- โ€ข UI / feature lists โ†’ Wire-frame or Product Sketch
635
- 2. Map **every bullet point** to a labelled node/segment; keep their original order.
636
- 3. Use explicit connectors: arrows โ€œโ†’โ€, tree branches โ€œโ”œโ”€โ”€โ€, swim-lane boxes, or chart axes as appropriate.
637
- 4. Embed short node labels **verbatim** from bullets (โ‰ค 4 words each).
638
- 5. If a bullet contains numbers, reflect them in the graphic (e.g. โ€œ45 %โ€ in a pie slice).
639
- 6. Optional icon cues in square brackets, e.g. [Gear Icon], only when they aid comprehension.
640
- 7. Finish with a succinct style cue: colour palette (e.g. โ€œcool blues & greysโ€), line style (โ€œhand-drawn sketchโ€ or โ€œsleek vectorโ€), whitespace preference.
641
- 8. โ‰ค 120 words, English, no markdown, no commentary โ€“ **output the prompt text only**.
642
-
643
- Mini-examples
644
- โ€ข โ€œSleek industrial **product design** sketch: coffee-machine, curved metal body, touchscreen, matte-black, hand-drawn concept lines.โ€
645
- โ€ข โ€œHand-drawn colourful **mind-map**: KNOWLEDGE centre โ†’ ACQUISITION (Reading, Practice, Observation) โ€ฆ pastel palette.โ€
646
- โ€ข โ€œClean hand-drawn **wire-frame mock-up** for mobile banking app: title, login, dashboard (balance|transactions|quick-actions), bottom nav โ€ฆ monochrome sketch lines.โ€
647
- โ€ข โ€œFlat corporate **infographic** โ€˜Global Renewable Energy Trends 2025โ€™: stacked bar Solar|Wind|Hydro across 5 regions, pie Solar 45 % โ€ฆ teal-orange palette.โ€
648
- โ€ข โ€œDetailed hand-drawn **business process diagram**: Market Analysis โ†’ Strategy โ†’ Design โ†’ Implementation โ†’ Review, directional arrows, icons (magnifying-glass, lightbulb, gear) โ€ฆ vibrant educational style.โ€
649
- โ€ข โ€œVibrant hand-drawn **flowchart** BUSINESS WORKFLOW: START โ†’ Collect Requirements โ†’ Analyze Data โ†’ Implementation (Frontend | Backend) โ†’ Test & Integration โ†’ DEPLOY โ†’ END โ€ฆ minimal icons, colour-coded nodes.โ€"""
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
650
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
651
 
652
-
653
- messages = [
654
- {"role": "system", "content": system_prompt},
655
- {"role": "user", "content": f"Create an image prompt for:\nTitle: {title}\nContent: {content[:500]}"}
656
- ]
657
-
658
- headers = {
659
- "Authorization": f"Bearer {FRIENDLI_TOKEN}",
660
- "Content-Type": "application/json"
661
- }
662
-
663
- payload = {
664
- "model": FRIENDLI_MODEL_ID,
665
- "messages": messages,
666
- "max_tokens": 200,
667
- "temperature": 0.8,
668
- "stream": False
669
- }
670
-
671
- try:
672
- response = requests.post(FRIENDLI_API_URL, headers=headers, json=payload, timeout=30)
673
- if response.status_code == 200:
674
- response_data = response.json()
675
- if 'choices' in response_data and len(response_data['choices']) > 0:
676
- prompt = response_data['choices'][0]['message']['content'].strip()
677
- return f"Professional business presentation slide illustration: {prompt}, modern clean style, corporate colors, white background"
678
- except Exception as e:
679
- logger.error(f"Error generating FLUX prompt: {e}")
680
-
681
- # Fallback prompt
682
- return f"Professional business presentation illustration about {title}, modern minimalist style, clean design, corporate colors"
683
 
684
  def generate_flux_image_via_api(prompt: str) -> Optional[str]:
685
  """FLUX API๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€ ์ƒ์„ฑ"""
 
114
  ##############################################################################
115
  # AI Image Generation API Configuration - FLUX API
116
  ##############################################################################
117
+
118
  FLUX_API_URL = "http://211.233.58.201:7896"
119
  FLUX_API_ENABLED = False
120
  flux_api_client = None
 
617
  logger.error(f"Failed to generate diagram locally: {e}")
618
  return None
619
 
620
+
621
  ##############################################################################
622
+ # FLUX Image Prompt Generator โ€“ supports 6 diagram styles
623
  ##############################################################################
624
+ # โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ Prompt Templates (6 Styles) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
625
+ EXAMPLE_PROMPTS: dict[str, str] = {
626
+ "Product Design": """A sleek industrial design concept for a coffee machine:
627
+ - Curved metallic body with minimal bezel
628
+ - Touchscreen panel for settings
629
+ - Modern matte black finish
630
+ - Hand-drawn concept sketch style""",
631
+ "Mindmap": """A handrawn colorful mind map diagram, educational style, vibrant colors, clear hierarchy, golden ratio layout.
632
+ KNOWLEDGE
633
+ โ”œโ”€โ”€ ACQUISITION [Brain with Lightning ~60px]
634
+ โ”‚ โ”œโ”€โ”€ READING [Open Book with Glow]
635
+ โ”‚ โ”œโ”€โ”€ PRACTICE [Hands-on Tools]
636
+ โ”‚ โ””โ”€โ”€ OBSERVATION [Eye with Magnifier]
637
+ โ”œโ”€โ”€ PROCESSING [Gear Network ~50px]
638
+ โ”‚ โ”œโ”€โ”€ ANALYSIS [Graph Trending Up]
639
+ โ”‚ โ””โ”€โ”€ SYNTHESIS [Puzzle Pieces]
640
+ โ”œโ”€โ”€ RETENTION [Memory Chip ~45px]
641
+ โ”‚ โ”œโ”€โ”€ SHORT-TERM [Quick Flash]
642
+ โ”‚ โ””โ”€โ”€ LONG-TERM [Solid Archive]
643
+ โ””โ”€โ”€ APPLICATION
644
+ โ”œโ”€โ”€ CREATION [Artist Palette]
645
+ โ””โ”€โ”€ INNOVATION [Lightbulb Constellation]""",
646
+ "Mockup": """A clean hand-drawn style wireframe for a mobile banking app:
647
+ - Title screen with logo
648
+ - Login screen (username, password, login button)
649
+ - Dashboard with 3 main sections (balance, transactions, quick actions)
650
+ - Bottom navigation bar (home, transfers, profile)""",
651
+ "Infographic": """A sophisticated flat-style infographic for a multinational corporation's annual report:
652
+ - Title: "Global Renewable Energy Trends 2025"
653
+ - Subtitle: "Market Share and Growth Analysis"
654
+ - Visual Elements:
655
+ - Multi-segmented bar charts comparing Solar, Wind, and Hydro energy production across regions
656
+ - Pie chart displaying overall energy distribution: Solar (45%), Wind (30%), Hydro (25%)
657
+ - Trend lines indicating year-over-year growth
658
+ - Icons: Sleek, minimalist representations of a sun, wind turbine, and water droplet
659
+ - Layout: Clean, grid-based design with ample white space and pastel accents for a modern corporate look
660
+ - Annotations: Brief, impactful data callouts highlighting key performance indicators and future forecasts""",
661
+ "Diagram": """A detailed hand-drawn diagram illustrating an end-to-end business workflow:
662
+ - Title: "Integrated Business Process Diagram"
663
+ - Components:
664
+ - Market Analysis โ†’ Strategy Development โ†’ Product Design โ†’ Implementation โ†’ Post-Launch Review
665
+ - Visual Elements:
666
+ - Directional arrows, magnifying glass, lightbulb, gear, checklist icons
667
+ - Style: Vibrant, educational yet professional
668
+ - Layout: Clear hierarchy, color-coded sections""",
669
+ "Flowchart": """A hand-drawn style flowchart, vibrant colors, minimalistic icons.
670
+ BUSINESS WORKFLOW
671
+ โ”œโ”€โ”€ START [Green Button ~40px]
672
+ โ”‚ โ”œโ”€โ”€ COLLECT REQUIREMENTS [Folder Icon]
673
+ โ”‚ โ””โ”€โ”€ ANALYZE DATA [Chart Icon]
674
+ โ”œโ”€โ”€ IMPLEMENTATION [Coding Symbol ~50px]
675
+ โ”‚ โ”œโ”€โ”€ FRONTEND [Browser Icon]
676
+ โ”‚ โ””โ”€โ”€ BACKEND [Server Icon]
677
+ โ”œโ”€โ”€ TEST & INTEGRATION [Gear Icon ~45px]
678
+ โ””โ”€โ”€ DEPLOY โ†’ END [Checkered Flag ~40px]"""
679
+ }
680
+ STYLE_KEYS = list(EXAMPLE_PROMPTS.keys())
681
 
682
+ def generate_flux_prompt(title: str, content: str, style_key: str) -> str:
683
+ """
684
+ Build a FLUX image-generation prompt for one slide, following six
685
+ pre-defined visual styles (Product Design, Mindmap, Mockup, Infographic,
686
+ Diagram, Flowchart). `content` is the raw bullet-point block coming from
687
+ the slide; `style_key` picks which template to use.
688
+ """
689
+ # 1) Extract clean bullet points (max 8)
690
+ bullets = [
691
+ re.sub(r'^[\-\โ€ข\โ—]\s*', '', line).strip()
692
+ for line in content.splitlines()
693
+ if line.strip().startswith(('-', 'โ€ข', 'โ—'))
694
+ ][:8]
695
+
696
+ # Build node / list representation for templates that need it
697
+ if bullets:
698
+ node_block = '\n'.join([f"- {b}" for b in bullets])
699
+ tree_block = '\n'.join([f"โ”œโ”€โ”€ {b}" if i < len(bullets) - 1 else f"โ””โ”€โ”€ {b}"
700
+ for i, b in enumerate(bullets)])
701
+ else:
702
+ node_block = "- No explicit bullet points provided -"
703
+ tree_block = node_block
704
+
705
+ # 2) Select template
706
+ tpl = EXAMPLE_PROMPTS.get(style_key, EXAMPLE_PROMPTS["Diagram"])
707
+
708
+ # 3) Inject dynamic parts
709
+ prompt_body = tpl.format(title=title, nodes=node_block if "{nodes}" in tpl else tree_block)
710
+
711
+ # 4) Final stylistic tail โ€“ ensure English, โ‰ค120 words
712
+ style_tail = "Corporate palette, white background, high-resolution vector, clean composition."
713
+
714
+ # 5) Return full prompt (no markdown / commentary)
715
+ full_prompt = f"{prompt_body}\n\n{style_tail}"
716
+ return full_prompt.strip()
717
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
718
 
719
  def generate_flux_image_via_api(prompt: str) -> Optional[str]:
720
  """FLUX API๋ฅผ ํ†ตํ•ด ์ด๋ฏธ์ง€ ์ƒ์„ฑ"""