Sajjo commited on
Commit
762fc8d
·
verified ·
1 Parent(s): cfc0ac5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -79
app.py CHANGED
@@ -1,89 +1,112 @@
1
- import gradio as gr
2
- import os
3
- import wave
4
- import tempfile
5
- import numpy as np
6
-
7
- # Global variables to store file and line index
8
- file_index = 0
9
- line_index = 0
10
- lines = []
11
-
12
- # Function to read lines from a file
13
- def read_lines_from_file(file_path):
14
- global lines
15
- with open(file_path, 'r') as file:
16
- lines = file.readlines()
17
-
18
- # Function to save audio to a WAV file
19
- def save_audio_to_file(audio):
20
- global file_index, line_index, lines
21
 
22
- sample_rate, data = audio # audio is a tuple (sample_rate, data)
23
 
24
- # Save the audio data as a WAV file in a temporary location
25
- with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
26
- with wave.open(tmp_file.name, 'wb') as wav_file:
27
- wav_file.setnchannels(1) # Mono audio
28
- wav_file.setsampwidth(2) # 2 bytes per sample (16-bit PCM)
29
- wav_file.setframerate(sample_rate)
30
- wav_file.writeframes(data.tobytes())
31
 
32
- # Return the path to the saved WAV file
33
- return tmp_file.name
34
 
35
- # Gradio interface function
36
- def audio_capture_interface():
37
- global file_index, line_index, lines
38
 
39
- # Initial file to read
40
- files = os.listdir('./audio_samples')
41
- read_lines_from_file(os.path.join('./audio_samples', files[file_index]))
42
 
43
- # Define the interface components
44
- audio_input = gr.Audio(source="microphone", type="numpy", label="Speak and click submit")
45
- output_text = gr.Textbox(label="Status", placeholder="Status will appear here")
46
 
47
- # Function to capture and process the audio input
48
- def process_audio(audio):
49
- global line_index, lines
50
 
51
- try:
52
- file_path = save_audio_to_file(audio)
53
- return f"Audio saved to {file_path}"
54
- except Exception as e:
55
- return f"Error saving audio: {str(e)}"
56
-
57
- # Function to handle navigation buttons
58
- def navigate_lines(button):
59
- global line_index, lines
60
 
61
- if button == 'forward':
62
- line_index = min(line_index + 1, len(lines) - 1)
63
- elif button == 'previous':
64
- line_index = max(line_index - 1, 0)
65
 
66
- output_text.value = lines[line_index]
67
-
68
- # Create the Gradio interface
69
- iface = gr.Interface(
70
- fn=process_audio,
71
- inputs=audio_input,
72
- outputs=output_text,
73
- live=True,
74
- title="Audio Capture and Playback",
75
- description="Speak into the microphone and click submit to save audio. Navigate through lines using buttons below.",
76
- theme="compact",
77
- layout="vertical",
78
- examples=[["Start recording audio."]]
79
- )
80
-
81
- # Add navigation buttons
82
- iface.add_button("Previous", lambda: navigate_lines('previous'))
83
- iface.add_button("Forward", lambda: navigate_lines('forward'))
84
-
85
- return iface
86
-
87
- # Launch the interface
88
- iface = audio_capture_interface()
89
- iface.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import gradio as gr
2
+ # import os
3
+ # import wave
4
+ # import tempfile
5
+ # import numpy as np
6
+
7
+ # # Global variables to store file and line index
8
+ # file_index = 0
9
+ # line_index = 0
10
+ # lines = []
11
+
12
+ # # Function to read lines from a file
13
+ # def read_lines_from_file(file_path):
14
+ # global lines
15
+ # with open(file_path, 'r') as file:
16
+ # lines = file.readlines()
17
+
18
+ # # Function to save audio to a WAV file
19
+ # def save_audio_to_file(audio):
20
+ # global file_index, line_index, lines
21
 
22
+ # sample_rate, data = audio # audio is a tuple (sample_rate, data)
23
 
24
+ # # Save the audio data as a WAV file in a temporary location
25
+ # with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as tmp_file:
26
+ # with wave.open(tmp_file.name, 'wb') as wav_file:
27
+ # wav_file.setnchannels(1) # Mono audio
28
+ # wav_file.setsampwidth(2) # 2 bytes per sample (16-bit PCM)
29
+ # wav_file.setframerate(sample_rate)
30
+ # wav_file.writeframes(data.tobytes())
31
 
32
+ # # Return the path to the saved WAV file
33
+ # return tmp_file.name
34
 
35
+ # # Gradio interface function
36
+ # def audio_capture_interface():
37
+ # global file_index, line_index, lines
38
 
39
+ # # Initial file to read
40
+ # files = os.listdir('./audio_samples')
41
+ # read_lines_from_file(os.path.join('./audio_samples', files[file_index]))
42
 
43
+ # # Define the interface components
44
+ # audio_input = gr.Audio(source="microphone", type="numpy", label="Speak and click submit")
45
+ # output_text = gr.Textbox(label="Status", placeholder="Status will appear here")
46
 
47
+ # # Function to capture and process the audio input
48
+ # def process_audio(audio):
49
+ # global line_index, lines
50
 
51
+ # try:
52
+ # file_path = save_audio_to_file(audio)
53
+ # return f"Audio saved to {file_path}"
54
+ # except Exception as e:
55
+ # return f"Error saving audio: {str(e)}"
56
+
57
+ # # Function to handle navigation buttons
58
+ # def navigate_lines(button):
59
+ # global line_index, lines
60
 
61
+ # if button == 'forward':
62
+ # line_index = min(line_index + 1, len(lines) - 1)
63
+ # elif button == 'previous':
64
+ # line_index = max(line_index - 1, 0)
65
 
66
+ # output_text.value = lines[line_index]
67
+
68
+ # # Create the Gradio interface
69
+ # iface = gr.Interface(
70
+ # fn=process_audio,
71
+ # inputs=audio_input,
72
+ # outputs=output_text,
73
+ # live=True,
74
+ # title="Audio Capture and Playback",
75
+ # description="Speak into the microphone and click submit to save audio. Navigate through lines using buttons below.",
76
+ # theme="compact",
77
+ # layout="vertical",
78
+ # examples=[["Start recording audio."]]
79
+ # )
80
+
81
+ # # Add navigation buttons
82
+ # iface.add_button("Previous", lambda: navigate_lines('previous'))
83
+ # iface.add_button("Forward", lambda: navigate_lines('forward'))
84
+
85
+ # return iface
86
+
87
+ # # Launch the interface
88
+ # iface = audio_capture_interface()
89
+ # iface.launch()
90
+
91
+ import gradio as gr
92
+
93
+
94
+ def calculator(num1, operation, num2):
95
+ if operation == "add":
96
+ return num1 + num2
97
+ elif operation == "subtract":
98
+ return num1 - num2
99
+ elif operation == "multiply":
100
+ return num1 * num2
101
+ elif operation == "divide":
102
+ return num1 / num2
103
+
104
+
105
+ iface = gr.Interface(
106
+ calculator,
107
+ ["number", gr.Radio(["add", "subtract", "multiply", "divide"]), "number"],
108
+ "number",
109
+ allow_flagging="manual"
110
+ )
111
+
112
+ iface.launch()