Spaces:
Sleeping
Sleeping
Update helpers.py
Browse files- helpers.py +17 -24
helpers.py
CHANGED
@@ -3,8 +3,6 @@ import streamlit as st
|
|
3 |
import os
|
4 |
import openai
|
5 |
from dotenv import load_dotenv
|
6 |
-
from gtts import gTTS
|
7 |
-
import textwrap
|
8 |
|
9 |
# Function to accept OpenAI API Key as input from the user
|
10 |
def get_api_key():
|
@@ -25,29 +23,24 @@ def speech_to_text(audio_data):
|
|
25 |
)
|
26 |
return transcript["text"] # Extract the text from the response
|
27 |
|
|
|
|
|
|
|
28 |
def text_to_speech(input_text):
|
29 |
"""Generates a TTS audio file from the input text."""
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
for i, chunk in enumerate(chunks):
|
35 |
-
tts = gTTS(text=chunk, lang="en")
|
36 |
-
audio_file_path = f"temp_audio_play_{i}.mp3"
|
37 |
-
tts.save(audio_file_path)
|
38 |
-
audio_file_paths.append(audio_file_path)
|
39 |
|
40 |
-
return audio_file_paths # Return a list of file paths
|
41 |
|
42 |
-
def autoplay_audio(
|
43 |
-
""
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
"""
|
53 |
-
st.markdown(md, unsafe_allow_html=True)
|
|
|
3 |
import os
|
4 |
import openai
|
5 |
from dotenv import load_dotenv
|
|
|
|
|
6 |
|
7 |
# Function to accept OpenAI API Key as input from the user
|
8 |
def get_api_key():
|
|
|
23 |
)
|
24 |
return transcript["text"] # Extract the text from the response
|
25 |
|
26 |
+
|
27 |
+
from gtts import gTTS
|
28 |
+
|
29 |
def text_to_speech(input_text):
|
30 |
"""Generates a TTS audio file from the input text."""
|
31 |
+
tts = gTTS(text=input_text, lang="en")
|
32 |
+
audio_file_path = "temp_audio_play.mp3"
|
33 |
+
tts.save(audio_file_path)
|
34 |
+
return audio_file_path
|
|
|
|
|
|
|
|
|
|
|
35 |
|
|
|
36 |
|
37 |
+
def autoplay_audio(file_path: str):
|
38 |
+
with open(file_path, "rb") as f:
|
39 |
+
data = f.read()
|
40 |
+
b64 = base64.b64encode(data).decode("utf-8")
|
41 |
+
md = f"""
|
42 |
+
<audio autoplay>
|
43 |
+
<source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
|
44 |
+
</audio>
|
45 |
+
"""
|
46 |
+
st.markdown(md, unsafe_allow_html=True)
|
|
|
|