Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
from scipy.io.wavfile import write
|
4 |
+
from pydub import AudioSegment
|
5 |
+
|
6 |
+
# Define the core Animalese function
|
7 |
+
def animalese_synthesize(text, shorten=False, pitch=1.0):
|
8 |
+
def shorten_word(word):
|
9 |
+
if len(word) > 1:
|
10 |
+
return word[0] + word[-1]
|
11 |
+
return word
|
12 |
+
|
13 |
+
# Shorten words if the option is enabled
|
14 |
+
processed_script = text
|
15 |
+
if shorten:
|
16 |
+
processed_script = ''.join([shorten_word(w) for w in text.split()])
|
17 |
+
|
18 |
+
# Generate the waveform (This is a simplified version)
|
19 |
+
sample_rate = 44100
|
20 |
+
data = []
|
21 |
+
library_letter_secs = 0.15
|
22 |
+
library_samples_per_letter = int(library_letter_secs * sample_rate)
|
23 |
+
output_letter_secs = 0.075
|
24 |
+
output_samples_per_letter = int(output_letter_secs * sample_rate)
|
25 |
+
|
26 |
+
# Use a basic sine wave to simulate the sounds
|
27 |
+
for c in processed_script.upper():
|
28 |
+
if 'A' <= c <= 'Z':
|
29 |
+
freq = 440 + (ord(c) - ord('A')) * 10 # Simple mapping A-Z to frequencies
|
30 |
+
t = np.linspace(0, output_letter_secs, output_samples_per_letter, False)
|
31 |
+
wave = 0.5 * np.sin(2 * np.pi * freq * t * pitch)
|
32 |
+
data.extend(wave)
|
33 |
+
else:
|
34 |
+
data.extend([0] * output_samples_per_letter)
|
35 |
+
|
36 |
+
# Convert to numpy array and save as WAV
|
37 |
+
data = np.array(data, dtype=np.float32)
|
38 |
+
scaled = np.int16(data/np.max(np.abs(data)) * 32767)
|
39 |
+
|
40 |
+
return scaled, sample_rate
|
41 |
+
|
42 |
+
def generate_wav_file(text, shorten, pitch):
|
43 |
+
data, sample_rate = animalese_synthesize(text, shorten, pitch)
|
44 |
+
output_file = "animalese_output.wav"
|
45 |
+
write(output_file, sample_rate, data)
|
46 |
+
return output_file
|
47 |
+
|
48 |
+
# Define the Gradio interface
|
49 |
+
def preview_audio(text, shorten, pitch):
|
50 |
+
output_file = generate_wav_file(text, shorten, pitch)
|
51 |
+
return output_file
|
52 |
+
|
53 |
+
gr_interface = gr.Interface(
|
54 |
+
fn=preview_audio,
|
55 |
+
inputs=[
|
56 |
+
gr.Textbox(label="Text to Synthesize"),
|
57 |
+
gr.Checkbox(label="Shorten Words"),
|
58 |
+
gr.Slider(0.2, 2.0, step=0.1, label="Pitch", value=1.0)
|
59 |
+
],
|
60 |
+
outputs=gr.Audio(label="Preview Animalese Audio")
|
61 |
+
)
|
62 |
+
|
63 |
+
if __name__ == "__main__":
|
64 |
+
gr_interface.launch()
|