Spaces:
Running
Running
File size: 3,202 Bytes
4041fa9 b30b6f5 4041fa9 2d09a05 b30b6f5 4041fa9 b30b6f5 4041fa9 b30b6f5 09e08a6 b30b6f5 09e08a6 b30b6f5 09e08a6 b30b6f5 4041fa9 b30b6f5 4041fa9 09e08a6 b30b6f5 09e08a6 b30b6f5 09e08a6 b30b6f5 09e08a6 b30b6f5 2d09a05 54d182e 4041fa9 b30b6f5 09e08a6 b30b6f5 54d182e 09e08a6 b30b6f5 09e08a6 b30b6f5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
import re
import numpy as np
import wave
import gradio as gr
from blaise import *
class Animalese:
def __init__(self, letters_file, onload):
with open(letters_file, 'rb') as f:
self.letter_library = np.frombuffer(f.read(), dtype=np.uint8)
onload()
def synthesize(self, script, shorten=False, pitch=1.0):
def shorten_word(word):
return word[0] + word[-1] if len(word) > 1 else word
if shorten:
# Replace all non-alphabet characters with spaces and split the script into words
words = re.sub(r'[^a-zA-Z]', ' ', script).split()
# Shorten each word and join them back into a single string
processed_script = "".join(map(shorten_word, words))
else:
processed_script = script
data = []
sample_freq = 44100
library_letter_secs = 0.15
library_samples_per_letter = int(library_letter_secs * sample_freq)
output_letter_secs = 0.075
output_samples_per_letter = int(output_letter_secs * sample_freq)
for c in processed_script.upper():
if 'A' <= c <= 'Z':
library_letter_start = library_samples_per_letter * (ord(c) - ord('A'))
for i in range(output_samples_per_letter):
data.append(self.letter_library[44 + library_letter_start + int(i * pitch)])
else:
data.extend([127] * output_samples_per_letter)
# Create the .wav file data
data = np.array(data, dtype=np.uint8)
return self.create_wave(data, sample_freq)
def create_wave(self, data, sample_rate):
output_file = "output.wav"
with wave.open(output_file, "wb") as f:
f.setnchannels(1)
f.setsampwidth(1)
f.setframerate(sample_rate)
f.writeframes(data.tobytes())
return output_file
# Initialize the synthesizer
synth = Animalese('animalese.wav', lambda: print("Loaded"))
def generate_audio(text, shorten, pitch):
return synth.synthesize(text, shorten, pitch)
def preview_audio(audio_file):
with open(audio_file, 'rb') as f:
return f.read()
# Gradio UI
with gr.Blocks(theme=secret) as demo:
gr.Markdown("# Animalese.py Demo")
text_input = gr.Textbox(label="Input Text", placeholder="Enter text to convert to Animalese")
shorten_input = gr.Checkbox(label="Shorten Words")
pitch_input = gr.Slider(minimum=0.2, maximum=2.0, step=0.1, value=1.0, label="Pitch")
with gr.Row():
preview_button = gr.Button("Preview!")
download_button = gr.Button("Download!")
audio_output = gr.Audio(label="Output Audio", autoplay=True)
preview_button.click(fn=lambda text, shorten, pitch: preview_audio(generate_audio(text, shorten, pitch)),
inputs=[text_input, shorten_input, pitch_input],
outputs=audio_output)
download_button.click(fn=lambda text, shorten, pitch: generate_audio(text, shorten, pitch),
inputs=[text_input, shorten_input, pitch_input],
outputs=gr.File(label="Download .wav"))
demo.launch()
|