Blane187 commited on
Commit
4041fa9
·
verified ·
1 Parent(s): a98ad53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -1,20 +1,26 @@
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
@@ -36,12 +42,13 @@ class Animalese:
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"))
@@ -56,7 +63,7 @@ def preview_audio(audio_file):
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")
 
1
+ import re
2
  import numpy as np
3
  import wave
4
+ import gradio as gr
 
5
 
6
  class Animalese:
7
  def __init__(self, letters_file, onload):
8
  with open(letters_file, 'rb') as f:
9
  self.letter_library = np.frombuffer(f.read(), dtype=np.uint8)
10
  onload()
11
+
12
  def synthesize(self, script, shorten=False, pitch=1.0):
13
  def shorten_word(word):
14
  return word[0] + word[-1] if len(word) > 1 else word
15
 
16
+ if shorten:
17
+ # Replace all non-alphabet characters with spaces and split the script into words
18
+ words = re.sub(r'[^a-zA-Z]', ' ', script).split()
19
+ # Shorten each word and join them back into a single string
20
+ processed_script = "".join(map(shorten_word, words))
21
+ else:
22
+ processed_script = script
23
+
24
  data = []
25
 
26
  sample_freq = 44100
 
42
  return self.create_wave(data, sample_freq)
43
 
44
  def create_wave(self, data, sample_rate):
45
+ output_file = "output.wav"
46
+ with wave.open(output_file, "wb") as f:
47
  f.setnchannels(1)
48
  f.setsampwidth(1)
49
  f.setframerate(sample_rate)
50
  f.writeframes(data.tobytes())
51
+ return output_file
52
 
53
  # Initialize the synthesizer
54
  synth = Animalese('animalese.wav', lambda: print("Loaded"))
 
63
  # Gradio UI
64
  with gr.Blocks() as demo:
65
  gr.Markdown("# Animalese.js Demo in Gradio")
66
+
67
  text_input = gr.Textbox(label="Input Text", placeholder="Enter text to convert to Animalese")
68
  shorten_input = gr.Checkbox(label="Shorten Words")
69
  pitch_input = gr.Slider(minimum=0.2, maximum=2.0, step=0.1, value=1.0, label="Pitch")