frogcho123 commited on
Commit
7f66b95
1 Parent(s): 8d7bec1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -3
app.py CHANGED
@@ -44,18 +44,35 @@ def translate_voice(audio, target_lang):
44
  return filename, text, translated_text, target_lang
45
 
46
 
 
 
 
 
 
 
 
 
 
 
47
  def record_audio():
 
48
  fs = 16000
49
- duration = 5 # Record audio for 5 seconds, you can adjust the duration as needed
50
- audio = sd.rec(int(duration * fs), samplerate=fs, channels=1)
 
 
 
51
  sd.wait()
52
  return audio.flatten()
53
 
54
 
 
 
 
55
  iface = gr.Interface(
56
  fn=translate_voice,
57
  inputs=[
58
- gr.inputs.Audio(type="microphone", label="Speak"),
59
  gr.inputs.Dropdown(choices=['en', 'ru', 'de', 'fr'], label="Target Language")
60
  ],
61
  outputs=[
@@ -66,3 +83,4 @@ iface = gr.Interface(
66
  ]
67
  )
68
  iface.launch()
 
 
44
  return filename, text, translated_text, target_lang
45
 
46
 
47
+ def toggle_record(button):
48
+ global is_recording
49
+ if button:
50
+ button.text = "Stop Recording"
51
+ is_recording = True
52
+ else:
53
+ button.text = "Start Recording"
54
+ is_recording = False
55
+
56
+
57
  def record_audio():
58
+ global is_recording
59
  fs = 16000
60
+ audio = []
61
+ while is_recording:
62
+ block = sd.rec(int(fs), samplerate=fs, channels=1)
63
+ audio.append(block)
64
+ audio = sd.playrec(audio, samplerate=fs, channels=1)
65
  sd.wait()
66
  return audio.flatten()
67
 
68
 
69
+ is_recording = False
70
+
71
+
72
  iface = gr.Interface(
73
  fn=translate_voice,
74
  inputs=[
75
+ gr.inputs.Button(label="Start Recording", type="boolean", toggle=True, default=False, onclick=toggle_record),
76
  gr.inputs.Dropdown(choices=['en', 'ru', 'de', 'fr'], label="Target Language")
77
  ],
78
  outputs=[
 
83
  ]
84
  )
85
  iface.launch()
86
+