nguyenbh commited on
Commit
a9c5d3b
·
verified ·
1 Parent(s): c2df150

Load sample from url

Browse files
Files changed (1) hide show
  1. app.py +48 -28
app.py CHANGED
@@ -5,11 +5,13 @@ import ssl
5
  import base64
6
  import urllib.request
7
  import tempfile
8
- import soundfile
 
9
  from datetime import datetime
10
  from typing import Dict, List, Optional, Tuple
11
  import edge_tts
12
  from langdetect import detect
 
13
 
14
 
15
  # Custom CSS for better styling
@@ -185,24 +187,46 @@ Aqui estão os pensamentos:""",
185
  }
186
  }
187
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  class VoiceNotesApp:
189
  def __init__(self):
190
  # Azure endpoint configuration
191
- # self.azure_url = os.getenv("AZURE_ENDPOINT")
192
- # self.api_key = os.getenv("AZURE_API_KEY")
193
- self.azure_url = "https://nguyenbach-9897-westus3-covns.westus3.inference.ml.azure.com/score"
194
- self.api_key = "uABZxEGiK6iXlBWsvy7xOEJ1zCmeLkvSHuf6wEykfqtf4I7KbbWiJQQJ99BBAAAAAAAAAAAAINFRAZML2JhK"
195
 
196
- # Initialize sample audio files
197
  self.sample_audios = {
198
- "English - Weekend Plan": "content/english.weekend.plan.wav",
199
- "Chinese - Kids & Work": "content/chinese.kid.work.mp3",
200
- "German - Vacation Planning": "content/german.vacation.work.mp3",
201
- "French - Random Thoughts": "content/french.random.vacation.mp3",
202
- "Italian - Daily Life": "content/italian.daily.life.mp3",
203
- "Japanese - Seattle Trip Report": "content/japanese.seattle.trip.report.mp3",
204
- "Spanish - Soccer Class": "content/spanish.soccer.class.mp3",
205
- "Portuguese - Buying House & Friends": "content/portugese.house.friends.mp3"
206
  }
207
 
208
  # Initialize storage
@@ -534,16 +558,9 @@ class VoiceNotesApp:
534
 
535
  def create_interface(self):
536
  """Create an enhanced Gradio interface with improved styling and layout"""
537
- # Preload English sample
538
  default_audio_path = self.sample_audios["English - Weekend Plan"]
539
- # Read the audio file for proper initialization
540
- try:
541
- audio_data, sample_rate = soundfile.read(default_audio_path)
542
- default_audio = (sample_rate, audio_data)
543
- except Exception as e:
544
- print(f"Error loading default audio: {e}")
545
- default_audio = None
546
-
547
  # Create the layout with tabs
548
  with gr.Blocks(
549
  css=CUSTOM_CSS,
@@ -573,8 +590,7 @@ class VoiceNotesApp:
573
  type="filepath",
574
  label="",
575
  interactive=True,
576
- show_download_button=True,
577
- value=default_audio
578
  )
579
 
580
  with gr.Row():
@@ -622,9 +638,13 @@ class VoiceNotesApp:
622
  if not sample_name:
623
  return None
624
  try:
625
- audio_path = self.sample_audios[sample_name]
626
- audio_data, sample_rate = soundfile.read(audio_path)
627
- return (sample_rate, audio_data)
 
 
 
 
628
  except Exception as e:
629
  print(f"Error loading sample audio: {e}")
630
  return None
 
5
  import base64
6
  import urllib.request
7
  import tempfile
8
+ import requests
9
+ import soundfile as sf
10
  from datetime import datetime
11
  from typing import Dict, List, Optional, Tuple
12
  import edge_tts
13
  from langdetect import detect
14
+ from io import BytesIO
15
 
16
 
17
  # Custom CSS for better styling
 
187
  }
188
  }
189
 
190
+
191
+ def load_audio_from_url(url):
192
+ """
193
+ Load audio from a URL using soundfile
194
+
195
+ Args:
196
+ url (str): URL of the audio file
197
+
198
+ Returns:
199
+ tuple: (audio_data, sample_rate) if successful, None otherwise
200
+ """
201
+ try:
202
+ # Get the audio file from the URL
203
+ response = requests.get(url)
204
+ response.raise_for_status() # Raise exception for bad status codes
205
+
206
+ # For other formats that soundfile supports directly (WAV, FLAC, etc.)
207
+ audio_data, sample_rate = sf.read(BytesIO(response.content))
208
+ return sample_rate, audio_data
209
+
210
+ except Exception as e:
211
+ print(f"Error loading audio from URL: {e}")
212
+ return None
213
+
214
  class VoiceNotesApp:
215
  def __init__(self):
216
  # Azure endpoint configuration
217
+ self.azure_url = os.getenv("AZURE_ENDPOINT")
218
+ self.api_key = os.getenv("AZURE_API_KEY")
 
 
219
 
220
+ # Initialize sample audio files - all using HTTPS URLs
221
  self.sample_audios = {
222
+ "English - Weekend Plan": "https://diamondfan.github.io/audio_files/english.weekend.plan.wav",
223
+ "Chinese - Kids & Work": "https://diamondfan.github.io/audio_files/chinese.kid.work.wav",
224
+ "German - Vacation Planning": "https://diamondfan.github.io/audio_files/german.vacation.work.wav",
225
+ "French - Random Thoughts": "https://diamondfan.github.io/audio_files/french.random.vacation.wav",
226
+ "Italian - Daily Life": "https://diamondfan.github.io/audio_files/italian.daily.life.wav",
227
+ "Japanese - Seattle Trip Report": "https://diamondfan.github.io/audio_files/japanese.seattle.trip.report.wav",
228
+ "Spanish - Soccer Class": "https://diamondfan.github.io/audio_files/spanish.soccer.class.wav",
229
+ "Portuguese - Buying House & Friends": "https://diamondfan.github.io/audio_files/portugese.house.friends.wav"
230
  }
231
 
232
  # Initialize storage
 
558
 
559
  def create_interface(self):
560
  """Create an enhanced Gradio interface with improved styling and layout"""
561
+ # Preload Chinese sample as default
562
  default_audio_path = self.sample_audios["English - Weekend Plan"]
563
+
 
 
 
 
 
 
 
564
  # Create the layout with tabs
565
  with gr.Blocks(
566
  css=CUSTOM_CSS,
 
590
  type="filepath",
591
  label="",
592
  interactive=True,
593
+ show_download_button=True
 
594
  )
595
 
596
  with gr.Row():
 
638
  if not sample_name:
639
  return None
640
  try:
641
+ audio_url = self.sample_audios[sample_name]
642
+ # Use the load_audio_from_url function to fetch the audio
643
+ result = load_audio_from_url(audio_url)
644
+ if result:
645
+ sample_rate, audio_data = result
646
+ return (sample_rate, audio_data)
647
+ return None
648
  except Exception as e:
649
  print(f"Error loading sample audio: {e}")
650
  return None