Archime commited on
Commit
f76a38c
·
1 Parent(s): fc64c8b

add 3 step

Browse files
Files changed (1) hide show
  1. app.py +54 -100
app.py CHANGED
@@ -4,49 +4,37 @@ from app.logger_config import (
4
  )
5
  import numpy as np
6
  import gradio as gr
7
- import asyncio
8
  from fastrtc.webrtc import WebRTC
9
  from fastrtc.utils import AdditionalOutputs
10
  from pydub import AudioSegment
11
- import time
12
- import os
13
  from gradio.utils import get_space
14
-
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  from app.utils import (
16
- raise_error,
17
  READ_SIZE,
18
  generate_coturn_config,
19
  read_and_stream_audio,
20
  stop_streaming,
21
- task,
22
- task_fake
23
  )
24
  from app.session_utils import (
25
  on_load,
26
  on_unload,
27
  get_active_session_hashes,
28
- register_session_hash_code,
29
  reset_all_active_sessions,
30
- get_active_task_flag_file,
31
- remove_active_task_flag_file
32
-
33
  )
34
 
35
- from app.ui_utils import (
36
- SUPPORTED_LANGS_MAP,
37
- EXAMPLE_CONFIGS,
38
- apply_preset_if_example,
39
- reset_to_defaults,
40
- summarize_config,
41
- handle_additional_outputs,
42
- get_custom_theme,
43
- on_file_load
44
- )
45
- import nemo.collections.asr as nemo_asr
46
- from app.session_utils import (
47
- get_active_task_flag_file,
48
- get_session_hashe_chunks_dir
49
- )
50
  import spaces
51
  # --------------------------------------------------------
52
  # Initialization
