Update app.py
Browse files
app.py
CHANGED
|
@@ -5,23 +5,24 @@ from huggingface_hub import InferenceClient
|
|
| 5 |
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
|
| 7 |
# --- Constants and Configuration ---
|
| 8 |
-
#
|
| 9 |
MODEL_ID = "google/flan-t5-large"
|
| 10 |
|
| 11 |
-
#
|
| 12 |
-
|
| 13 |
-
SYSTEM_PROMPT = """You are an expert at creating flowcharts. A user will provide a text prompt, and your task is to generate the flowchart description in the Graphviz DOT language.
|
| 14 |
-
|
| 15 |
Your response MUST be ONLY the Graphviz DOT language source code for a directed graph (digraph).
|
| 16 |
- The graph should be top-to-bottom (`rankdir=TB`).
|
| 17 |
- Use rounded boxes for process steps (`shape=box, style="rounded,filled", fillcolor="#EAEAFB"`).
|
| 18 |
- Use diamonds for decision points (`shape=diamond, fillcolor="#F9EED5"`).
|
| 19 |
- Use ellipses for start and end nodes (`shape=ellipse, fillcolor="#D5D6F9"`).
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
# --- Helper Functions ---
|
| 23 |
def create_placeholder_image(text="Flowchart will be generated here", size=(600, 800), path="placeholder.png"):
|
| 24 |
-
|
| 25 |
try:
|
| 26 |
img = Image.new('RGB', size, color=(255, 255, 255))
|
| 27 |
draw = ImageDraw.Draw(img)
|
|
@@ -53,18 +54,24 @@ def generate_flowchart(prompt: str, hf_token: str):
|
|
| 53 |
return create_placeholder_image("Please enter a prompt to generate a flowchart."), None
|
| 54 |
|
| 55 |
try:
|
| 56 |
-
# 1.
|
|
|
|
|
|
|
| 57 |
client = InferenceClient(model=MODEL_ID, token=hf_token)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
temperature=0.7,
|
|
|
|
| 65 |
)
|
| 66 |
-
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
# Sometimes the model still adds markdown, let's strip it just in case
|
| 69 |
if dot_code.startswith("```dot"):
|
| 70 |
dot_code = dot_code[len("```dot"):].strip()
|
|
@@ -84,8 +91,8 @@ def generate_flowchart(prompt: str, hf_token: str):
|
|
| 84 |
error_message = f"An error occurred.\nThis could be due to an invalid API token,\nan issue with the AI model, or invalid generated DOT code.\n\nDetails: {str(e)}"
|
| 85 |
return create_placeholder_image(error_message), gr.update(visible=False)
|
| 86 |
|
| 87 |
-
|
| 88 |
# --- Gradio UI ---
|
|
|
|
| 89 |
css = """
|
| 90 |
footer {display: none !important}
|
| 91 |
.gradio-container {background-color: #f8f9fa}
|
|
@@ -93,9 +100,7 @@ footer {display: none !important}
|
|
| 93 |
"""
|
| 94 |
|
| 95 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
| 96 |
-
# Get the Hugging Face token from environment variables
|
| 97 |
hf_token = os.environ.get("HF_TOKEN")
|
| 98 |
-
|
| 99 |
gr.Markdown("# AI Flowchart Generator")
|
| 100 |
gr.Markdown(
|
| 101 |
"Our AI Flowchart Generator allows you to create detailed flowcharts instantly. Whether you need a free "
|
|
@@ -103,7 +108,6 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 103 |
"visually engaging results. Discover how AI can enhance your workflow with the best flow chart generator "
|
| 104 |
"AI solution available online."
|
| 105 |
)
|
| 106 |
-
|
| 107 |
with gr.Group():
|
| 108 |
with gr.Row(equal_height=False):
|
| 109 |
with gr.Column(scale=1):
|
|
@@ -114,9 +118,7 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 114 |
)
|
| 115 |
with gr.Row():
|
| 116 |
generate_btn = gr.Button("✨ Generate", variant="primary")
|
| 117 |
-
|
| 118 |
status_display = gr.Markdown("", elem_id="status_display")
|
| 119 |
-
|
| 120 |
with gr.Column(scale=1):
|
| 121 |
output_image = gr.Image(
|
| 122 |
label="Generated Flowchart",
|
|
@@ -131,25 +133,10 @@ with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
| 131 |
variant="primary",
|
| 132 |
visible=False,
|
| 133 |
)
|
| 134 |
-
|
| 135 |
-
# --- Event Listeners ---
|
| 136 |
def on_generate_click(prompt):
|
| 137 |
-
|
| 138 |
-
yield (
|
| 139 |
-
gr.update(interactive=False), # Disable button
|
| 140 |
-
gr.update(visible=False), # Hide download button
|
| 141 |
-
create_placeholder_image("🧠 Generating with AI... Please wait."),
|
| 142 |
-
"Generating... this can take up to 30 seconds."
|
| 143 |
-
)
|
| 144 |
-
# Call the main generation function
|
| 145 |
img_path, download_btn_update = generate_flowchart(prompt, hf_token)
|
| 146 |
-
|
| 147 |
-
yield (
|
| 148 |
-
gr.update(interactive=True), # Re-enable button
|
| 149 |
-
download_btn_update, # Show/update download button
|
| 150 |
-
img_path,
|
| 151 |
-
"" # Clear status message
|
| 152 |
-
)
|
| 153 |
|
| 154 |
generate_btn.click(
|
| 155 |
fn=on_generate_click,
|
|
|
|
| 5 |
from PIL import Image, ImageDraw, ImageFont
|
| 6 |
|
| 7 |
# --- Constants and Configuration ---
|
| 8 |
+
# NOW USING a Text-Generation model which is more economical with free credits.
|
| 9 |
MODEL_ID = "google/flan-t5-large"
|
| 10 |
|
| 11 |
+
# The system prompt needs to be a single, direct instruction for this type of model.
|
| 12 |
+
SYSTEM_PROMPT_TEMPLATE = """Task: Generate a flowchart description in the Graphviz DOT language based on the following text.
|
|
|
|
|
|
|
| 13 |
Your response MUST be ONLY the Graphviz DOT language source code for a directed graph (digraph).
|
| 14 |
- The graph should be top-to-bottom (`rankdir=TB`).
|
| 15 |
- Use rounded boxes for process steps (`shape=box, style="rounded,filled", fillcolor="#EAEAFB"`).
|
| 16 |
- Use diamonds for decision points (`shape=diamond, fillcolor="#F9EED5"`).
|
| 17 |
- Use ellipses for start and end nodes (`shape=ellipse, fillcolor="#D5D6F9"`).
|
| 18 |
+
|
| 19 |
+
Text: "{user_prompt}"
|
| 20 |
+
|
| 21 |
+
DOT Language Code:"""
|
| 22 |
|
| 23 |
# --- Helper Functions ---
|
| 24 |
def create_placeholder_image(text="Flowchart will be generated here", size=(600, 800), path="placeholder.png"):
|
| 25 |
+
# (This function remains unchanged)
|
| 26 |
try:
|
| 27 |
img = Image.new('RGB', size, color=(255, 255, 255))
|
| 28 |
draw = ImageDraw.Draw(img)
|
|
|
|
| 54 |
return create_placeholder_image("Please enter a prompt to generate a flowchart."), None
|
| 55 |
|
| 56 |
try:
|
| 57 |
+
# 1. Prepare the full prompt for the text-generation model
|
| 58 |
+
full_prompt = SYSTEM_PROMPT_TEMPLATE.format(user_prompt=prompt)
|
| 59 |
+
|
| 60 |
client = InferenceClient(model=MODEL_ID, token=hf_token)
|
| 61 |
+
|
| 62 |
+
# --- THIS IS THE KEY CHANGE ---
|
| 63 |
+
# Use client.text_generation instead of client.chat_completion
|
| 64 |
+
dot_code = client.text_generation(
|
| 65 |
+
prompt=full_prompt,
|
| 66 |
+
max_new_tokens=1024, # Flan-T5 can generate a lot, let's give it space
|
| 67 |
temperature=0.7,
|
| 68 |
+
do_sample=True,
|
| 69 |
)
|
| 70 |
+
# --- END OF KEY CHANGE ---
|
| 71 |
|
| 72 |
+
# The result is a direct string, so we just need to strip it.
|
| 73 |
+
dot_code = dot_code.strip()
|
| 74 |
+
|
| 75 |
# Sometimes the model still adds markdown, let's strip it just in case
|
| 76 |
if dot_code.startswith("```dot"):
|
| 77 |
dot_code = dot_code[len("```dot"):].strip()
|
|
|
|
| 91 |
error_message = f"An error occurred.\nThis could be due to an invalid API token,\nan issue with the AI model, or invalid generated DOT code.\n\nDetails: {str(e)}"
|
| 92 |
return create_placeholder_image(error_message), gr.update(visible=False)
|
| 93 |
|
|
|
|
| 94 |
# --- Gradio UI ---
|
| 95 |
+
# (The entire Gradio UI block remains unchanged)
|
| 96 |
css = """
|
| 97 |
footer {display: none !important}
|
| 98 |
.gradio-container {background-color: #f8f9fa}
|
|
|
|
| 100 |
"""
|
| 101 |
|
| 102 |
with gr.Blocks(theme=gr.themes.Soft(), css=css) as demo:
|
|
|
|
| 103 |
hf_token = os.environ.get("HF_TOKEN")
|
|
|
|
| 104 |
gr.Markdown("# AI Flowchart Generator")
|
| 105 |
gr.Markdown(
|
| 106 |
"Our AI Flowchart Generator allows you to create detailed flowcharts instantly. Whether you need a free "
|
|
|
|
| 108 |
"visually engaging results. Discover how AI can enhance your workflow with the best flow chart generator "
|
| 109 |
"AI solution available online."
|
| 110 |
)
|
|
|
|
| 111 |
with gr.Group():
|
| 112 |
with gr.Row(equal_height=False):
|
| 113 |
with gr.Column(scale=1):
|
|
|
|
| 118 |
)
|
| 119 |
with gr.Row():
|
| 120 |
generate_btn = gr.Button("✨ Generate", variant="primary")
|
|
|
|
| 121 |
status_display = gr.Markdown("", elem_id="status_display")
|
|
|
|
| 122 |
with gr.Column(scale=1):
|
| 123 |
output_image = gr.Image(
|
| 124 |
label="Generated Flowchart",
|
|
|
|
| 133 |
variant="primary",
|
| 134 |
visible=False,
|
| 135 |
)
|
|
|
|
|
|
|
| 136 |
def on_generate_click(prompt):
|
| 137 |
+
yield (gr.update(interactive=False), gr.update(visible=False), create_placeholder_image("🧠 Generating with AI... Please wait."), "Generating... this can take up to 30 seconds.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 138 |
img_path, download_btn_update = generate_flowchart(prompt, hf_token)
|
| 139 |
+
yield (gr.update(interactive=True), download_btn_update, img_path, "")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
generate_btn.click(
|
| 142 |
fn=on_generate_click,
|