SatyamSinghal commited on
Commit
70e0c17
·
verified ·
1 Parent(s): 8e8d254

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +176 -3
app.py CHANGED
@@ -20,14 +20,181 @@ def text_to_speech(text, language="en"):
20
  except Exception as e:
21
  return f"Error: {str(e)}", None
22
 
23
- # Define the Gradio Interface
24
- language_options = [("English", "en"), ("Spanish", "es"), ("French", "fr"), ("German", "de")]
 
 
 
 
 
 
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  interface = gr.Interface(
27
  fn=text_to_speech,
28
  inputs=[
29
  gr.Textbox(label="Enter Text", placeholder="Type your text here..."),
30
- gr.Dropdown(choices=[lang[1] for lang in language_options], label="Select Language", value="en"),
31
  ],
32
  outputs=[
33
  gr.Textbox(label="Status"),
@@ -35,6 +202,12 @@ interface = gr.Interface(
35
  ],
36
  title="AI Text-to-Speech Web App",
37
  description="Convert text into natural-sounding speech using AI. Select a language, type text, and download the audio!",
 
 
 
 
 
 
38
  )
39
 
40
  # Launch the interface
 
20
  except Exception as e:
21
  return f"Error: {str(e)}", None
22
 
23
+ # Define the HTML interface with embedded JavaScript and CSS
24
+ html_content = """
25
+ <!DOCTYPE html>
26
+ <html lang="en">
27
+ <head>
28
+ <meta charset="UTF-8">
29
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
30
+ <title>AI Text-to-Speech Web App</title>
31
 
32
+ <!-- AOS (Animate On Scroll) for animations -->
33
+ <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aos@2.3.4/dist/aos.css">
34
+
35
+ <!-- Lottie for professional animations -->
36
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/bodymovin/5.7.6/lottie.min.js"></script>
37
+
38
+ <!-- SweetAlert2 for custom pop-ups -->
39
+ <script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
40
+
41
+ <!-- Custom CSS for styling -->
42
+ <style>
43
+ body {
44
+ font-family: 'Arial', sans-serif;
45
+ background-color: #f4f7fc;
46
+ color: #333;
47
+ margin: 0;
48
+ padding: 0;
49
+ display: flex;
50
+ justify-content: center;
51
+ align-items: center;
52
+ height: 100vh;
53
+ flex-direction: column;
54
+ }
55
+
56
+ h1 {
57
+ color: #4CAF50;
58
+ font-size: 36px;
59
+ margin-bottom: 20px;
60
+ }
61
+
62
+ .app-container {
63
+ width: 90%;
64
+ max-width: 600px;
65
+ background-color: #fff;
66
+ padding: 20px;
67
+ border-radius: 10px;
68
+ box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
69
+ text-align: center;
70
+ transition: all 0.3s ease;
71
+ }
72
+
73
+ .app-container:hover {
74
+ box-shadow: 0 8px 30px rgba(0, 0, 0, 0.2);
75
+ transform: scale(1.02);
76
+ }
77
+
78
+ .input-field,
79
+ .select-lang,
80
+ .btn {
81
+ width: 100%;
82
+ padding: 15px;
83
+ margin: 10px 0;
84
+ font-size: 16px;
85
+ border-radius: 5px;
86
+ border: 1px solid #ddd;
87
+ }
88
+
89
+ .btn {
90
+ background-color: #4CAF50;
91
+ color: white;
92
+ border: none;
93
+ cursor: pointer;
94
+ font-weight: bold;
95
+ }
96
+
97
+ .btn:hover {
98
+ background-color: #45a049;
99
+ }
100
+
101
+ .status-message {
102
+ margin-top: 20px;
103
+ font-size: 18px;
104
+ color: #555;
105
+ }
106
+
107
+ #audio-player {
108
+ margin-top: 20px;
109
+ display: none;
110
+ }
111
+
112
+ .animation-container {
113
+ width: 300px;
114
+ height: 300px;
115
+ margin: 0 auto;
116
+ }
117
+ </style>
118
+ </head>
119
+ <body>
120
+
121
+ <!-- Lottie Animation -->
122
+ <div class="animation-container" data-aos="fade-up">
123
+ <div id="lottie-animation"></div>
124
+ </div>
125
+
126
+ <h1>AI Text-to-Speech Web App</h1>
127
+
128
+ <div class="app-container" data-aos="zoom-in">
129
+ <input type="text" id="input-text" class="input-field" placeholder="Type your text here..." />
130
+ <select id="language-selector" class="select-lang">
131
+ <option value="en">English</option>
132
+ <option value="es">Spanish</option>
133
+ <option value="fr">French</option>
134
+ <option value="de">German</option>
135
+ </select>
136
+ <button class="btn" onclick="convertTextToSpeech()">Convert to Speech</button>
137
+
138
+ <div class="status-message" id="status"></div>
139
+ <audio id="audio-player" controls></audio>
140
+ </div>
141
+
142
+ <!-- AOS Library -->
143
+ <script src="https://cdn.jsdelivr.net/npm/aos@2.3.4/dist/aos.js"></script>
144
+
145
+ <script>
146
+ // Initialize AOS library
147
+ AOS.init();
148
+
149
+ // Lottie animation for a professional look
150
+ var animation = lottie.loadAnimation({
151
+ container: document.getElementById('lottie-animation'),
152
+ renderer: 'svg',
153
+ loop: true,
154
+ autoplay: true,
155
+ path: 'https://assets1.lottiefiles.com/packages/lf20_LZkcjJ.json' // You can change this to your preferred animation URL
156
+ });
157
+
158
+ function convertTextToSpeech() {
159
+ let text = document.getElementById("input-text").value;
160
+ let language = document.getElementById("language-selector").value;
161
+ let statusDiv = document.getElementById("status");
162
+ let audioPlayer = document.getElementById("audio-player");
163
+
164
+ if (!text.trim()) {
165
+ statusDiv.innerHTML = "Please enter some text!";
166
+ return;
167
+ }
168
+
169
+ // Show loading status
170
+ statusDiv.innerHTML = "Converting... Please wait.";
171
+
172
+ // Simulate API call with setTimeout (replace this with your real API call)
173
+ setTimeout(() => {
174
+ // Here, you should call your backend and get the audio file
175
+ let audioBlob = new Blob([new ArrayBuffer(1024)], { type: 'audio/mp3' }); // Mock blob for demo
176
+ audioPlayer.src = URL.createObjectURL(audioBlob);
177
+ audioPlayer.style.display = "block";
178
+
179
+ // Show success popup using SweetAlert2
180
+ Swal.fire({
181
+ icon: 'success',
182
+ title: 'Conversion Successful!',
183
+ text: 'Your text has been converted to speech.',
184
+ });
185
+ }, 2000); // Simulating delay of 2 seconds
186
+ }
187
+ </script>
188
+ </body>
189
+ </html>
190
+ """
191
+
192
+ # Gradio interface setup
193
  interface = gr.Interface(
194
  fn=text_to_speech,
195
  inputs=[
196
  gr.Textbox(label="Enter Text", placeholder="Type your text here..."),
197
+ gr.Dropdown(choices=["en", "es", "fr", "de"], label="Select Language", value="en"),
198
  ],
199
  outputs=[
200
  gr.Textbox(label="Status"),
 
202
  ],
203
  title="AI Text-to-Speech Web App",
204
  description="Convert text into natural-sounding speech using AI. Select a language, type text, and download the audio!",
205
+ allow_flagging="never",
206
+ theme="compact",
207
+ layout="vertical",
208
+ live=True,
209
+ # Display the HTML content
210
+ css=html_content,
211
  )
212
 
213
  # Launch the interface