Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,57 +1,45 @@
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
|
|
|
|
|
|
3 |
import os
|
4 |
-
import tempfile
|
5 |
-
from transformers import pipeline
|
6 |
-
import torch
|
7 |
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
text_to_audio = None
|
15 |
|
16 |
-
def
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
return gr.Audio(value=fp.name, type="filepath"), "Speech generated successfully"
|
22 |
-
except Exception as e:
|
23 |
-
return None, f"Error in speech generation: {str(e)}"
|
24 |
|
25 |
-
def
|
26 |
-
|
27 |
-
|
28 |
-
audio = text_to_audio(text, forward_params={"do_sample": True, "max_new_tokens": 256})
|
29 |
-
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as fp:
|
30 |
-
audio['audio'].save(fp.name)
|
31 |
-
return gr.Audio(value=fp.name, type="filepath"), "Sound generated successfully"
|
32 |
-
else:
|
33 |
-
return None, "Text-to-audio pipeline not available. Check logs for initialization error."
|
34 |
-
except Exception as e:
|
35 |
-
return None, f"Error in sound generation: {str(e)}"
|
36 |
|
37 |
-
|
38 |
-
|
39 |
-
gr.Markdown("# Lightweight Text-to-Speech and Text-to-Sound Generation Tool")
|
40 |
-
|
41 |
-
with gr.Tab("Text-to-Speech"):
|
42 |
-
text_input = gr.Textbox(label="Enter text for speech generation")
|
43 |
-
language_input = gr.Dropdown(["en", "es", "fr", "de", "it"], label="Select Language", value="en")
|
44 |
-
speech_button = gr.Button("Generate Speech")
|
45 |
-
speech_output = gr.Audio(label="Generated Speech")
|
46 |
-
speech_message = gr.Textbox(label="Message")
|
47 |
-
|
48 |
-
with gr.Tab("Text-to-Sound"):
|
49 |
-
sound_input = gr.Textbox(label="Enter text description for sound generation")
|
50 |
-
sound_button = gr.Button("Generate Sound")
|
51 |
-
sound_output = gr.Audio(label="Generated Sound")
|
52 |
-
sound_message = gr.Textbox(label="Message")
|
53 |
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
iface.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from gtts import gTTS
|
3 |
+
import numpy as np
|
4 |
+
import io
|
5 |
+
import base64
|
6 |
import os
|
|
|
|
|
|
|
7 |
|
8 |
+
def text_to_speech(text, language='en'):
|
9 |
+
tts = gTTS(text=text, lang=language)
|
10 |
+
fp = io.BytesIO()
|
11 |
+
tts.write_to_fp(fp)
|
12 |
+
fp.seek(0)
|
13 |
+
return fp
|
|
|
14 |
|
15 |
+
def generate_sound(frequency, duration):
|
16 |
+
# Generate a simple sine wave
|
17 |
+
t = np.linspace(0, duration, int(44100 * duration), False)
|
18 |
+
audio = np.sin(2 * np.pi * frequency * t)
|
19 |
+
return (44100, audio.astype(np.float32))
|
|
|
|
|
|
|
20 |
|
21 |
+
def generate_voiceover(text, language):
|
22 |
+
audio_fp = text_to_speech(text, language)
|
23 |
+
return audio_fp
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
def generate_sound_effect(frequency, duration):
|
26 |
+
return generate_sound(frequency, duration)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
28 |
+
# Gradio interface
|
29 |
+
iface = gr.Interface(
|
30 |
+
fn=[generate_voiceover, generate_sound_effect],
|
31 |
+
inputs=[
|
32 |
+
gr.Textbox(label="Enter text for speech generation"),
|
33 |
+
gr.Dropdown(choices=["en", "es", "fr", "de", "it"], label="Select Language", value="en"),
|
34 |
+
gr.Number(label="Frequency (Hz)", value=440),
|
35 |
+
gr.Number(label="Duration (seconds)", value=1)
|
36 |
+
],
|
37 |
+
outputs=[
|
38 |
+
gr.Audio(label="Generated Speech"),
|
39 |
+
gr.Audio(label="Generated Sound Effect")
|
40 |
+
],
|
41 |
+
title="Simple Text-to-Speech and Sound Generation Tool",
|
42 |
+
description="Generate speech from text and create simple sound effects."
|
43 |
+
)
|
44 |
|
45 |
iface.launch()
|