arifather51 commited on
Commit
0ea333c
Β·
verified Β·
1 Parent(s): a9cb85d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +111 -0
app.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from TTS.api import TTS
3
+ from pydub import AudioSegment
4
+ import tempfile
5
+ import os
6
+
7
+ # Load Hugging Face TTS model
8
+ tts = TTS(model_name="tts_models/en/ljspeech/tacotron2-DDC", progress_bar=False, gpu=False)
9
+
10
+ def text_to_speech_hf(text, rate, pitch):
11
+ if not text.strip():
12
+ return None, "Please enter text to convert."
13
+ try:
14
+ # Temporary WAV file
15
+ tmp_wav = "/tmp/output.wav"
16
+ tmp_mp3 = "/tmp/output.mp3"
17
+
18
+ # Adjust rate (speed). Note: pitch adjustment not directly supported
19
+ speed = 1.0 + (rate / 100.0) # rate -50 to +50
20
+ tts.tts_to_file(text=text, file_path=tmp_wav, speaker=tts.speakers[0], speed=speed)
21
+
22
+ # Convert WAV to MP3
23
+ audio = AudioSegment.from_wav(tmp_wav)
24
+ audio.export(tmp_mp3, format="mp3")
25
+
26
+ return tmp_mp3, None
27
+ except Exception as e:
28
+ return None, f"TTS generation failed: {e}"
29
+
30
+ with gr.Blocks(analytics_enabled=False) as demo:
31
+ gr.Markdown("# πŸŽ™οΈ Hugging Face TTS Text-to-Speech")
32
+
33
+ with gr.Row():
34
+ with gr.Column(scale=1):
35
+ gr.Markdown("## Text-to-Speech with Hugging Face TTS")
36
+ gr.Markdown("""
37
+ Convert text to speech using Hugging Face TTS model.
38
+ Adjust speech rate: 0 is default, positive values increase speed, negative values decrease.
39
+ """)
40
+
41
+ gr.HTML("""
42
+ <div style="margin: 20px 0; padding: 15px; border: 1px solid #4CAF50; border-radius: 10px; background-color: #f1f8e9;">
43
+ <p style="margin-top: 0;"><b>Looking for more features?</b></p>
44
+ <p>You can upgrade to advanced versions that include:</p>
45
+ <ul>
46
+ <li><b>Subtitle Support</b>: Input SRT format or TXT</li>
47
+ <li><b>File Upload</b>: Easily upload text files</li>
48
+ <li><b>MP3 Output</b>: Generate audio in multiple formats</li>
49
+ </ul>
50
+ <div style="text-align: center; margin-top: 15px;">
51
+ <a href="https://text-to-speech.wingetgui.com/" target="_blank"
52
+ style="display: inline-block;
53
+ background: linear-gradient(45deg, #4CAF50, #8BC34A);
54
+ color: white;
55
+ padding: 12px 30px;
56
+ text-decoration: none;
57
+ border-radius: 30px;
58
+ font-weight: bold;
59
+ font-size: 16px;">Try New Version βž”</a>
60
+ </div>
61
+ </div>
62
+ """)
63
+
64
+ with gr.Column(scale=1):
65
+ gr.HTML("""
66
+ <div style="height: 100%; background-color: #f0f8ff; padding: 15px; border-radius: 10px;">
67
+ <h2 style="color: #1e90ff; margin-top: 0;">Turn Your Text Into Professional Videos!</h2>
68
+ <ul style="list-style-type: none; padding-left: 0;">
69
+ <li>βœ… <b>40+ languages and 300+ voices supported</b></li>
70
+ <li>βœ… <b>Custom backgrounds, music, and visual effects</b></li>
71
+ <li>βœ… <b>Create engaging video content from simple text</b></li>
72
+ <li>βœ… <b>Perfect for educators, content creators, and marketers</b></li>
73
+ </ul>
74
+ <div style="text-align: center; margin-top: 20px;">
75
+ <span style="font-size: 96px;">🎬</span>
76
+ <div style="margin-top: 15px;">
77
+ <a href="https://text2video.wingetgui.com/" target="_blank"
78
+ style="display: inline-block;
79
+ background: linear-gradient(45deg, #2196F3, #21CBF3);
80
+ color: white;
81
+ padding: 12px 30px;
82
+ text-decoration: none;
83
+ border-radius: 30px;
84
+ font-weight: bold;
85
+ font-size: 16px;">Try Text-to-Video βž”</a>
86
+ </div>
87
+ </div>
88
+ </div>
89
+ """)
90
+
91
+ with gr.Row():
92
+ with gr.Column():
93
+ text_input = gr.Textbox(label="Input Text", lines=5)
94
+ rate_slider = gr.Slider(minimum=-50, maximum=50, value=0, label="Speech Rate Adjustment (%)", step=1)
95
+ pitch_slider = gr.Slider(minimum=-20, maximum=20, value=0, label="Pitch Adjustment (Hz)", step=1)
96
+
97
+ generate_btn = gr.Button("Generate Speech", variant="primary")
98
+
99
+ audio_output = gr.Audio(label="Generated Audio", type="filepath")
100
+ warning_md = gr.Markdown(label="Warning", visible=False)
101
+
102
+ generate_btn.click(
103
+ fn=text_to_speech_hf,
104
+ inputs=[text_input, rate_slider, pitch_slider],
105
+ outputs=[audio_output, warning_md]
106
+ )
107
+
108
+ gr.Markdown("Experience the power of Hugging Face TTS for text-to-speech conversion, and explore our advanced Text-to-Video Converter for more creative possibilities!")
109
+
110
+ demo.queue()
111
+ demo.launch(show_api=False)