Buckets:
| import gradio as gr | |
| import json | |
| import time | |
| from simulations.tasks import SIMULATION_METHODS | |
| def get_method_schema(method_slug): | |
| """Get parameter schema for a simulation method.""" | |
| from simulations.models import SimulationMethod | |
| try: | |
| method = SimulationMethod.objects.get(slug=method_slug) | |
| return method.parameters_schema | |
| except SimulationMethod.DoesNotExist: | |
| return {} | |
| def create_param_inputs(method_slug): | |
| """Create Gradio input components based on method parameters.""" | |
| schema = get_method_schema(method_slug) | |
| inputs = [] | |
| for param_name, param_info in schema.get('properties', {}).items(): | |
| default = param_info.get('default', 100) | |
| min_val = param_info.get('minimum', 0) | |
| max_val = param_info.get('maximum', 1000) | |
| step = param_info.get('step', 1) | |
| inputs.append( | |
| gr.Slider( | |
| minimum=min_val, | |
| maximum=max_val, | |
| value=default, | |
| step=step, | |
| label=param_info.get('title', param_name) | |
| ) | |
| ) | |
| return inputs | |
| def run_simulation_gradio(method_slug, *args): | |
| """Run simulation and return results.""" | |
| from simulations.models import SimulationMethod, SimulationRun | |
| try: | |
| method = SimulationMethod.objects.get(slug=method_slug) | |
| except SimulationMethod.DoesNotExist: | |
| return f"Erreur: Méthode '{method_slug}' non trouvée" | |
| params = {} | |
| schema = get_method_schema(method_slug) | |
| properties = schema.get('properties', {}) | |
| for i, (param_name, param_info) in enumerate(properties.items()): | |
| if i < len(args): | |
| params[param_name] = args[i] | |
| run = SimulationRun.objects.create( | |
| method=method, | |
| name=f"Gradio - {method.name}", | |
| parameters=params | |
| ) | |
| method_func = SIMULATION_METHODS.get(method_slug) | |
| if not method_func: | |
| return f"Erreur: Méthode '{method_slug}' non implémentée" | |
| try: | |
| gen = method_func(params) | |
| result = None | |
| for progress in gen: | |
| yield f"Progression: {progress}%" | |
| result = progress if 'progress' in dir() else {} | |
| run.set_success(result or {}) | |
| run.refresh_from_db() | |
| output = f"Simulation terminée!\n\n" | |
| output += f"Méthode: {method.name}\n" | |
| output += f"ID: {run.id}\n\n" | |
| if run.result_data: | |
| output += "Résultats:\n" | |
| for key, value in run.result_data.items(): | |
| if isinstance(value, (int, float)): | |
| output += f" {key}: {value:.4f}\n" | |
| elif isinstance(value, list) and len(value) < 10: | |
| output += f" {key}: {value}\n" | |
| else: | |
| output += f" {key}: [{type(value).__name__}]\n" | |
| if run.plot_file: | |
| output += f"\nGraphique: {run.plot_file.url}" | |
| return output | |
| except Exception as e: | |
| run.set_failure(str(e)) | |
| return f"Erreur: {str(e)}" | |
| def update_inputs(method_slug): | |
| return create_param_inputs(method_slug) | |
| def create_gradio_app(): | |
| """Create the Gradio interface.""" | |
| with gr.Blocks(title="Simulation Numérique") as app: | |
| gr.Markdown("# Simulation Numérique") | |
| gr.Markdown("Sélectionnez une méthode et configurez les paramètres.") | |
| with gr.Row(): | |
| with gr.Column(): | |
| method_dropdown = gr.Dropdown( | |
| choices=list(SIMULATION_METHODS.keys()), | |
| value='monte-carlo-pi', | |
| label="Méthode de simulation" | |
| ) | |
| param_container = gr.Column() | |
| with param_container: | |
| initial_inputs = create_param_inputs('monte-carlo-pi') | |
| run_button = gr.Button("Lancer la simulation", variant="primary") | |
| with gr.Column(): | |
| output = gr.Textbox( | |
| label="Résultat", | |
| lines=20, | |
| interactive=False | |
| ) | |
| def on_method_change(method_slug): | |
| return create_param_inputs(method_slug) | |
| method_dropdown.change( | |
| fn=on_method_change, | |
| inputs=method_dropdown, | |
| outputs=param_container | |
| ) | |
| run_button.click( | |
| fn=run_simulation_gradio, | |
| inputs=[method_dropdown] + initial_inputs, | |
| outputs=output | |
| ) | |
| return app | |
| if __name__ == "__main__": | |
| app = create_gradio_app() | |
| app.launch() | |
Xet Storage Details
- Size:
- 4.55 kB
- Xet hash:
- 394dce6dacba6667ea2a1beed9d6cae1180a72b1a9c8ef9827e84c6f5ac5db2d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.