File size: 1,644 Bytes
e67e9cb
6e05b44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e67e9cb
 
6e05b44
e67e9cb
6e05b44
 
 
e67e9cb
6e05b44
 
 
 
e67e9cb
 
 
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
import gradio as gr
from gtts import gTTS
from pydub import AudioSegment
import tempfile
import os

def generate_tts(text, language, pitch_shift):
    tts = gTTS(text=text, lang=language)
    
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
        tts.save(temp_file.name)
        temp_file_path = temp_file.name
    
    try:
        audio = AudioSegment.from_file(temp_file_path, format="mp3")
    except Exception as e:
        print(f"Error loading audio file: {e}")
        return None
    
    # Apply pitch shifting (more pronounced)
    if pitch_shift:
        audio = audio._spawn(audio.raw_data, overrides={"frame_rate": int(audio.frame_rate * 0.8)})
        audio = audio.set_frame_rate(44100)
    
    with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as out_file:
        audio.export(out_file.name, format="mp3")
        out_file_path = out_file.name
    
    os.remove(temp_file_path)
    
    return out_file_path

def chatbot(text, language, male_voice):
    output_audio_path = generate_tts(text, language, male_voice)
    if output_audio_path is None:
        return "Error generating audio"
    return output_audio_path

iface = gr.Interface(
    fn=chatbot,
    inputs=[
        gr.Textbox(label="Enter your text"), 
        gr.Dropdown(label="Select Language", choices=["en", "es", "fr", "de", "it", "hi"], value="en"), 
        gr.Checkbox(label="Male Voice", value=True)
    ],
    outputs=gr.Audio(label="Generated Audio"),
    live=True,
    title="Text-to-Speech AI Chatbot",
    description="Generate speech with different languages and voice options."
)

iface.launch()