Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -263,8 +263,7 @@ import wave
|
|
263 |
import tempfile
|
264 |
import numpy as np
|
265 |
|
266 |
-
# Global variables to store
|
267 |
-
file_index = 0
|
268 |
line_index = 0
|
269 |
lines = []
|
270 |
|
@@ -272,14 +271,13 @@ lines = []
|
|
272 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
273 |
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-calculator-demo")
|
274 |
|
275 |
-
# Ensure the directory exists
|
276 |
-
os.makedirs('./audio_samples', exist_ok=True)
|
277 |
-
|
278 |
# Function to read lines from a file
|
279 |
def read_lines_from_file(file_path):
|
280 |
-
global lines
|
281 |
with open(file_path, 'r') as file:
|
282 |
lines = file.readlines()
|
|
|
|
|
283 |
|
284 |
# Function to save audio to a WAV file
|
285 |
def save_audio_to_file(audio):
|
@@ -302,57 +300,54 @@ def save_to_hf_dataset(text, audio_path):
|
|
302 |
audio_data = f.read()
|
303 |
hf_writer.save({"text": text, "audio": audio_data})
|
304 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
305 |
# Gradio interface function
|
306 |
def audio_capture_interface():
|
307 |
-
global file_index, line_index, lines
|
308 |
-
|
309 |
-
# Check for files in the directory
|
310 |
-
files = [f for f in os.listdir('./audio_samples') if os.path.isfile(os.path.join('./audio_samples', f))]
|
311 |
-
if not files:
|
312 |
-
return gr.Interface(fn=lambda: "No text files found in the directory.", inputs=None, outputs="text")
|
313 |
-
|
314 |
-
read_lines_from_file(os.path.join('./audio_samples', files[file_index]))
|
315 |
-
|
316 |
-
# Define the interface components
|
317 |
-
audio_input = gr.Audio(source="microphone", type="numpy", label="Speak and click submit")
|
318 |
-
output_text = gr.Textbox(label="Status", placeholder="Status will appear here")
|
319 |
-
|
320 |
-
# Function to capture and process the audio input
|
321 |
-
def process_audio(audio):
|
322 |
-
global line_index, lines
|
323 |
-
|
324 |
-
try:
|
325 |
-
text_line = lines[line_index].strip()
|
326 |
-
file_path = save_audio_to_file(audio)
|
327 |
-
save_to_hf_dataset(text_line, file_path)
|
328 |
-
return f"Audio saved to {file_path} and uploaded to Hugging Face Dataset."
|
329 |
-
except Exception as e:
|
330 |
-
return f"Error saving audio: {str(e)}"
|
331 |
-
|
332 |
-
# Function to handle navigation buttons
|
333 |
-
def navigate_lines(button):
|
334 |
-
global line_index, lines
|
335 |
-
|
336 |
-
if button == 'forward':
|
337 |
-
line_index = min(line_index + 1, len(lines) - 1)
|
338 |
-
elif button == 'previous':
|
339 |
-
line_index = max(line_index - 1, 0)
|
340 |
-
|
341 |
-
return lines[line_index].strip()
|
342 |
-
|
343 |
-
# Create the Gradio interface
|
344 |
with gr.Blocks() as iface:
|
345 |
with gr.Row():
|
346 |
-
|
|
|
|
|
|
|
347 |
with gr.Row():
|
348 |
-
|
349 |
with gr.Row():
|
350 |
-
gr.Button("Previous").click(lambda: navigate_lines('previous'),
|
351 |
-
gr.Button("Forward").click(lambda: navigate_lines('forward'),
|
352 |
-
gr.Button("Submit").click(process_audio, inputs=audio_input, outputs=
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
353 |
|
354 |
return iface
|
355 |
|
356 |
# Launch the interface
|
357 |
iface = audio_capture_interface()
|
358 |
iface.launch()
|
|
|
|
263 |
import tempfile
|
264 |
import numpy as np
|
265 |
|
266 |
+
# Global variables to store line index and lines
|
|
|
267 |
line_index = 0
|
268 |
lines = []
|
269 |
|
|
|
271 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
272 |
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-calculator-demo")
|
273 |
|
|
|
|
|
|
|
274 |
# Function to read lines from a file
|
275 |
def read_lines_from_file(file_path):
|
276 |
+
global lines, line_index
|
277 |
with open(file_path, 'r') as file:
|
278 |
lines = file.readlines()
|
279 |
+
line_index = 0 # Reset line index when a new file is loaded
|
280 |
+
return lines[line_index].strip() if lines else "No lines found in the file."
|
281 |
|
282 |
# Function to save audio to a WAV file
|
283 |
def save_audio_to_file(audio):
|
|
|
300 |
audio_data = f.read()
|
301 |
hf_writer.save({"text": text, "audio": audio_data})
|
302 |
|
303 |
+
# Function to capture and process the audio input
|
304 |
+
def process_audio(audio):
|
305 |
+
global line_index, lines
|
306 |
+
try:
|
307 |
+
text_line = lines[line_index].strip()
|
308 |
+
file_path = save_audio_to_file(audio)
|
309 |
+
save_to_hf_dataset(text_line, file_path)
|
310 |
+
return f"Audio saved to {file_path} and uploaded to Hugging Face Dataset."
|
311 |
+
except Exception as e:
|
312 |
+
return f"Error saving audio: {str(e)}"
|
313 |
+
|
314 |
+
# Function to handle navigation buttons
|
315 |
+
def navigate_lines(button):
|
316 |
+
global line_index, lines
|
317 |
+
if button == 'forward':
|
318 |
+
line_index = min(line_index + 1, len(lines) - 1)
|
319 |
+
elif button == 'previous':
|
320 |
+
line_index = max(line_index - 1, 0)
|
321 |
+
return lines[line_index].strip()
|
322 |
+
|
323 |
# Gradio interface function
|
324 |
def audio_capture_interface():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
325 |
with gr.Blocks() as iface:
|
326 |
with gr.Row():
|
327 |
+
file_upload = gr.File(label="Upload a text file", file_types=["text"])
|
328 |
+
text_display = gr.Textbox(label="Text", value="Please upload a file to begin.", interactive=False)
|
329 |
+
with gr.Row():
|
330 |
+
audio_input = gr.Audio(source="microphone", type="numpy", label="Speak and click submit")
|
331 |
with gr.Row():
|
332 |
+
status_output = gr.Textbox(label="Status", placeholder="Status will appear here")
|
333 |
with gr.Row():
|
334 |
+
gr.Button("Previous").click(lambda: navigate_lines('previous'), outputs=text_display)
|
335 |
+
gr.Button("Forward").click(lambda: navigate_lines('forward'), outputs=text_display)
|
336 |
+
gr.Button("Submit").click(process_audio, inputs=audio_input, outputs=status_output)
|
337 |
+
|
338 |
+
# Handle file upload
|
339 |
+
def upload_file(file):
|
340 |
+
if file:
|
341 |
+
file_path = file.name
|
342 |
+
text_display.update(read_lines_from_file(file_path))
|
343 |
+
else:
|
344 |
+
text_display.update("No file uploaded.")
|
345 |
+
|
346 |
+
file_upload.upload(upload_file, outputs=text_display)
|
347 |
|
348 |
return iface
|
349 |
|
350 |
# Launch the interface
|
351 |
iface = audio_capture_interface()
|
352 |
iface.launch()
|
353 |
+
|