@@ -109,43 +97,14 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
109
  """
110
  )
111
 
112
- btn_proceed_streaming = gr.Button("Proceed to Streaming", visible=False)
113
- ui_components_oload_audio = [active_filepath, btn_proceed_streaming]
114
  main_audio.change(fn=on_file_load, inputs=[main_audio], outputs=ui_components_oload_audio)
115
- # main_audio.stop_recording(fn=on_file_load, inputs=[main_audio], outputs=ui_components_one)
116
- # main_audio.clear(fn=on_file_load, inputs=[main_audio], outputs=ui_components_one)
117
 
118
- btn_proceed_streaming.click(lambda: gr.Walkthrough(selected=1), outputs=walkthrough)
119
 
120
  # === STEP 2 ===
121
- with gr.Step("Stream", id=1) as audio_stream:
122
- gr.Markdown("### Step 2: Start audio streaming")
123
- with gr.Group():
124
- with gr.Column():
125
- webrtc_stream = WebRTC(
126
- label="Live Stream",
127
- mode="receive",
128
- modality="audio",
129
- rtc_configuration=generate_coturn_config(),
130
- visible=True,
131
- inputs=main_audio,
132
- )
133
- start_stream_button = gr.Button("▶️ Start Streaming", variant="primary")
134
-
135
- webrtc_stream.stream(
136
- fn=read_and_stream_audio,
137
- inputs=[active_filepath, session_hash_code,gr.State(READ_SIZE)],
138
- outputs=[webrtc_stream],
139
- trigger=start_stream_button.click,
140
- concurrency_id="audio_stream",
141
- concurrency_limit=10,
142
- )
143
- status_message_stream = gr.Markdown("", elem_id="status-message-stream", visible=False)
144
- go_to_config = gr.Button("Go to Configuration", visible=False, variant="secondary")
145
- go_to_config.click(lambda: gr.Walkthrough(selected=2), outputs=walkthrough)
146
-
147
- # === STEP 3 ===
148
- with gr.Step("Configuration", id=2):
149
  gr.Markdown("### Step 3: Configure the Task")
150
  with gr.Group():
151
  with gr.Row():
@@ -234,24 +193,49 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
234
  )
235
 
236
  go_to_task = gr.Button("Go to Task")
237
- go_to_task.click(lambda: gr.Walkthrough(selected=3), outputs=walkthrough)
238
 
239
- # === STEP 4 ===
240
- with gr.Step("Task", id=3) as task_step:
241
- with gr.Row():
242
- gr.Markdown("## Step 4: Start the Task")
243
  with gr.Row():
 
 
244
  with gr.Column():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
245
  status_slider = gr.Slider(
246
  0, 100,
247
  value=0,
248
- label="Streaming Progress",
249
  show_label=True,
250
  interactive=False,
251
  visible=False,
252
  show_reset_button=False
253
  )
254
- stop_stream_button = gr.Button("⏹️ Stop Streaming", visible=False,variant="stop")
 
 
 
 
 
 
 
 
 
 
 
255
  with gr.Row():
256
  gr.Markdown("---")
257
  with gr.Row():
@@ -270,7 +254,7 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
270
  with gr.Row():
271
  status_message_task = gr.Markdown("", elem_id="status-message-task",elem_classes=["info"], visible=False)
272
  with gr.Row():
273
- start_task_button = gr.Button("▶️ Start Task", visible=True, variant="primary")
274
  stop_task_button = gr.Button("⏹️ Stop Task", visible=False,variant="stop")
275
 
276
  stop_stream_button.click(
@@ -278,46 +262,16 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
278
  inputs=[session_hash_code],
279
  )
280
 
281
- def stop_task_fn(session_hash_code):
282
- remove_active_task_flag_file(session_hash_code)
283
- yield "Task stopped by user."
284
-
285
-
286
  stop_task_button.click(
287
  fn=stop_task_fn,
288
  inputs=session_hash_code,
289
- outputs=task_output
290
  )
291
- # task(session_hash_code)
292
  config_task_ui = [session_hash_code,task_type, lang_source, lang_target,
293
  chunk_secs, left_context_secs, right_context_secs,
294
  streaming_policy, alignatt_thr, waitk_lagging,
295
  exclude_sink_frames, xatt_scores_layer, hallucinations_detector]
296
 
297
- def start_task_asr_ast(
298
- session_hash_code,
299
- task_type, lang_source, lang_target,
300
- chunk_secs, left_context_secs, right_context_secs,
301
- streaming_policy, alignatt_thr, waitk_lagging,
302
- exclude_sink_frames, xatt_scores_layer, hallucinations_detector
303
- ):
304
- """Stream transcription or translation results in real time."""
305
- accumulated = ""
306
- # Boucle sur le générateur de `task2()`
307
- for result, status, current_chunk in task(
308
- session_hash_code,
309
- task_type, lang_source, lang_target,
310
- chunk_secs, left_context_secs, right_context_secs,
311
- streaming_policy, alignatt_thr, waitk_lagging,
312
- exclude_sink_frames, xatt_scores_layer, hallucinations_detector
313
- ):
314
- if status == "success":
315
- yield accumulated + result, gr.update(visible=True,value=current_chunk , elem_classes=["info"]), gr.update(visible=False), gr.update(visible=True)
316
- accumulated += result
317
- elif status in ["warning","info" ]:
318
- yield accumulated, gr.update(visible=True,value=result , elem_classes=[status]), gr.update(visible=False), gr.update(visible=True)
319
- elif status in ["error", "done"]:
320
- yield accumulated, gr.update(visible=True,value=result , elem_classes=[status]), gr.update(visible=True), gr.update(visible=False)
321
 
322
 
323
  start_task_button.click(
@@ -330,11 +284,11 @@ with gr.Blocks(theme=theme, css=css_style) as demo:
330
  exclude_sink_frames, xatt_scores_layer, hallucinations_detector
331
 
332
  ],
333
- outputs=[task_output,status_message_task,start_task_button,stop_task_button]
334
  )
335
 
336
  ui_components = [
337
- start_stream_button, stop_stream_button,
338
  go_to_config, audio_source_step, status_slider,walkthrough,status_message_stream
339
  ]
340
 
 
4
  )
5
  import numpy as np
6
  import gradio as gr
 
7
  from fastrtc.webrtc import WebRTC
8
  from fastrtc.utils import AdditionalOutputs
9
  from pydub import AudioSegment
 
 
10
  from gradio.utils import get_space
11
+ from app.supported_languages import (
12
+ SUPPORTED_LANGS_MAP,
13
+ )
14
+ from app.ui_utils import (
15
+ EXAMPLE_CONFIGS,
16
+ apply_preset_if_example,
17
+ reset_to_defaults,
18
+ summarize_config,
19
+ handle_additional_outputs,
20
+ get_custom_theme,
21
+ on_file_load,
22
+ start_task_asr_ast,
23
+ stop_task_fn
24
+ )
25
  from app.utils import (
 
26
  READ_SIZE,
27
  generate_coturn_config,
28
  read_and_stream_audio,
29
  stop_streaming,
 
 
30
  )
31
  from app.session_utils import (
32
  on_load,
33
  on_unload,
34
  get_active_session_hashes,
 
35
  reset_all_active_sessions,
 
 
 
36
  )
37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  import spaces
39
  # --------------------------------------------------------
40
  # Initialization
 
97
  """
