BHW commited on
Commit
8677091
·
verified ·
1 Parent(s): 8b1b4fb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -6,10 +6,11 @@ import hashlib
6
 
7
 
8
  MAX_LENGTH = 1.5 # in seconds
9
- LABLES = ["Hi, doctor", "docker home", "Ask doctor", "Hey, daughter", "Hi, gemtek", "Hi, AI"]
 
 
 
10
 
11
- # Initialize a list to store file paths
12
- uploaded_files = []
13
  help_en = f"""
14
  ### Usage Instructions:
15
  1. **Select a Label**: Choose a label from the dropdown menu, or select "Custom" to enter your own label.
@@ -39,6 +40,9 @@ def save_audio(audio, dropdown_label, custom_label, speaker_name):
39
  if not speaker_name:
40
  raise gr.Error("User name cannot be empty 💥!", duration=5)
41
 
 
 
 
42
  # Get sample rate and audio data from Gradio
43
  sample_rate = audio[0]
44
  audio_data = np.array(audio[1])
@@ -50,26 +54,21 @@ def save_audio(audio, dropdown_label, custom_label, speaker_name):
50
  if audio_length > MAX_LENGTH:
51
  raise gr.Error(f"Recording is longer than {MAX_LENGTH} second 💥!", duration=5)
52
 
53
- # Generate speaker_id using a hash function to ensure consistency
54
- speaker_id = hashlib.sha256(speaker_name.encode()).hexdigest()[:8]
55
-
56
  # Generate file name
57
  filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{speaker_id}_{label}.wav"
58
 
59
  # Save the audio file in wav format
60
  sf.write(filename, audio_data, sample_rate)
61
 
62
- # Add the new file path to the list of uploaded files
63
- uploaded_files.append(filename)
 
 
64
 
65
  # Create an info message
66
  info_message = f"Recorded audio length: {audio_length:.2f} seconds."
67
 
68
- # Display the info message using gr.Info
69
- gr.Info(info_message, duration=5)
70
-
71
- # Return the list of all file paths for download
72
- return uploaded_files
73
 
74
  # Interface design using gr.Blocks
75
  def create_interface():
@@ -95,16 +94,23 @@ def create_interface():
95
  # Create a list to display all uploaded files
96
  file_list = gr.Files(label="Download your recordings")
97
 
98
- # Create an info display area
99
- info_display = gr.Info()
100
 
101
  # Add the info output to display recording length
102
  submit_button.click(
103
  fn=save_audio,
104
  inputs=[audio, label_dropdown, custom_label, speaker_name],
105
- outputs=[file_list],
106
  )
107
 
 
 
 
 
 
 
 
108
  # Add usage instructions in both English and Chinese
109
  gr.Markdown(help_en)
110
  gr.Markdown(help_zh_tw)
 
6
 
7
 
8
  MAX_LENGTH = 1.5 # in seconds
9
+ LABLES = ["Hi, doctor", "doctor home", "Ask doctor", "Hey, daughter", "Hi, gemtek", "Hi, AI", "Custom"]
10
+
11
+ # Initialize a dictionary to store file paths by speaker_id
12
+ uploaded_files = {}
13
 
 
 
14
  help_en = f"""
15
  ### Usage Instructions:
16
  1. **Select a Label**: Choose a label from the dropdown menu, or select "Custom" to enter your own label.
 
40
  if not speaker_name:
41
  raise gr.Error("User name cannot be empty 💥!", duration=5)
42
 
43
+ # Generate speaker_id using a hash function to ensure consistency
44
+ speaker_id = hashlib.sha256(speaker_name.encode()).hexdigest()[:8]
45
+
46
  # Get sample rate and audio data from Gradio
47
  sample_rate = audio[0]
48
  audio_data = np.array(audio[1])
 
54
  if audio_length > MAX_LENGTH:
55
  raise gr.Error(f"Recording is longer than {MAX_LENGTH} second 💥!", duration=5)
56
 
 
 
 
57
  # Generate file name
58
  filename = f"{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}_{speaker_id}_{label}.wav"
59
 
60
  # Save the audio file in wav format
61
  sf.write(filename, audio_data, sample_rate)
62
 
63
+ # Add the new file path to the list of uploaded files by speaker_id
64
+ if speaker_id not in uploaded_files:
65
+ uploaded_files[speaker_id] = []
66
+ uploaded_files[speaker_id].append(filename)
67
 
68
  # Create an info message
69
  info_message = f"Recorded audio length: {audio_length:.2f} seconds."
70
 
71
+ return uploaded_files[speaker_id], info_message
 
 
 
 
72
 
73
  # Interface design using gr.Blocks
74
  def create_interface():
 
94
  # Create a list to display all uploaded files
95
  file_list = gr.Files(label="Download your recordings")
96
 
97
+ # Create an info display area for feedback messages
98
+ info_display = gr.Markdown()
99
 
100
  # Add the info output to display recording length
101
  submit_button.click(
102
  fn=save_audio,
103
  inputs=[audio, label_dropdown, custom_label, speaker_name],
104
+ outputs=[file_list, info_display],
105
  )
106
 
107
+ # Update file list based on the current speaker
108
+ def get_user_files(speaker_name):
109
+ speaker_id = hashlib.sha256(speaker_name.encode()).hexdigest()[:8]
110
+ return uploaded_files.get(speaker_id, [])
111
+
112
+ speaker_name.submit(fn=get_user_files, inputs=speaker_name, outputs=file_list)
113
+
114
  # Add usage instructions in both English and Chinese
115
  gr.Markdown(help_en)
116
  gr.Markdown(help_zh_tw)