tiffank1802 commited on
Commit
8348bee
·
1 Parent(s): 61db407

Disable Celery on HF Spaces, run simulations synchronously

Browse files
simulations/views_views.py CHANGED
@@ -1,8 +1,11 @@
1
  from django.shortcuts import render, get_object_or_404, redirect
2
  from django.http import JsonResponse
3
  from .models import SimulationMethod, SimulationRun
4
- from .tasks import run_simulation
5
  import json
 
 
 
6
 
7
 
8
  def home(request):
@@ -51,7 +54,29 @@ def run_create(request, method_slug=None):
51
  parameters=parameters,
52
  )
53
 
54
- run_simulation.delay(run.id)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55
 
56
  return redirect('run_detail', run_id=run.id)
57
 
 
1
  from django.shortcuts import render, get_object_or_404, redirect
2
  from django.http import JsonResponse
3
  from .models import SimulationMethod, SimulationRun
4
+ from .tasks import run_simulation, clean_for_json, SIMULATION_METHODS
5
  import json
6
+ import os
7
+
8
+ HF_SPACES = os.environ.get('HF_SPACES', '').lower() in ('true', '1', 'yes')
9
 
10
 
11
  def home(request):
 
54
  parameters=parameters,
55
  )
56
 
57
+ if HF_SPACES:
58
+ run.set_running()
59
+ method_func = SIMULATION_METHODS.get(run.method.slug)
60
+
61
+ if method_func:
62
+ gen = method_func(run.parameters)
63
+ result = None
64
+ try:
65
+ while True:
66
+ progress = next(gen)
67
+ run.progress = progress
68
+ run.save(update_fields=['progress'])
69
+ except StopIteration as e:
70
+ result = e.value
71
+
72
+ if result:
73
+ result = clean_for_json(result)
74
+
75
+ run.set_success(result)
76
+ else:
77
+ run.set_failure(f"Méthode inconnue: {run.method.slug}")
78
+ else:
79
+ run_simulation.delay(run.id)
80
 
81
  return redirect('run_detail', run_id=run.id)
82
 
simulationserver/celery.py CHANGED
@@ -1,11 +1,13 @@
1
  import os
2
  from celery import Celery
3
 
4
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'simulationserver.settings')
5
 
6
- app = Celery('simulationserver')
7
- app.config_from_object('django.conf:settings', namespace='CELERY')
8
- app.autodiscover_tasks()
 
 
9
 
10
 
11
  @app.task(bind=True, ignore_result=True)
 
1
  import os
2
  from celery import Celery
3
 
4
+ HF_SPACES = os.environ.get('HF_SPACES', '').lower() in ('true', '1', 'yes')
5
 
6
+ if not HF_SPACES:
7
+ os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'simulationserver.settings')
8
+ app = Celery('simulationserver')
9
+ app.config_from_object('django.conf:settings', namespace='CELERY')
10
+ app.autodiscover_tasks()
11
 
12
 
13
  @app.task(bind=True, ignore_result=True)