DexterSptizu commited on
Commit
a801f9e
·
verified ·
1 Parent(s): 7324b0c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -0
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from openai import OpenAI
3
+ import os
4
+ from pathlib import Path
5
+ import base64
6
+
7
+ def create_audio_player(audio_path):
8
+ """Create an HTML audio player for the generated audio file"""
9
+ audio_file = open(audio_path, 'rb')
10
+ audio_bytes = audio_file.read()
11
+ audio_base64 = base64.b64encode(audio_bytes).decode()
12
+ audio_player = f'<audio controls><source src="data:audio/mp3;base64,{audio_base64}" type="audio/mp3"></audio>'
13
+ return audio_player
14
+
15
+ def main():
16
+ st.title("🗣️ Text to Speech with OpenAI")
17
+
18
+ # Sidebar for API key
19
+ with st.sidebar:
20
+ st.header("Configuration")
21
+ api_key = st.text_input("Enter your OpenAI API key", type="password")
22
+ st.markdown("""
23
+ ### How to get an API key
24
+ 1. Go to [OpenAI API Keys](https://platform.openai.com/api-keys)
25
+ 2. Create a new secret key
26
+ 3. Copy and paste it here
27
+ """)
28
+
29
+ # Main content
30
+ st.markdown("Convert your text to natural-sounding speech using OpenAI's TTS API.")
31
+
32
+ # Text input
33
+ user_input = st.text_area("Enter the text you want to convert to speech:",
34
+ height=150,
35
+ placeholder="Enter your text here...")
36
+
37
+ # Voice selection
38
+ voice_option = st.selectbox(
39
+ "Select a voice:",
40
+ ["alloy", "echo", "fable", "onyx", "nova", "shimmer"]
41
+ )
42
+
43
+ # Create a button to generate audio
44
+ if st.button("Generate Speech"):
45
+ if not api_key:
46
+ st.error("Please enter your OpenAI API key in the sidebar.")
47
+ return
48
+
49
+ if not user_input:
50
+ st.error("Please enter some text to convert to speech.")
51
+ return
52
+
53
+ try:
54
+ # Show spinning indicator while processing
55
+ with st.spinner("Generating audio..."):
56
+ # Initialize OpenAI client with the API key
57
+ client = OpenAI(api_key=api_key)
58
+
59
+ # Create output directory if it doesn't exist
60
+ output_dir = Path("generated_audio")
61
+ output_dir.mkdir(exist_ok=True)
62
+
63
+ # Generate unique filename
64
+ output_file = output_dir / "output.mp3"
65
+
66
+ # Generate speech
67
+ response = client.audio.speech.create(
68
+ model="tts-1",
69
+ voice=voice_option,
70
+ input=user_input
71
+ )
72
+
73
+ # Save the audio file
74
+ response.stream_to_file(str(output_file))
75
+
76
+ # Display success message
77
+ st.success("Audio generated successfully!")
78
+
79
+ # Create and display audio player
80
+ st.markdown("### Listen to the generated audio:")
81
+ st.markdown(create_audio_player(output_file), unsafe_allow_html=True)
82
+
83
+ # Add download button
84
+ with open(output_file, "rb") as file:
85
+ st.download_button(
86
+ label="Download Audio",
87
+ data=file,
88
+ file_name="generated_speech.mp3",
89
+ mime="audio/mp3"
90
+ )
91
+
92
+ except Exception as e:
93
+ st.error(f"An error occurred: {str(e)}")
94
+
95
+ if __name__ == "__main__":
96
+ main()