Spaces:
Running
Running
Update app.py
Browse files
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
|
| 621 |
##############################################################################
|
| 622 |
-
|
| 623 |
-
|
| 624 |
-
|
| 625 |
-
|
| 626 |
-
|
| 627 |
-
|
| 628 |
-
|
| 629 |
-
|
| 630 |
-
|
| 631 |
-
|
| 632 |
-
|
| 633 |
-
|
| 634 |
-
|
| 635 |
-
|
| 636 |
-
|
| 637 |
-
|
| 638 |
-
|
| 639 |
-
|
| 640 |
-
|
| 641 |
-
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
| 645 |
-
|
| 646 |
-
|
| 647 |
-
|
| 648 |
-
|
| 649 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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๋ฅผ ํตํด ์ด๋ฏธ์ง ์์ฑ"""
|