File size: 1,755 Bytes
ed6dab8
 
 
 
 
7a38f58
 
ed6dab8
 
 
 
 
 
 
 
 
 
 
 
b8d2f96
ed6dab8
b8d2f96
 
ed6dab8
 
b8d2f96
ed6dab8
 
eadd27b
7a38f58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import base64
import streamlit as st
import os
import openai
from dotenv import load_dotenv
from gtts import gTTS
import textwrap

# Function to accept OpenAI API Key as input from the user
def get_api_key():
    """Prompt the user for their OpenAI API Key."""
    api_key = st.text_input("Enter your OpenAI API Key", type="password")
    if api_key:
        openai.api_key = api_key
        return api_key
    else:
        return None

def speech_to_text(audio_data):
    """Transcribes audio data to text using OpenAI's API."""
    with open(audio_data, "rb") as audio_file:
        transcript = openai.Audio.transcribe(
            model="whisper-1",  # Ensure the model is correct
            file=audio_file
        )
    return transcript["text"]  # Extract the text from the response

def text_to_speech(input_text):
    """Generates a TTS audio file from the input text."""
    # Split the text into manageable chunks
    chunks = textwrap.wrap(input_text, 1000)  # Wrap at 1000 characters
    
    audio_file_paths = []
    for i, chunk in enumerate(chunks):
        tts = gTTS(text=chunk, lang="en")
        audio_file_path = f"temp_audio_play_{i}.mp3"
        tts.save(audio_file_path)
        audio_file_paths.append(audio_file_path)

    return audio_file_paths  # Return a list of file paths

def autoplay_audio(file_paths: list):
    """Automatically plays audio from the provided file paths."""
    for file_path in file_paths:
        with open(file_path, "rb") as f:
            data = f.read()
        b64 = base64.b64encode(data).decode("utf-8")
        md = f"""
        <audio autoplay>
        <source src="data:audio/mp3;base64,{b64}" type="audio/mp3">
        </audio>
        """
        st.markdown(md, unsafe_allow_html=True)