Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# streamlit_app.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
import numpy as np
|
| 5 |
+
import tempfile
|
| 6 |
+
import soundfile as sf
|
| 7 |
+
import os
|
| 8 |
+
import io
|
| 9 |
+
|
| 10 |
+
# Caching the text-to-speech model
|
| 11 |
+
@st.cache_resource
|
| 12 |
+
def load_tts_pipeline():
|
| 13 |
+
return pipeline("text-to-speech", model="microsoft/speecht5_tts")
|
| 14 |
+
|
| 15 |
+
# Initialize the model once using cache
|
| 16 |
+
tts_pipe = load_tts_pipeline()
|
| 17 |
+
|
| 18 |
+
# Initialize session state for conversation history, bot response, and selected options
|
| 19 |
+
if 'conversation_history' not in st.session_state:
|
| 20 |
+
st.session_state.conversation_history = ""
|
| 21 |
+
if 'tts_audio' not in st.session_state:
|
| 22 |
+
st.session_state.tts_audio = None
|
| 23 |
+
|
| 24 |
+
def convert_text_to_speech(text):
|
| 25 |
+
# Generate speech from text
|
| 26 |
+
audio = tts_pipe(text)
|
| 27 |
+
return audio
|
| 28 |
+
|
| 29 |
+
def save_audio(audio):
|
| 30 |
+
# Save the audio to a temporary file
|
| 31 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.wav')
|
| 32 |
+
with open(temp_file.name, 'wb') as f:
|
| 33 |
+
f.write(audio['audio'])
|
| 34 |
+
return temp_file.name
|
| 35 |
+
|
| 36 |
+
# Sidebar options
|
| 37 |
+
st.sidebar.title("App Settings")
|
| 38 |
+
|
| 39 |
+
# Option to toggle between features
|
| 40 |
+
feature = st.sidebar.radio("Choose Feature:", ("Text-to-Speech", "Other Options"))
|
| 41 |
+
|
| 42 |
+
# Text-to-Speech Feature
|
| 43 |
+
if feature == "Text-to-Speech":
|
| 44 |
+
st.title("🗣 Text-to-Speech Converter")
|
| 45 |
+
st.subheader("Convert your text to speech using a TTS model!")
|
| 46 |
+
|
| 47 |
+
# Input field for text
|
| 48 |
+
user_message = st.text_area("Enter text to convert to speech:")
|
| 49 |
+
|
| 50 |
+
if st.button("Convert"):
|
| 51 |
+
if user_message:
|
| 52 |
+
# Convert text to speech
|
| 53 |
+
tts_audio = convert_text_to_speech(user_message)
|
| 54 |
+
audio_file = save_audio(tts_audio)
|
| 55 |
+
|
| 56 |
+
# Display the audio player
|
| 57 |
+
st.audio(audio_file, format='audio/wav')
|
| 58 |
+
st.success("Conversion successful!")
|
| 59 |
+
|
| 60 |
+
# Clean up temporary file
|
| 61 |
+
os.remove(audio_file)
|
| 62 |
+
else:
|
| 63 |
+
st.warning("Please enter text before converting.")
|
| 64 |
+
|
| 65 |
+
# About section
|
| 66 |
+
st.markdown("---")
|
| 67 |
+
st.markdown("### About this App")
|
| 68 |
+
st.info("This app uses a text-to-speech model from the Hugging Face Transformers library. Enter text to hear it spoken out loud.")
|
| 69 |
+
|
| 70 |
+
st.sidebar.markdown("---")
|
| 71 |
+
st.sidebar.write("Created by [Your Name](https://github.com/yourprofile)")
|