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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import graphviz
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image, ImageDraw, ImageFont
|
| 5 |
+
|
| 6 |
+
# --- Setup: Create directories and placeholder image ---
|
| 7 |
+
|
| 8 |
+
# Create a directory for output images if it doesn't exist
|
| 9 |
+
if not os.path.exists("outputs"):
|
| 10 |
+
os.makedirs("outputs")
|
| 11 |
+
|
| 12 |
+
def create_placeholder_image(path="placeholder.png"):
|
| 13 |
+
"""Creates a placeholder image to display on app startup."""
|
| 14 |
+
try:
|
| 15 |
+
img = Image.new('RGB', (600, 800), color=(255, 255, 255))
|
| 16 |
+
draw = ImageDraw.Draw(img)
|
| 17 |
+
try:
|
| 18 |
+
# Use a common font, fallback to default
|
| 19 |
+
font = ImageFont.truetype("DejaVuSans.ttf", 24)
|
| 20 |
+
except IOError:
|
| 21 |
+
font = ImageFont.load_default()
|
| 22 |
+
text = "Flowchart will be generated here"
|
| 23 |
+
# Get text size using textbbox
|
| 24 |
+
bbox = draw.textbbox((0, 0), text, font=font)
|
| 25 |
+
text_width = bbox[2] - bbox[0]
|
| 26 |
+
text_height = bbox[3] - bbox[1]
|
| 27 |
+
position = ((600 - text_width) / 2, (800 - text_height) / 2)
|
| 28 |
+
draw.text(position, text, fill=(200, 200, 200), font=font)
|
| 29 |
+
img.save(path)
|
| 30 |
+
return path
|
| 31 |
+
except Exception:
|
| 32 |
+
return None
|
| 33 |
+
|
| 34 |
+
# --- Core Logic: Flowchart Generation ---
|
| 35 |
+
|
| 36 |
+
def generate_flowchart(prompt: str) -> str:
|
| 37 |
+
"""
|
| 38 |
+
Generates a flowchart using Graphviz based on the input prompt.
|
| 39 |
+
Returns the file path of the generated PNG image.
|
| 40 |
+
"""
|
| 41 |
+
# A simple keyword-based logic to generate different flowcharts
|
| 42 |
+
if "coffee" in prompt.lower():
|
| 43 |
+
dot_string = """
|
| 44 |
+
digraph G {
|
| 45 |
+
graph [pad="0.5", nodesep="0.5", ranksep="0.8", fontname="helvetica"];
|
| 46 |
+
node [shape=box, style="rounded,filled", fillcolor="#EAEAFB", fontname="helvetica"];
|
| 47 |
+
edge [color="#666666"];
|
| 48 |
+
rankdir=TB;
|
| 49 |
+
|
| 50 |
+
start [label="Start", shape=ellipse, fillcolor="#D5D6F9"];
|
| 51 |
+
have_beans [label="Have coffee beans?", shape=diamond, fillcolor="#F9EED5"];
|
| 52 |
+
buy_beans [label="Buy coffee beans"];
|
| 53 |
+
grind_beans [label="Grind beans"];
|
| 54 |
+
boil_water [label="Boil water"];
|
| 55 |
+
add_grounds [label="Add grounds to filter"];
|
| 56 |
+
pour_water [label="Pour water over grounds"];
|
| 57 |
+
wait [label="Wait for brewing"];
|
| 58 |
+
pour_cup [label="Pour coffee into cup"];
|
| 59 |
+
add_extras [label="Add milk/sugar?", shape=diamond, fillcolor="#F9EED5"];
|
| 60 |
+
add_milk_sugar [label="Add milk and/or sugar"];
|
| 61 |
+
drink [label="Drink coffee"];
|
| 62 |
+
end_node [label="Enjoy your coffee!", shape=ellipse, fillcolor="#D5D6F9"];
|
| 63 |
+
|
| 64 |
+
start -> have_beans;
|
| 65 |
+
have_beans -> grind_beans [label=" Yes"];
|
| 66 |
+
have_beans -> buy_beans [label="No "];
|
| 67 |
+
buy_beans -> grind_beans;
|
| 68 |
+
grind_beans -> boil_water;
|
| 69 |
+
boil_water -> add_grounds;
|
| 70 |
+
add_grounds -> pour_water;
|
| 71 |
+
pour_water -> wait;
|
| 72 |
+
wait -> pour_cup;
|
| 73 |
+
pour_cup -> add_extras;
|
| 74 |
+
add_extras -> add_milk_sugar [label=" Yes"];
|
| 75 |
+
add_extras -> drink [label="No "];
|
| 76 |
+
add_milk_sugar -> drink;
|
| 77 |
+
drink -> end_node;
|
| 78 |
+
}
|
| 79 |
+
"""
|
| 80 |
+
else:
|
| 81 |
+
# A default flowchart for any other prompt
|
| 82 |
+
cleaned_prompt = prompt.replace("'", "").replace("\"", "")[:60]
|
| 83 |
+
dot_string = f"""
|
| 84 |
+
digraph G {{
|
| 85 |
+
graph [fontname="helvetica"];
|
| 86 |
+
node [shape=box, style="rounded,filled", fillcolor="#EAEAFB", fontname="helvetica"];
|
| 87 |
+
rankdir=TB;
|
| 88 |
+
|
| 89 |
+
start [label="Start", shape=ellipse, fillcolor="#D5D6F9"];
|
| 90 |
+
process [label="Process user request:\\n'{cleaned_prompt}... '"];
|
| 91 |
+
end_node [label="End", shape=ellipse, fillcolor="#D5D6F9"];
|
| 92 |
+
|
| 93 |
+
start -> process -> end_node;
|
| 94 |
+
}}
|
| 95 |
+
"""
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
graph = graphviz.Source(dot_string)
|
| 99 |
+
# Save the rendered graph to a file, overwriting previous ones
|
| 100 |
+
output_path = graph.render(os.path.join("outputs", "flowchart"), format='png', cleanup=True)
|
| 101 |
+
return output_path
|
| 102 |
+
except Exception as e:
|
| 103 |
+
print(f"Error rendering graphviz: {e}")
|
| 104 |
+
return create_placeholder_image("error.png") # Return an error image if something goes wrong
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
def handle_generate_click(prompt: str):
|
| 108 |
+
"""Function to be called on button click. Updates the UI."""
|
| 109 |
+
if not prompt:
|
| 110 |
+
# Handle empty prompt case
|
| 111 |
+
placeholder_path = create_placeholder_image()
|
| 112 |
+
return placeholder_path, gr.update(visible=False)
|
| 113 |
+
|
| 114 |
+
image_path = generate_flowchart(prompt)
|
| 115 |
+
|
| 116 |
+
# Return the image path to update the gr.Image component
|
| 117 |
+
# and a gr.update object for the gr.DownloadButton
|
| 118 |
+
return image_path, gr.update(value=image_path, visible=True)
|
| 119 |
+
|
| 120 |
+
|
| 121 |
+
# --- Gradio UI ---
|
| 122 |
+
|
| 123 |
+
# Custom CSS to hide the footer for a cleaner look like the screenshot
|
| 124 |
+
css = """
|
| 125 |
+
footer {display: none !important}
|
| 126 |
+
.gradio-container {background-color: #f8f9fa}
|
| 127 |
+
"""
|
| 128 |
+
|
| 129 |
+
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 130 |
+
gr.Markdown("# AI Flowchart Generator")
|
| 131 |
+
gr.Markdown(
|
| 132 |
+
"Our AI Flowchart Generator allows you to create detailed flowcharts instantly. Whether you need a free "
|
| 133 |
+
"online AI flowchart generator from text or an intuitive flowchart maker AI, this tool delivers accurate and "
|
| 134 |
+
"visually engaging results. Discover how AI can enhance your workflow with the best flow chart generator "
|
| 135 |
+
"AI solution available online."
|
| 136 |
+
)
|
| 137 |
+
|
| 138 |
+
with gr.Group(): # Using gr.Group to create the white card effect
|
| 139 |
+
with gr.Row(equal_height=False):
|
| 140 |
+
with gr.Column(scale=1):
|
| 141 |
+
lang_dropdown = gr.Dropdown(
|
| 142 |
+
label="Language",
|
| 143 |
+
choices=["English"],
|
| 144 |
+
value="English"
|
| 145 |
+
)
|
| 146 |
+
chart_type_dropdown = gr.Dropdown(
|
| 147 |
+
choices=["Flowchart"],
|
| 148 |
+
value="Flowchart",
|
| 149 |
+
label=" " # Using space for an empty label like in the screenshot
|
| 150 |
+
)
|
| 151 |
+
prompt_input = gr.Textbox(
|
| 152 |
+
lines=10,
|
| 153 |
+
placeholder="Generate a flowchart for making coffee",
|
| 154 |
+
value="Generate a flowchart for making coffee",
|
| 155 |
+
label=" " # Empty label
|
| 156 |
+
)
|
| 157 |
+
with gr.Row():
|
| 158 |
+
# These buttons are for UI purposes and are not connected to logic
|
| 159 |
+
templates_btn = gr.Button("📄 Templates")
|
| 160 |
+
generate_btn = gr.Button("✨ Generate", variant="primary")
|
| 161 |
+
|
| 162 |
+
with gr.Column(scale=1):
|
| 163 |
+
output_image = gr.Image(
|
| 164 |
+
label="Generated Flowchart",
|
| 165 |
+
type="filepath",
|
| 166 |
+
interactive=False,
|
| 167 |
+
value=create_placeholder_image(),
|
| 168 |
+
height=600,
|
| 169 |
+
show_label=False
|
| 170 |
+
)
|
| 171 |
+
download_btn = gr.DownloadButton(
|
| 172 |
+
"⬇️ Download",
|
| 173 |
+
variant="primary",
|
| 174 |
+
visible=False, # Initially hidden
|
| 175 |
+
)
|
| 176 |
+
|
| 177 |
+
# --- Event Listeners ---
|
| 178 |
+
generate_btn.click(
|
| 179 |
+
fn=handle_generate_click,
|
| 180 |
+
inputs=[prompt_input],
|
| 181 |
+
outputs=[output_image, download_btn]
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
# Pre-load the default example when the app starts
|
| 185 |
+
demo.load(
|
| 186 |
+
fn=handle_generate_click,
|
| 187 |
+
inputs=[prompt_input],
|
| 188 |
+
outputs=[output_image, download_btn]
|
| 189 |
+
)
|
| 190 |
+
|
| 191 |
+
if __name__ == "__main__":
|
| 192 |
+
demo.launch()
|