shukdevdatta123 commited on
Commit
76fbbc1
·
verified ·
1 Parent(s): 450f755

Update helpers.py

Browse files
Files changed (1) hide show
  1. 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
- # 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)
 
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)