Sajjo commited on
Commit
125809c
·
verified ·
1 Parent(s): 6261f79

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +103 -81
app.py CHANGED
@@ -1,91 +1,113 @@
1
- import gradio as gr
2
- import os
3
-
4
- HF_TOKEN = os.getenv('HW_Token')
5
- hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "save_audio")
6
-
7
- import gradio as gr
8
- import os
9
-
10
- class TextFileReader:
11
- def __init__(self):
12
- self.lines = []
13
- self.current_index = 0
14
-
15
- def read_lines(self, file):
16
- self.lines = file.decode('utf-8').splitlines()
17
- self.current_index = 0
18
- return self.get_current_line()
19
-
20
- def get_current_line(self):
21
- if 0 <= self.current_index < len(self.lines):
22
- return self.lines[self.current_index]
23
- else:
24
- return "End of file reached."
25
-
26
- def forward_line(self):
27
- self.current_index = min(self.current_index + 1, len(self.lines) - 1)
28
- return self.get_current_line()
29
-
30
- def backward_line(self):
31
- self.current_index = max(self.current_index - 1, 0)
32
- return self.get_current_line()
33
-
34
- reader = TextFileReader()
35
-
36
- # Define a function to save the text lines to a file
37
- def save_text_lines(file):
38
- lines = reader.read_lines(file)
39
- with open("text_lines.txt", "w") as f:
40
- f.write("\n".join(reader.lines))
41
- return lines
42
-
43
- # Define a function to save the audio file and corresponding text
44
- def save_audio_text(audio, text):
45
- if not os.path.exists("recordings"):
46
- os.makedirs("/recordings")
47
-
48
- # Debugging to print out the structure of the audio variable
49
- print("Received audio data:", audio)
50
-
51
- # Check if audio is a dictionary and contains 'data'
52
- if isinstance(audio, dict) and 'data' in audio:
53
- audio_data = audio['data']
54
- audio_path = f"/recordings/line_{reader.current_index}.wav"
55
- text_path = f"/recordings/line_{reader.current_index}.txt"
56
 
57
- with open(audio_path, "wb") as f:
58
- f.write(audio_data)
59
 
60
- with open(text_path, "w") as f:
61
- f.write(text)
62
 
63
- # Move to the next line after saving
64
- next_line = reader.forward_line()
65
- return next_line
66
- else:
67
- return "Audio data is not in the expected format."
68
-
69
- # Define the Gradio interface
70
- with gr.Blocks() as demo:
71
- with gr.Row():
72
- file_upload = gr.File(label="Upload a text file", type="binary")
73
- generate_button = gr.Button("Generate Lines")
74
 
75
- current_line = gr.Textbox(label="Current Line")
76
 
77
- def update_output(file):
78
- lines = reader.read_lines(file)
79
- save_text_lines(file) # Save the text lines to a file
80
- return lines
81
 
82
- generate_button.click(fn=update_output, inputs=file_upload, outputs=current_line)
83
 
84
- with gr.Row():
85
- audio_record = gr.Audio(sources=["microphone","upload"], type="filepath")
86
- save_button = gr.Button("Save Audio and Next Line")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
- save_button.click(fn=save_audio_text, inputs=[audio_record, current_line], outputs=current_line)
89
 
90
- demo.launch()
 
 
 
 
 
91
 
 
 
1
+ # import gradio as gr
2
+ # import os
3
+
4
+ # HF_TOKEN = os.getenv('HW_Token')
5
+ # hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "save_audio")
6
+
7
+ # import gradio as gr
8
+ # import os
9
+
10
+ # class TextFileReader:
11
+ # def __init__(self):
12
+ # self.lines = []
13
+ # self.current_index = 0
14
+
15
+ # def read_lines(self, file):
16
+ # self.lines = file.decode('utf-8').splitlines()
17
+ # self.current_index = 0
18
+ # return self.get_current_line()
19
+
20
+ # def get_current_line(self):
21
+ # if 0 <= self.current_index < len(self.lines):
22
+ # return self.lines[self.current_index]
23
+ # else:
24
+ # return "End of file reached."
25
+
26
+ # def forward_line(self):
27
+ # self.current_index = min(self.current_index + 1, len(self.lines) - 1)
28
+ # return self.get_current_line()
29
+
30
+ # def backward_line(self):
31
+ # self.current_index = max(self.current_index - 1, 0)
32
+ # return self.get_current_line()
33
+
34
+ # reader = TextFileReader()
35
+
36
+ # # Define a function to save the text lines to a file
37
+ # def save_text_lines(file):
38
+ # lines = reader.read_lines(file)
39
+ # with open("text_lines.txt", "w") as f:
40
+ # f.write("\n".join(reader.lines))
41
+ # return lines
42
+
43
+ # # Define a function to save the audio file and corresponding text
44
+ # def save_audio_text(audio, text):
45
+ # if not os.path.exists("recordings"):
46
+ # os.makedirs("/recordings")
47
+
48
+ # # Debugging to print out the structure of the audio variable
49
+ # print("Received audio data:", audio)
50
+
51
+ # # Check if audio is a dictionary and contains 'data'
52
+ # if isinstance(audio, dict) and 'data' in audio:
53
+ # audio_data = audio['data']
54
+ # audio_path = f"/recordings/line_{reader.current_index}.wav"
55
+ # text_path = f"/recordings/line_{reader.current_index}.txt"
56
 
57
+ # with open(audio_path, "wb") as f:
58
+ # f.write(audio_data)
59
 
60
+ # with open(text_path, "w") as f:
61
+ # f.write(text)
62
 
63
+ # # Move to the next line after saving
64
+ # next_line = reader.forward_line()
65
+ # return next_line
66
+ # else:
67
+ # return "Audio data is not in the expected format."
68
+
69
+ # # Define the Gradio interface
70
+ # with gr.Blocks() as demo:
71
+ # with gr.Row():
72
+ # file_upload = gr.File(label="Upload a text file", type="binary")
73
+ # generate_button = gr.Button("Generate Lines")
74
 
75
+ # current_line = gr.Textbox(label="Current Line")
76
 
77
+ # def update_output(file):
78
+ # lines = reader.read_lines(file)
79
+ # save_text_lines(file) # Save the text lines to a file
80
+ # return lines
81
 
82
+ # generate_button.click(fn=update_output, inputs=file_upload, outputs=current_line)
83
 
84
+ # with gr.Row():
85
+ # audio_record = gr.Audio(sources=["microphone","upload"], type="filepath")
86
+ # save_button = gr.Button("Save Audio and Next Line")
87
+
88
+ # save_button.click(fn=save_audio_text, inputs=[audio_record, current_line], outputs=current_line)
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
+ )
112
 
113
+ iface.launch()