Spaces:
Build error
Build error
File size: 6,160 Bytes
8f10667 674c8a3 3e4ee4f 674c8a3 a46e94f 674c8a3 a46e94f 674c8a3 a46e94f 674c8a3 3e4ee4f a46e94f 674c8a3 a46e94f 674c8a3 a46e94f 674c8a3 a46e94f 3e4ee4f 674c8a3 a46e94f 674c8a3 8f10667 3e4ee4f 674c8a3 3e4ee4f 674c8a3 3e4ee4f a46e94f 674c8a3 a46e94f 3e4ee4f a46e94f 3e4ee4f 674c8a3 a46e94f 674c8a3 a46e94f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | import gradio as gr
import time
# Step 1: Define the application logic
def initialize_settings():
"""Initializes default settings for the workflow."""
return {
"temperature": 0.7,
"max_tokens": 100,
"model": "base-model-v1"
}
def process_input(user_text, settings):
"""
Step 2: Simulates processing the input text based on settings.
Includes a delay to demonstrate the progress bar.
"""
if not user_text.strip():
raise gr.Error("Input text cannot be empty!")
# Simulate processing time
time.sleep(1.5)
# Simple processing logic
temp = settings.get("temperature", 0.7)
processed_text = f"Processed (temp={temp}): {user_text.upper()}"
return processed_text
def generate_report(processed_text):
"""
Step 3: Generates a final report based on the processed text.
"""
time.sleep(1)
report = f"""
# Final Report
**Input Analysis**: Complete
**Status**: Success
**Data**: {processed_text}
---
*Generated by Step-by-Step Demo*
"""
return report
# Step 4: Build the Gradio 6 Interface
with gr.Blocks() as demo:
# Header with required link
gr.HTML("""
<div style="text-align: center; margin-bottom: 20px;">
<h1>🚀 Step-by-Step Execution Demo</h1>
<p>Built with <a href="https://huggingface.co/spaces/akhaliq/anycoder" target="_blank">anycoder</a></p>
</div>
""")
# We use a State component to keep track of settings across steps
settings_state = gr.State(value=initialize_settings())
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### ⚙️ Configuration")
# Configuration inputs
temp_slider = gr.Slider(
minimum=0.1,
maximum=2.0,
step=0.1,
value=0.7,
label="Temperature",
info="Controls randomness"
)
max_tokens = gr.Slider(
minimum=50,
maximum=500,
step=50,
value=100,
label="Max Tokens"
)
model_choice = gr.Dropdown(
choices=["base-model-v1", "pro-model-v2", "ultra-model-v3"],
value="base-model-v1",
label="Model Selection"
)
# Button to update settings state
update_settings_btn = gr.Button("Update Settings", variant="secondary")
gr.Markdown("---")
gr.Markdown("### 📝 Input")
user_input = gr.Textbox(
label="Enter Text",
placeholder="Type something here to process...",
lines=3
)
# Main execution button
run_btn = gr.Button("Run Pipeline", variant="primary", size="lg")
with gr.Column(scale=1):
gr.Markdown("### 📊 Execution Results")
# Step indicators (using Markdown for simplicity)
step_1_status = gr.Markdown("⏳ **Step 1**: Waiting for input...")
step_2_status = gr.Markdown("⏸️ **Step 2**: Waiting for Step 1...")
step_3_status = gr.Markdown("⏸️ **Step 3**: Waiting for Step 2...")
gr.Markdown("---")
# Output areas
processed_output = gr.Textbox(
label="Processed Output",
interactive=False,
visible=False
)
final_report = gr.Markdown(
label="Final Report",
visible=False
)
# Event Listeners
# 1. Update settings state when configuration changes
def update_settings(temp, tokens, model, current_settings):
new_settings = current_settings.copy()
new_settings["temperature"] = temp
new_settings["max_tokens"] = tokens
new_settings["model"] = model
return new_settings, gr.Info("Settings updated successfully!")
update_settings_btn.click(
fn=update_settings,
inputs=[temp_slider, max_tokens, model_choice, settings_state],
outputs=[settings_state]
)
# 2. The Main Pipeline Execution
def run_pipeline(text, settings):
# Step 1: Validate and Update Status
if not text.strip():
raise gr.Error("Please enter text first.")
yield {
step_1_status: "✅ **Step 1**: Input received and validated.",
step_2_status: "⏳ **Step 2**: Processing data...",
step_3_status: "⏸️ **Step 3**: Waiting for Step 2...",
processed_output: gr.Textbox(visible=False),
final_report: gr.Markdown(visible=False)
}
# Step 2: Process Data
processed = process_input(text, settings)
yield {
step_2_status: "✅ **Step 2**: Data processed successfully.",
step_3_status: "⏳ **Step 3**: Generating report...",
processed_output: gr.Textbox(value=processed, visible=True)
}
# Step 3: Generate Report
report = generate_report(processed)
yield {
step_3_status: "✅ **Step 3**: Report generated successfully.",
final_report: gr.Markdown(value=report, visible=True)
}
# Connect the main button to the pipeline generator
run_btn.click(
fn=run_pipeline,
inputs=[user_input, settings_state],
outputs=[step_1_status, step_2_status, step_3_status, processed_output, final_report],
api_visibility="public"
)
# Launch the app with Gradio 6 specific parameters
demo.launch(
theme=gr.themes.Soft(
primary_hue="indigo",
secondary_hue="blue",
neutral_hue="slate",
font=gr.themes.GoogleFont("Inter"),
text_size="lg",
spacing_size="lg",
radius_size="md"
),
footer_links=[
{"label": "Built with anycoder", "url": "https://huggingface.co/spaces/akhaliq/anycoder"}
]
) |