Eldeeb commited on
Commit
64e5450
·
verified ·
1 Parent(s): b534e1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -16
app.py CHANGED
@@ -1,11 +1,9 @@
1
- # streamlit_app.py
2
  import streamlit as st
3
  from transformers import pipeline
 
4
  import numpy as np
5
- import tempfile
6
  import soundfile as sf
7
- import os
8
- import io
9
 
10
  # Caching the text-to-speech model
11
  @st.cache_resource
@@ -21,17 +19,24 @@ if 'conversation_history' not in st.session_state:
21
  if 'tts_audio' not in st.session_state:
22
  st.session_state.tts_audio = None
23
 
 
 
 
 
 
 
 
 
24
  def convert_text_to_speech(text):
 
25
  # Generate speech from text
26
- audio = tts_pipe(text)
27
  return audio
28
 
29
- def save_audio(audio):
30
- # Save the audio to a temporary file
31
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.wav')
32
- with open(temp_file.name, 'wb') as f:
33
- f.write(audio['audio'])
34
- return temp_file.name
35
 
36
  # Sidebar options
37
  st.sidebar.title("App Settings")
@@ -51,14 +56,11 @@ if feature == "Text-to-Speech":
51
  if user_message:
52
  # Convert text to speech
53
  tts_audio = convert_text_to_speech(user_message)
54
- audio_file = save_audio(tts_audio)
55
 
56
  # Display the audio player
57
- st.audio(audio_file, format='audio/wav')
58
  st.success("Conversion successful!")
59
-
60
- # Clean up temporary file
61
- os.remove(audio_file)
62
  else:
63
  st.warning("Please enter text before converting.")
64
 
 
 
1
  import streamlit as st
2
  from transformers import pipeline
3
+ import io
4
  import numpy as np
 
5
  import soundfile as sf
6
+ import requests
 
7
 
8
  # Caching the text-to-speech model
9
  @st.cache_resource
 
19
  if 'tts_audio' not in st.session_state:
20
  st.session_state.tts_audio = None
21
 
22
+ # Example function to obtain speaker embeddings
23
+ def get_speaker_embeddings():
24
+ # Placeholder: Replace with actual code to obtain embeddings
25
+ url = "https://huggingface.co/datasets/Matthijs/cmu-arctic-xvectors/resolve/main/xvectors.npy"
26
+ response = requests.get(url)
27
+ speaker_embeddings = np.load(io.BytesIO(response.content))
28
+ return speaker_embeddings
29
+
30
  def convert_text_to_speech(text):
31
+ speaker_embeddings = get_speaker_embeddings() # Obtain speaker embeddings
32
  # Generate speech from text
33
+ audio = tts_pipe(text, speaker_embeddings=speaker_embeddings)
34
  return audio
35
 
36
+ def convert_audio_to_bytes(audio):
37
+ # Convert audio data to bytes
38
+ audio_buffer = io.BytesIO(audio['audio'])
39
+ return audio_buffer
 
 
40
 
41
  # Sidebar options
42
  st.sidebar.title("App Settings")
 
56
  if user_message:
57
  # Convert text to speech
58
  tts_audio = convert_text_to_speech(user_message)
59
+ audio_bytes = convert_audio_to_bytes(tts_audio)
60
 
61
  # Display the audio player
62
+ st.audio(audio_bytes, format='audio/wav')
63
  st.success("Conversion successful!")
 
 
 
64
  else:
65
  st.warning("Please enter text before converting.")
66