File size: 6,062 Bytes
7ded678
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import gradio as gr
from gtts import gTTS
import io
import os
import time
from gtts.lang import _main_langs
from docx import Document

# Directory for audio files
AUDIO_DIR = 'audio_files'
# Maximum file age in seconds (24 hours)
MAX_FILE_AGE = 24 * 60 * 60

# Function to convert text to speech using gTTS
def text_to_speech(text, lang, tld):
    lang_codes = {lang_name: lang_code for lang_code, lang_name in _main_langs().items()}
    lang_code = lang_codes[lang]
    
    tts = gTTS(text, lang=lang_code, tld=tld)
    fp = io.BytesIO()
    tts.write_to_fp(fp)
    fp.seek(0)
    
    os.makedirs(AUDIO_DIR, exist_ok=True)
    file_name = str(time.time()) + '.mp3'
    file_path = os.path.join(AUDIO_DIR, file_name)
    
    with open(file_path, 'wb') as f:
        f.write(fp.read())
    
    delete_old_audio_files()
    return file_path, file_path

# Function to delete old audio files
def delete_old_audio_files():
    now = time.time()
    for file_name in os.listdir(AUDIO_DIR):
        file_path = os.path.join(AUDIO_DIR, file_name)
        if now - os.path.getmtime(file_path) > MAX_FILE_AGE:
            os.remove(file_path)

# Function to convert TXT file to speech
def txt_to_speech(file, lang, tld):
    with open(file.name, 'r') as f:
        text = f.read()
    return text_to_speech(text, lang, tld)

# Function to convert DOCX file to speech
def docx_to_speech(file, lang, tld):
    doc = Document(file.name)
    text = "\n".join([para.text for para in doc.paragraphs])
    return text_to_speech(text, lang, tld)

# Create beautiful Gradio interface
with gr.Blocks(css="""

    .gradio-container {

        background: linear-gradient(to right, #f6f8fa, #e9ecef);

    }

    .tabs {

        border-radius: 10px;

        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);

    }

    .input-container {

        background: white;

        padding: 20px;

        border-radius: 8px;

        margin: 10px 0;

    }

    .button {

        background: #007bff;

        color: white;

        border-radius: 5px;

        padding: 10px 20px;

        transition: all 0.3s ease;

    }

    .button:hover {

        background: #0056b3;

        transform: translateY(-2px);

    }

""") as iface:
    gr.Markdown(
        """

        # ๐ŸŽ™๏ธ Text to Speech Converter By LiaqatEagle

        ### Convert your text into natural-sounding speech

        """
    )
    
    with gr.Tabs(elem_classes="tabs") as tabs:
        with gr.Tab("โœ๏ธ Text to Speech"):
            with gr.Column(elem_classes="input-container"):
                text_input = gr.Textbox(
                    lines=10,
                    label="Enter your text here:",
                    placeholder="Type or paste your text here..."
                )
                with gr.Row():
                    lang_input = gr.Dropdown(
                        choices=list(_main_langs().values()),
                        label="Select Language:",
                        value="English"
                    )
                    tld_input = gr.Dropdown(
                        choices=["com", "co.uk", "ca"],
                        label="Select TLD:",
                        value="com"
                    )
                convert_btn = gr.Button("Convert", elem_classes="button")
                audio_output = gr.Audio(label="Audio Output")
                file_output = gr.File(label="Download Audio")
                
                convert_btn.click(
                    fn=text_to_speech,
                    inputs=[text_input, lang_input, tld_input],
                    outputs=[audio_output, file_output]
                )
        
        with gr.Tab("๐Ÿ“„ TXT to Speech"):
            with gr.Column(elem_classes="input-container"):
                file_input = gr.File(label="Upload your TXT file")
                with gr.Row():
                    lang_input_file = gr.Dropdown(
                        choices=list(_main_langs().values()),
                        label="Select Language:",
                        value="English"
                    )
                    tld_input_file = gr.Dropdown(
                        choices=["com", "co.uk", "ca"],
                        label="Select TLD:",
                        value="com"
                    )
                convert_btn_file = gr.Button("Convert", elem_classes="button")
                audio_output_file = gr.Audio(label="Audio Output")
                file_output_file = gr.File(label="Download Audio")
                
                convert_btn_file.click(
                    fn=txt_to_speech,
                    inputs=[file_input, lang_input_file, tld_input_file],
                    outputs=[audio_output_file, file_output_file]
                )
        
        with gr.Tab("๐Ÿ“ DOCX to Speech"):
            with gr.Column(elem_classes="input-container"):
                docx_file_input = gr.File(label="Upload your DOCX file")
                with gr.Row():
                    lang_input_docx = gr.Dropdown(
                        choices=list(_main_langs().values()),
                        label="Select Language:",
                        value="English"
                    )
                    tld_input_docx = gr.Dropdown(
                        choices=["com", "co.uk", "ca"],
                        label="Select TLD:",
                        value="com"
                    )
                convert_btn_docx = gr.Button("Convert", elem_classes="button")
                audio_output_docx = gr.Audio(label="Audio Output")
                file_output_docx = gr.File(label="Download Audio")
                
                convert_btn_docx.click(
                    fn=docx_to_speech,
                    inputs=[docx_file_input, lang_input_docx, tld_input_docx],
                    outputs=[audio_output_docx, file_output_docx]
                )

# Enable queue and launch the interface
iface.queue()
iface.launch()