Blane187 commited on
Commit
b30b6f5
·
verified ·
1 Parent(s): 09e08a6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -53
app.py CHANGED
@@ -1,64 +1,78 @@
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()
 
 
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()