Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,29 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
-
from moshi
|
4 |
-
from moshi.tokenizer import Tokenizer
|
5 |
-
from moshi.configuration import MoshiConfig
|
6 |
|
7 |
-
# Initialize
|
8 |
-
|
9 |
-
|
10 |
-
moshi = Moshi(config)
|
11 |
-
moshi.load_pretrained("kyutai/moshika-pytorch-bf16")
|
12 |
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
-
def
|
18 |
-
# Convert audio to tensor and process
|
19 |
-
audio_tensor = torch.tensor(audio).to(device).float()
|
20 |
-
with torch.no_grad():
|
21 |
-
tokens = moshi.encode_audio(audio_tensor)
|
22 |
-
text = tokenizer.decode(tokens)
|
23 |
-
return text
|
24 |
-
|
25 |
-
def text_to_speech(text):
|
26 |
# Generate speech from text
|
27 |
-
|
28 |
-
|
29 |
-
speech = moshi.decode_audio(tokens)
|
30 |
-
return (config.sample_rate, speech.cpu().numpy())
|
31 |
|
32 |
# Create Gradio interface
|
33 |
iface = gr.Interface(
|
34 |
-
fn=[
|
35 |
inputs=[
|
36 |
gr.Audio(source="microphone", type="numpy", label="Speak"),
|
37 |
gr.Textbox(label="Enter text for speech synthesis")
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
+
from moshi import MoshiServer
|
|
|
|
|
4 |
|
5 |
+
# Initialize MoshiServer
|
6 |
+
server = MoshiServer()
|
7 |
+
server.load_model("kyutai/moshika-pytorch-bf16")
|
|
|
|
|
8 |
|
9 |
+
def process_audio(audio, sample_rate):
|
10 |
+
# Convert audio to the correct format
|
11 |
+
audio_tensor = torch.tensor(audio).float()
|
12 |
+
if audio_tensor.dim() == 2:
|
13 |
+
audio_tensor = audio_tensor.mean(dim=1)
|
14 |
+
|
15 |
+
# Process audio
|
16 |
+
result = server.process_audio(audio_tensor, sample_rate)
|
17 |
+
return result.text
|
18 |
|
19 |
+
def generate_speech(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
20 |
# Generate speech from text
|
21 |
+
result = server.generate_speech(text)
|
22 |
+
return (server.config.sample_rate, result.audio.cpu().numpy())
|
|
|
|
|
23 |
|
24 |
# Create Gradio interface
|
25 |
iface = gr.Interface(
|
26 |
+
fn=[process_audio, generate_speech],
|
27 |
inputs=[
|
28 |
gr.Audio(source="microphone", type="numpy", label="Speak"),
|
29 |
gr.Textbox(label="Enter text for speech synthesis")
|