File size: 2,119 Bytes
98f5ae4
 
 
 
 
 
e088c4e
 
98f5ae4
 
 
 
 
 
 
 
 
 
 
 
 
e088c4e
98f5ae4
 
e088c4e
 
98f5ae4
 
 
 
e088c4e
0a036cb
 
 
 
 
 
e088c4e
98f5ae4
 
 
 
 
 
 
 
 
19556fd
98f5ae4
 
 
 
 
 
 
e088c4e
98f5ae4
143549e
 
98f5ae4
 
 
50169a4
 
804a2b5
 
143549e
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
66
67
import gradio as gr
import os
import sys
import logging
import requests

from tts import TTS

# Add the current directory to the path so we can import from app
sys.path.append(os.path.dirname(os.path.abspath(__file__)))

# Configure logging
logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger(__name__)

# Function to fetch audio from a URL
def fetch_audio(input_text):
    # Replace with the actual backend URL for generating audio
    backend_url = "http://localhost:8080/api/text_to_speech"
    response = requests.get(backend_url, params={"text": input_text})
    if response.status_code == 200:
        logger.info(f"Generated audio from text: {input_text}")
        logger.info(f"Audio URL: {response.url}")
        return response.url  # Return the URL of the generated audio
    else:
        return None
    
def generate_audio(input_text):
    try:
        audio_array = TTS.generate(input_text)
        return (44100, audio_array)
    except Exception as e:
        logger.error(f"Error generating audio: {e}")
        return None
    
# Gradio interface
def create_gradio_interface():
    # Create the Gradio interface
    with gr.Blocks(title="Swahili TTS Model") as demo:
        gr.Markdown("# Swahili TTS Model")
        gr.Markdown("Generate Swahili speech from text.")
        
        with gr.Row():
            with gr.Column():
                input_text = gr.Textbox(label="Input Text", lines=5)
                generate_btn = gr.Button("Generate Speech")
            
            with gr.Column():
                # show generated audio from a url on output, href = "https://sample.com/audio.wav"
                output = gr.Audio(label="Generated Audio", type="filepath")
        
            generate_btn.click(
                fn=generate_audio,
                inputs=input_text,
                outputs=output,
                queue=False,
            )
    return demo

app = create_gradio_interface()

# Launch the app
if __name__ == "__main__":
    app.launch(server_name="0.0.0.0", server_port=9090)