Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -163,6 +163,100 @@
|
|
| 163 |
|
| 164 |
# demo.launch()
|
| 165 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 166 |
import gradio as gr
|
| 167 |
import os
|
| 168 |
import wave
|
|
@@ -178,6 +272,9 @@ lines = []
|
|
| 178 |
HF_TOKEN = os.getenv('HF_TOKEN')
|
| 179 |
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-calculator-demo")
|
| 180 |
|
|
|
|
|
|
|
|
|
|
| 181 |
# Function to read lines from a file
|
| 182 |
def read_lines_from_file(file_path):
|
| 183 |
global lines
|
|
@@ -209,8 +306,11 @@ def save_to_hf_dataset(text, audio_path):
|
|
| 209 |
def audio_capture_interface():
|
| 210 |
global file_index, line_index, lines
|
| 211 |
|
| 212 |
-
#
|
| 213 |
-
files = os.listdir('./audio_samples')
|
|
|
|
|
|
|
|
|
|
| 214 |
read_lines_from_file(os.path.join('./audio_samples', files[file_index]))
|
| 215 |
|
| 216 |
# Define the interface components
|
|
@@ -238,17 +338,17 @@ def audio_capture_interface():
|
|
| 238 |
elif button == 'previous':
|
| 239 |
line_index = max(line_index - 1, 0)
|
| 240 |
|
| 241 |
-
|
| 242 |
|
| 243 |
# Create the Gradio interface
|
| 244 |
with gr.Blocks() as iface:
|
| 245 |
with gr.Row():
|
| 246 |
-
gr.Textbox(label="Text", value=lines[line_index], interactive=False)
|
| 247 |
with gr.Row():
|
| 248 |
audio_input.render()
|
| 249 |
with gr.Row():
|
| 250 |
-
gr.Button("Previous").click(lambda: navigate_lines('previous'),
|
| 251 |
-
gr.Button("Forward").click(lambda: navigate_lines('forward'),
|
| 252 |
gr.Button("Submit").click(process_audio, inputs=audio_input, outputs=output_text)
|
| 253 |
|
| 254 |
return iface
|
|
@@ -256,4 +356,3 @@ def audio_capture_interface():
|
|
| 256 |
# Launch the interface
|
| 257 |
iface = audio_capture_interface()
|
| 258 |
iface.launch()
|
| 259 |
-
|
|
|
|
| 163 |
|
| 164 |
# demo.launch()
|
| 165 |
|
| 166 |
+
# import gradio as gr
|
| 167 |
+
# import os
|
| 168 |
+
# import wave
|
| 169 |
+
# import tempfile
|
| 170 |
+
# import numpy as np
|
| 171 |
+
|
| 172 |
+
# # Global variables to store file and line index
|
| 173 |
+
# file_index = 0
|
| 174 |
+
# line_index = 0
|
| 175 |
+
# lines = []
|
| 176 |
+
|
| 177 |
+
# # Hugging Face token and dataset saver
|
| 178 |
+
# HF_TOKEN = os.getenv('HF_TOKEN')
|
| 179 |
+
# hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced-calculator-demo")
|
| 180 |
+
|
| 181 |
+
# # Function to read lines from a file
|
| 182 |
+
# def read_lines_from_file(file_path):
|
| 183 |
+
# global lines
|
| 184 |
+
# with open(file_path, 'r') as file:
|
| 185 |
+
# lines = file.readlines()
|
| 186 |
+
|
| 187 |
+
# # Function to save audio to a WAV file
|
| 188 |
+
# def save_audio_to_file(audio):
|
| 189 |
+
# sample_rate, data = audio # audio is a tuple (sample_rate, data)
|
| 190 |
+
|
| 191 |
+
# # Save the audio data as a WAV file in a temporary location
|
| 192 |
+
# with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
|
| 193 |
+
# with wave.open(tmp_file.name, 'wb') as wav_file:
|
| 194 |
+
# wav_file.setnchannels(1) # Mono audio
|
| 195 |
+
# wav_file.setsampwidth(2) # 2 bytes per sample (16-bit PCM)
|
| 196 |
+
# wav_file.setframerate(sample_rate)
|
| 197 |
+
# wav_file.writeframes(data.tobytes())
|
| 198 |
+
|
| 199 |
+
# # Return the path to the saved WAV file
|
| 200 |
+
# return tmp_file.name
|
| 201 |
+
|
| 202 |
+
# # Function to save data to the Hugging Face dataset
|
| 203 |
+
# def save_to_hf_dataset(text, audio_path):
|
| 204 |
+
# with open(audio_path, "rb") as f:
|
| 205 |
+
# audio_data = f.read()
|
| 206 |
+
# hf_writer.save({"text": text, "audio": audio_data})
|
| 207 |
+
|
| 208 |
+
# # Gradio interface function
|
| 209 |
+
# def audio_capture_interface():
|
| 210 |
+
# global file_index, line_index, lines
|
| 211 |
+
|
| 212 |
+
# # Initial file to read
|
| 213 |
+
# files = os.listdir('./audio_samples')
|
| 214 |
+
# read_lines_from_file(os.path.join('./audio_samples', files[file_index]))
|
| 215 |
+
|
| 216 |
+
# # Define the interface components
|
| 217 |
+
# audio_input = gr.Audio(source="microphone", type="numpy", label="Speak and click submit")
|
| 218 |
+
# output_text = gr.Textbox(label="Status", placeholder="Status will appear here")
|
| 219 |
+
|
| 220 |
+
# # Function to capture and process the audio input
|
| 221 |
+
# def process_audio(audio):
|
| 222 |
+
# global line_index, lines
|
| 223 |
+
|
| 224 |
+
# try:
|
| 225 |
+
# text_line = lines[line_index].strip()
|
| 226 |
+
# file_path = save_audio_to_file(audio)
|
| 227 |
+
# save_to_hf_dataset(text_line, file_path)
|
| 228 |
+
# return f"Audio saved to {file_path} and uploaded to Hugging Face Dataset."
|
| 229 |
+
# except Exception as e:
|
| 230 |
+
# return f"Error saving audio: {str(e)}"
|
| 231 |
+
|
| 232 |
+
# # Function to handle navigation buttons
|
| 233 |
+
# def navigate_lines(button):
|
| 234 |
+
# global line_index, lines
|
| 235 |
+
|
| 236 |
+
# if button == 'forward':
|
| 237 |
+
# line_index = min(line_index + 1, len(lines) - 1)
|
| 238 |
+
# elif button == 'previous':
|
| 239 |
+
# line_index = max(line_index - 1, 0)
|
| 240 |
+
|
| 241 |
+
# output_text.value = lines[line_index]
|
| 242 |
+
|
| 243 |
+
# # Create the Gradio interface
|
| 244 |
+
# with gr.Blocks() as iface:
|
| 245 |
+
# with gr.Row():
|
| 246 |
+
# gr.Textbox(label="Text", value=lines[line_index], interactive=False)
|
| 247 |
+
# with gr.Row():
|
| 248 |
+
# audio_input.render()
|
| 249 |
+
# with gr.Row():
|
| 250 |
+
# gr.Button("Previous").click(lambda: navigate_lines('previous'), outputs=output_text)
|
| 251 |
+
# gr.Button("Forward").click(lambda: navigate_lines('forward'), outputs=output_text)
|
| 252 |
+
# gr.Button("Submit").click(process_audio, inputs=audio_input, outputs=output_text)
|
| 253 |
+
|
| 254 |
+
# return iface
|
| 255 |
+
|
| 256 |
+
# # Launch the interface
|
| 257 |
+
# iface = audio_capture_interface()
|
| 258 |
+
# iface.launch()
|
| 259 |
+
|
| 260 |
import gradio as gr
|
| 261 |
import os
|
| 262 |
import wave
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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 |
+
text_display = gr.Textbox(label="Text", value=lines[line_index].strip(), interactive=False)
|
| 347 |
with gr.Row():
|
| 348 |
audio_input.render()
|
| 349 |
with gr.Row():
|
| 350 |
+
gr.Button("Previous").click(lambda: navigate_lines('previous'), None, text_display)
|
| 351 |
+
gr.Button("Forward").click(lambda: navigate_lines('forward'), None, text_display)
|
| 352 |
gr.Button("Submit").click(process_audio, inputs=audio_input, outputs=output_text)
|
| 353 |
|
| 354 |
return iface
|
|
|
|
| 356 |
# Launch the interface
|
| 357 |
iface = audio_capture_interface()
|
| 358 |
iface.launch()
|
|
|