import gradio as gr import numpy as np def simulate_success(num_simulations, success_rate_1, success_rate_2, cost_per_attempt_1, cost_per_attempt_2): num_simulations = int(num_simulations) # Ensure num_simulations is an integer def run_simulation(success_rate, cost_per_attempt): # Simulating the outcomes results = np.random.rand(num_simulations) < success_rate # Counting the number of successes and failures successes = np.sum(results) failures = num_simulations - successes # Calculating the total cost for successes and failures total_success_cost = successes * cost_per_attempt total_failure_cost = failures * cost_per_attempt return successes, failures, total_success_cost, total_failure_cost # Running the simulation for both options successes_option_1, failures_option_1, success_cost_1, failure_cost_1 = run_simulation(success_rate_1, cost_per_attempt_1) successes_option_2, failures_option_2, success_cost_2, failure_cost_2 = run_simulation(success_rate_2, cost_per_attempt_2) # Formatting the output output_1 = ( f"Option 1: {successes_option_1} successes, {failures_option_1} failures\n" f"Total cost for successes: {success_cost_1} gold\n" f"Total cost for failures: {failure_cost_1} gold\n" f"Total cost overall: {success_cost_1 + failure_cost_1} gold" ) output_2 = ( f"Option 2: {successes_option_2} successes, {failures_option_2} failures\n" f"Total cost for successes: {success_cost_2} gold\n" f"Total cost for failures: {failure_cost_2} gold\n" f"Total cost overall: {success_cost_2 + failure_cost_2} gold" ) return output_1, output_2 # Create Gradio interface interface = gr.Interface( fn=simulate_success, inputs=[ gr.Number(label="Number of Simulations", value=30, precision=0), gr.Slider(0.01, 1.0, step=0.01, label="Success Rate Option 1", value=0.10), gr.Slider(0.01, 1.0, step=0.01, label="Success Rate Option 2", value=0.15), gr.Number(label="Cost per Attempt Option 1", value=850, precision=0), gr.Number(label="Cost per Attempt Option 2", value=1450, precision=0) ], outputs=[ gr.Textbox(label="Results for Option 1"), gr.Textbox(label="Results for Option 2") ], title="Success Rate Simulation", description="Simulate success and failure outcomes with different rates and costs." ) # Launch the app with sharing option enabled interface.launch()