98
  )
99
 
100
+ go_to_config = gr.Button("Go to Configuration", visible=False)
101
+ ui_components_oload_audio = [active_filepath, go_to_config]
102
  main_audio.change(fn=on_file_load, inputs=[main_audio], outputs=ui_components_oload_audio)
 
 
103
 
104
+ go_to_config.click(lambda: gr.Walkthrough(selected=1), outputs=walkthrough)
105
 
106
  # === STEP 2 ===
107
+ with gr.Step("Configuration", id=1)as config_step:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  gr.Markdown("### Step 3: Configure the Task")
109
  with gr.Group():
110
  with gr.Row():
 
193
  )
194
 
195
  go_to_task = gr.Button("Go to Task")
196
+ go_to_task.click(lambda: gr.Walkthrough(selected=2), outputs=walkthrough)
197
 
198
+ # === STEP 3 ===
199
+ with gr.Step("Task", id=2) as task_step:
 
 
200
  with gr.Row():
201
+ gr.Markdown("## Step 4: Start Stream Task")
202
+ with gr.Group():
203
  with gr.Column():
204
+ webrtc_stream = WebRTC(
205
+ label="Live Stream",
206
+ mode="receive",
207
+ modality="audio",
208
+ rtc_configuration=generate_coturn_config(),
209
+ visible=True,
210
+ inputs=main_audio,
211
+ icon= "https://cdn-icons-png.flaticon.com/128/18429/18429788.png",
212
+ pulse_color= "#df7a7a",
213
+ icon_radius= "10px",
214
+ icon_button_color= "rgb(255, 255, 255)",
215
+ height=150,
216
+ show_label=False
217
+ )
218
  status_slider = gr.Slider(
219
  0, 100,
220
  value=0,
221
+ label="Streaming Progress %",
222
  show_label=True,
223
  interactive=False,
224
  visible=False,
225
  show_reset_button=False
226
  )
227
+ start_stream_button = gr.Button("▶️ Start Streaming", variant="primary")
228
+ stop_stream_button = gr.Button("⏹️ Stop Streaming", visible=False,variant="stop")
229
+ webrtc_stream.stream(
230
+ fn=read_and_stream_audio,
231
+ inputs=[active_filepath, session_hash_code,gr.State(READ_SIZE)],
232
+ outputs=[webrtc_stream],
233
+ trigger=start_stream_button.click,
234
+ concurrency_id="audio_stream",
235
+ concurrency_limit=10,
236
+ )
237
+ status_message_stream = gr.Markdown("", elem_id="status-message-stream", visible=False)
238
+
239
  with gr.Row():
240
  gr.Markdown("---")
241
  with gr.Row():
 
254
  with gr.Row():
255
  status_message_task = gr.Markdown("", elem_id="status-message-task",elem_classes=["info"], visible=False)
256
  with gr.Row():
257
+ start_task_button = gr.Button("▶️ Start Task", visible=False, variant="primary")
258
  stop_task_button = gr.Button("⏹️ Stop Task", visible=False,variant="stop")
259
 
260
  stop_stream_button.click(
 
262
  inputs=[session_hash_code],
263
  )
264
 
 
 
 
 
 
265
  stop_task_button.click(
266
  fn=stop_task_fn,
267
  inputs=session_hash_code,
268
+ outputs=task_output
269
  )
 
270
  config_task_ui = [session_hash_code,task_type, lang_source, lang_target,
271
  chunk_secs, left_context_secs, right_context_secs,
272
  streaming_policy, alignatt_thr, waitk_lagging,
273
  exclude_sink_frames, xatt_scores_layer, hallucinations_detector]
274
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
275
 
276
 
277
  start_task_button.click(
 
284
  exclude_sink_frames, xatt_scores_layer, hallucinations_detector
285
 
286
  ],
287
+ outputs=[task_output,status_message_task,start_task_button,stop_task_button,config_step]
288
  )
289
 
290
  ui_components = [
291
+ start_stream_button, stop_stream_button,start_task_button,
292
  go_to_config, audio_source_step, status_slider,walkthrough,status_message_stream
293
  ]
294