Spanicin commited on
Commit
4290815
·
verified ·
1 Parent(s): 6775f15

Update app_parallel.py

Browse files
Files changed (1) hide show
  1. app_parallel.py +10 -6
app_parallel.py CHANGED
@@ -78,6 +78,8 @@ start_time = None
78
  audio_chunks = []
79
  preprocessed_data = None
80
  args = None
 
 
81
 
82
  app.config['temp_response'] = None
83
  app.config['generation_thread'] = None
@@ -285,7 +287,7 @@ def split_audio(audio_path, TEMP_DIR, chunk_duration):
285
  return audio_chunks, total_duration
286
 
287
  # Generator function to yield chunk results as they are processed
288
- def generate_chunks(audio_chunks, preprocessed_data, args):
289
  global TEMP_DIR
290
  future_to_chunk = {executor.submit(process_chunk, chunk[1], preprocessed_data, args): chunk[0] for chunk in audio_chunks}
291
 
@@ -297,7 +299,7 @@ def generate_chunks(audio_chunks, preprocessed_data, args):
297
  # add video for streaming
298
  loop = asyncio.new_event_loop()
299
  asyncio.set_event_loop(loop)
300
- loop.run_until_complete(add_video(temp_file_path))
301
  loop.close()
302
  yield json.dumps({'start_time': idx, 'video_index': chunk_idx}).encode('utf-8')
303
  except Exception as e:
@@ -309,10 +311,11 @@ def generate_chunks(audio_chunks, preprocessed_data, args):
309
  @app.route("/run", methods=['POST'])
310
  def parallel_processing():
311
  global start_time
312
- global audio_chunks, preprocessed_data, args
313
  start_time = time.time()
314
  global TEMP_DIR
315
  TEMP_DIR = create_temp_dir()
 
316
  unique_id = str(uuid.uuid4())
317
  print('request:',request.method)
318
  try:
@@ -367,7 +370,8 @@ def parallel_processing():
367
 
368
  os.makedirs('lives', exist_ok=True)
369
  print("Entering generate m3u8")
370
- generate_m3u8(audio_duration, f'lives/{unique_id}.m3u8')
 
371
 
372
  return jsonify({'video_url': f'{unique_id}.m3u8'}), 200
373
 
@@ -378,12 +382,12 @@ def parallel_processing():
378
 
379
  @app.route("/stream", methods=["GET"])
380
  def stream_results():
381
- global audio_chunks, preprocessed_data, args
382
  print("audio_chunks",audio_chunks)
383
  print("preprocessed_data",preprocessed_data)
384
  print("args",args)
385
  try:
386
- return stream_with_context(generate_chunks(audio_chunks, preprocessed_data, args))
387
  except Exception as e:
388
  return jsonify({'status': 'error', 'message': str(e)}), 500
389
 
 
78
  audio_chunks = []
79
  preprocessed_data = None
80
  args = None
81
+ unique_id = None
82
+ m3u8_path = None
83
 
84
  app.config['temp_response'] = None
85
  app.config['generation_thread'] = None
 
287
  return audio_chunks, total_duration
288
 
289
  # Generator function to yield chunk results as they are processed
290
+ def generate_chunks(audio_chunks, preprocessed_data, args, m3u8_path):
291
  global TEMP_DIR
292
  future_to_chunk = {executor.submit(process_chunk, chunk[1], preprocessed_data, args): chunk[0] for chunk in audio_chunks}
293
 
 
299
  # add video for streaming
300
  loop = asyncio.new_event_loop()
301
  asyncio.set_event_loop(loop)
302
+ loop.run_until_complete(add_video(temp_file_path, m3u8_path))
303
  loop.close()
304
  yield json.dumps({'start_time': idx, 'video_index': chunk_idx}).encode('utf-8')
305
  except Exception as e:
 
311
  @app.route("/run", methods=['POST'])
312
  def parallel_processing():
313
  global start_time
314
+ global audio_chunks, preprocessed_data, args, m3u8_path
315
  start_time = time.time()
316
  global TEMP_DIR
317
  TEMP_DIR = create_temp_dir()
318
+ global unique_id
319
  unique_id = str(uuid.uuid4())
320
  print('request:',request.method)
321
  try:
 
370
 
371
  os.makedirs('lives', exist_ok=True)
372
  print("Entering generate m3u8")
373
+ m3u8_path = f'lives/{unique_id}.m3u8'
374
+ generate_m3u8(audio_duration, m3u8_path)
375
 
376
  return jsonify({'video_url': f'{unique_id}.m3u8'}), 200
377
 
 
382
 
383
  @app.route("/stream", methods=["GET"])
384
  def stream_results():
385
+ global audio_chunks, preprocessed_data, args, m3u8_path
386
  print("audio_chunks",audio_chunks)
387
  print("preprocessed_data",preprocessed_data)
388
  print("args",args)
389
  try:
390
+ return stream_with_context(generate_chunks(audio_chunks, preprocessed_data, args, m3u8_path))
391
  except Exception as e:
392
  return jsonify({'status': 'error', 'message': str(e)}), 500
393