Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -89,46 +89,46 @@
|
|
89 |
|
90 |
# demo.launch()
|
91 |
|
92 |
-
import gradio as gr
|
93 |
|
94 |
|
95 |
-
def calculator(num1, operation, num2):
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
|
105 |
|
106 |
-
iface = gr.Interface(
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
)
|
113 |
|
114 |
-
iface.launch()
|
115 |
|
116 |
-
import os
|
117 |
|
118 |
-
HF_TOKEN = os.getenv('HF_TOKEN')
|
119 |
-
hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced")
|
120 |
-
|
121 |
-
iface = gr.Interface(
|
122 |
-
calculator,
|
123 |
-
["number", gr.Radio(["add", "subtract", "multiply", "divide"]), "number"],
|
124 |
-
"number",
|
125 |
-
description="Check out the crowd-sourced dataset at: [https://huggingface.co/Sajjo/crowdsourced](https://huggingface.co/Sajjo/crowdsourced)",
|
126 |
-
allow_flagging="manual",
|
127 |
-
flagging_options=["wrong sign", "off by one", "other"],
|
128 |
-
flagging_callback=hf_writer
|
129 |
-
)
|
130 |
|
131 |
-
iface.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
132 |
|
133 |
# import numpy as np
|
134 |
# import gradio as gr
|
@@ -162,3 +162,98 @@ iface.launch()
|
|
162 |
# btn.click(lambda *args: callback.flag(args), [img_input, strength, img_output], None, preprocess=False)
|
163 |
|
164 |
# demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
|
90 |
# demo.launch()
|
91 |
|
92 |
+
# import gradio as gr
|
93 |
|
94 |
|
95 |
+
# def calculator(num1, operation, num2):
|
96 |
+
# if operation == "add":
|
97 |
+
# return num1 + num2
|
98 |
+
# elif operation == "subtract":
|
99 |
+
# return num1 - num2
|
100 |
+
# elif operation == "multiply":
|
101 |
+
# return num1 * num2
|
102 |
+
# elif operation == "divide":
|
103 |
+
# return num1 / num2
|
104 |
|
105 |
|
106 |
+
# iface = gr.Interface(
|
107 |
+
# calculator,
|
108 |
+
# ["number", gr.Radio(["add", "subtract", "multiply", "divide"]), "number"],
|
109 |
+
# "number",
|
110 |
+
# allow_flagging="manual",
|
111 |
+
# flagging_options=["correct", "wrong"]
|
112 |
+
# )
|
113 |
|
114 |
+
# iface.launch()
|
115 |
|
116 |
+
# import os
|
117 |
|
118 |
+
# HF_TOKEN = os.getenv('HF_TOKEN')
|
119 |
+
# hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "crowdsourced")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
+
# iface = gr.Interface(
|
122 |
+
# calculator,
|
123 |
+
# ["number", gr.Radio(["add", "subtract", "multiply", "divide"]), "number"],
|
124 |
+
# "number",
|
125 |
+
# description="Check out the crowd-sourced dataset at: [https://huggingface.co/Sajjo/crowdsourced](https://huggingface.co/Sajjo/crowdsourced)",
|
126 |
+
# allow_flagging="manual",
|
127 |
+
# flagging_options=["wrong sign", "off by one", "other"],
|
128 |
+
# flagging_callback=hf_writer
|
129 |
+
# )
|
130 |
+
|
131 |
+
# iface.launch()
|
132 |
|
133 |
# import numpy as np
|
134 |
# import gradio as gr
|
|
|
162 |
# btn.click(lambda *args: callback.flag(args), [img_input, strength, img_output], None, preprocess=False)
|
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 |
+
|