Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,64 +1,78 @@
|
|
1 |
-
import numpy as np
|
2 |
import gradio as gr
|
3 |
-
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
return word
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
|
|
|
|
|
|
17 |
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
|
|
|
|
|
|
|
|
25 |
|
26 |
-
|
27 |
-
|
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 |
-
|
37 |
-
|
38 |
-
scaled = np.int16(data/np.max(np.abs(data)) * 32767)
|
39 |
|
40 |
-
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
outputs=gr.Audio(label="Preview Animalese Audio")
|
61 |
-
)
|
62 |
|
63 |
-
|
64 |
-
gr_interface.launch()
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import wave
|
4 |
+
|
5 |
+
# Assuming the classes and methods from animalese.js are translated into Python
|
6 |
+
|
7 |
+
class Animalese:
|
8 |
+
def __init__(self, letters_file, onload):
|
9 |
+
with open(letters_file, 'rb') as f:
|
10 |
+
self.letter_library = np.frombuffer(f.read(), dtype=np.uint8)
|
11 |
+
onload()
|
12 |
+
|
13 |
+
def synthesize(self, script, shorten=False, pitch=1.0):
|
14 |
+
def shorten_word(word):
|
15 |
+
return word[0] + word[-1] if len(word) > 1 else word
|
16 |
+
|
17 |
+
processed_script = "".join(map(shorten_word, script.replace(/[^a-z]/gi, ' ').split())) if shorten else script
|
18 |
+
data = []
|
19 |
|
20 |
+
sample_freq = 44100
|
21 |
+
library_letter_secs = 0.15
|
22 |
+
library_samples_per_letter = int(library_letter_secs * sample_freq)
|
23 |
+
output_letter_secs = 0.075
|
24 |
+
output_samples_per_letter = int(output_letter_secs * sample_freq)
|
|
|
25 |
|
26 |
+
for c in processed_script.upper():
|
27 |
+
if 'A' <= c <= 'Z':
|
28 |
+
library_letter_start = library_samples_per_letter * (ord(c) - ord('A'))
|
29 |
+
for i in range(output_samples_per_letter):
|
30 |
+
data.append(self.letter_library[44 + library_letter_start + int(i * pitch)])
|
31 |
+
else:
|
32 |
+
data.extend([127] * output_samples_per_letter)
|
33 |
|
34 |
+
# Create the .wav file data
|
35 |
+
data = np.array(data, dtype=np.uint8)
|
36 |
+
return self.create_wave(data, sample_freq)
|
37 |
+
|
38 |
+
def create_wave(self, data, sample_rate):
|
39 |
+
with wave.open("output.wav", "wb") as f:
|
40 |
+
f.setnchannels(1)
|
41 |
+
f.setsampwidth(1)
|
42 |
+
f.setframerate(sample_rate)
|
43 |
+
f.writeframes(data.tobytes())
|
44 |
+
return "output.wav"
|
45 |
|
46 |
+
# Initialize the synthesizer
|
47 |
+
synth = Animalese('animalese.wav', lambda: print("Loaded"))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
|
49 |
+
def generate_audio(text, shorten, pitch):
|
50 |
+
return synth.synthesize(text, shorten, pitch)
|
|
|
51 |
|
52 |
+
def preview_audio(audio_file):
|
53 |
+
with open(audio_file, 'rb') as f:
|
54 |
+
return f.read()
|
55 |
|
56 |
+
# Gradio UI
|
57 |
+
with gr.Blocks() as demo:
|
58 |
+
gr.Markdown("# Animalese.js Demo in Gradio")
|
59 |
+
gr.Markdown("vist also [web version](https://huggingface.co/spaces/Blane187/animalese-js)")
|
60 |
+
text_input = gr.Textbox(label="Input Text", placeholder="Enter text to convert to Animalese")
|
61 |
+
shorten_input = gr.Checkbox(label="Shorten Words")
|
62 |
+
pitch_input = gr.Slider(minimum=0.2, maximum=2.0, step=0.1, value=1.0, label="Pitch")
|
63 |
|
64 |
+
with gr.Row():
|
65 |
+
preview_button = gr.Button("Preview!")
|
66 |
+
download_button = gr.Button("Download!")
|
67 |
+
|
68 |
+
audio_output = gr.Audio(label="Output Audio")
|
69 |
|
70 |
+
preview_button.click(fn=lambda text, shorten, pitch: preview_audio(generate_audio(text, shorten, pitch)),
|
71 |
+
inputs=[text_input, shorten_input, pitch_input],
|
72 |
+
outputs=audio_output)
|
73 |
+
|
74 |
+
download_button.click(fn=lambda text, shorten, pitch: generate_audio(text, shorten, pitch),
|
75 |
+
inputs=[text_input, shorten_input, pitch_input],
|
76 |
+
outputs=gr.File(label="Download .wav"))
|
|
|
|
|
77 |
|
78 |
+
demo.launch()
|
|