File size: 1,816 Bytes
aef1a8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
import streamlit as st
import os
from TTS.api import TTS
from pydub import AudioSegment
import io
import base64

# Initialize TTS model
@st.cache_resource
def load_tts_model():
    return TTS("tts_models/multilingual/multi-dataset/xtts_v2", gpu=True)

tts = load_tts_model()

st.title("French Text-to-Speech Converter")

# Text input
text_input = st.text_area("Enter your French text here:", height=150)

# Speed slider
speed = st.slider("Speech speed", 0.5, 1.5, 0.7, 0.1)

# Generate button
if st.button("Generate Speech"):
    if text_input:
        # Create a progress bar
        progress_bar = st.progress(0)
        status_text = st.empty()

        # Update progress
        status_text.text("Generating speech...")
        progress_bar.progress(25)

        # Generate speech
        output_path = "output.mp3"
        tts.tts_to_file(text=text_input,
                        file_path=output_path,
                        speaker_wav="female.wav",
                        speed=speed,
                        language="fr")

        progress_bar.progress(75)
        status_text.text("Processing audio...")

        # Read generated audio file
        audio = AudioSegment.from_mp3(output_path)

        # Create a download button
        buffer = io.BytesIO()
        audio.export(buffer, format="mp3")
        b64 = base64.b64encode(buffer.getvalue()).decode()
        href = f'<a href="data:audio/mp3;base64,{b64}" download="generated_speech.mp3">Download MP3</a>'
        
        progress_bar.progress(100)
        status_text.text("Done!")

        st.markdown(href, unsafe_allow_html=True)

        # Play audio in the browser
        st.audio(buffer, format="audio/mp3")

        # Clean up
        os.remove(output_path)
    else:
        st.warning("Please enter some text to convert to speech.")