shukdevdatta123 commited on
Commit
7a38f58
·
verified ·
1 Parent(s): d8ae64a

Update helpers.py

Browse files
Files changed (1) hide show
  1. helpers.py +26 -21
helpers.py CHANGED
@@ -1,10 +1,10 @@
1
- # helpers.py
2
-
3
  import base64
4
  import streamlit as st
5
  import os
6
  import openai
7
  from dotenv import load_dotenv
 
 
8
 
9
  # Function to accept OpenAI API Key as input from the user
10
  def get_api_key():
@@ -25,24 +25,29 @@ def speech_to_text(audio_data):
25
  )
26
  return transcript["text"] # Extract the text from the response
27
 
28
-
29
- from gtts import gTTS
30
-
31
  def text_to_speech(input_text):
32
  """Generates a TTS audio file from the input text."""
33
- tts = gTTS(text=input_text, lang="en")
34
- audio_file_path = "temp_audio_play.mp3"
35
- tts.save(audio_file_path)
36
- return audio_file_path
37
-
38
-
39
- def autoplay_audio(file_path: str):
40
- with open(file_path, "rb") as f:
41
- data = f.read()
42
- b64 = base64.b64encode(data).decode("utf-8")
43
- md = f"""
44
- <audio autoplay>
45
- <source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
46
- </audio>
47
- """
48
- st.markdown(md, unsafe_allow_html=True)
 
 
 
 
 
 
 
 
 
 
 
1
  import base64
2
  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
  )
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
+ # Split the text into manageable chunks
31
+ chunks = textwrap.wrap(input_text, 1000) # Wrap at 1000 characters
32
+
33
+ audio_file_paths = []
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(file_paths: list):
43
+ """Automatically plays audio from the provided file paths."""
44
+ for file_path in file_paths:
45
+ with open(file_path, "rb") as f:
46
+ data = f.read()
47
+ b64 = base64.b64encode(data).decode("utf-8")
48
+ md = f"""
49
+ <audio autoplay>
50
+ <source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
51
+ </audio>
52
+ """
53
+ st.markdown(md, unsafe_allow_html=True)