Spaces:
Build error
Build error
| 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"} | |
| ] | |
| ) |