barbaroo commited on
Commit
1d903b8
1 Parent(s): 587a51a

test stop button

Browse files
Files changed (1) hide show
  1. app.py +42 -16
app.py CHANGED
@@ -2,7 +2,8 @@ import gradio as gr
2
  import time
3
  from transformers import pipeline
4
  import torch
5
- import ffmpeg # Make sure it's ffmpeg-python
 
6
 
7
  # Check if GPU is available
8
  use_gpu = torch.cuda.is_available()
@@ -23,33 +24,50 @@ def extract_audio_from_m3u8(url):
23
  except Exception as e:
24
  return f"An error occurred: {e}"
25
 
 
 
 
 
 
26
  def transcribe_function(audio, state, uploaded_audio, m3u8_url):
 
 
 
27
  if m3u8_url:
28
  audio = extract_audio_from_m3u8(m3u8_url)
29
-
 
 
 
 
30
  if uploaded_audio is not None:
31
  audio = uploaded_audio
32
-
33
  if not audio:
34
- return {state_var: state, transcription_var: state} # Return a meaningful message
35
-
36
  try:
37
- time.sleep(3)
38
- text = p(audio, chunk_length_s= 50)["text"]
39
- state += text + "\n"
40
- return {state_var: state, transcription_var: state}
 
 
 
 
41
  except Exception as e:
42
- return {transcription_var: "An error occurred during transcription.", state_var: state} # Handle other exceptions
43
-
44
- # ... [most of your code remains unchanged]
45
-
46
  def reset_output(transcription, state):
47
  """Function to reset the state to an empty string."""
48
  return "", ""
49
 
50
- with gr.Blocks() as demo:
51
- state_var = gr.State("")
52
 
 
 
 
 
53
  with gr.Row():
54
  with gr.Column():
55
  microphone = gr.Audio(source="microphone", type="filepath", label="Microphone")
@@ -74,5 +92,13 @@ with gr.Blocks() as demo:
74
  [transcription_var, state_var],
75
  [transcription_var, state_var]
76
  )
 
 
 
 
 
 
 
77
 
78
- demo.launch()
 
 
2
  import time
3
  from transformers import pipeline
4
  import torch
5
+ import ffmpeg
6
+
7
 
8
  # Check if GPU is available
9
  use_gpu = torch.cuda.is_available()
 
24
  except Exception as e:
25
  return f"An error occurred: {e}"
26
 
27
+ def stop_transcription(state):
28
+ """Function to set a stop flag."""
29
+ state["stop_requested"] = True
30
+ return state
31
+
32
  def transcribe_function(audio, state, uploaded_audio, m3u8_url):
33
+ # Reset the stop request state at the start of transcription
34
+ state["stop_requested"] = False
35
+
36
  if m3u8_url:
37
  audio = extract_audio_from_m3u8(m3u8_url)
38
+
39
+ # Example of checking the stop state, could be integrated according to your logic
40
+ if state.get("stop_requested"):
41
+ return {"transcription_var": "Transcription stopped.", "state_var": state}
42
+
43
  if uploaded_audio is not None:
44
  audio = uploaded_audio
45
+
46
  if not audio:
47
+ return {"state_var": state, "transcription_var": "Please provide an audio file."}
48
+
49
  try:
50
+ # Simulate long-running task
51
+ for i in range(10):
52
+ if state.get("stop_requested"):
53
+ return {"transcription_var": "Transcription stopped.", "state_var": state}
54
+ time.sleep(1) # Simulate work being done
55
+ text = p(audio, chunk_length_s=50)["text"]
56
+ state["transcription"] += text + "\n"
57
+ return {"state_var": state, "transcription_var": state["transcription"]}
58
  except Exception as e:
59
+ return {"transcription_var": f"An error occurred during transcription: {e}", "state_var": state}
60
+
 
 
61
  def reset_output(transcription, state):
62
  """Function to reset the state to an empty string."""
63
  return "", ""
64
 
65
+ # Your setup continues here...
 
66
 
67
+ with gr.Blocks() as demo:
68
+ state_var = gr.State({"transcription": "", "stop_requested": False})
69
+
70
+
71
  with gr.Row():
72
  with gr.Column():
73
  microphone = gr.Audio(source="microphone", type="filepath", label="Microphone")
 
92
  [transcription_var, state_var],
93
  [transcription_var, state_var]
94
  )
95
+
96
+ stop_button = gr.Button("Stop Transcription")
97
+ stop_button.click(
98
+ stop_transcription,
99
+ inputs=[state_var],
100
+ outputs=[state_var]
101
+ )
102
 
103
+ # Launch the demo as usual
104
+ demo.launch()