BroDvd commited on
Commit
eb836ff
·
verified ·
1 Parent(s): 3ac3420

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -60
app.py CHANGED
@@ -1,85 +1,82 @@
1
- from pathlib import Path
2
- from threading import Thread
3
 
4
- import gdown
5
- import gradio as gr
6
- import librosa
7
- import numpy as np
8
- import torch
9
 
10
- from gradio_examples import EXAMPLES
11
- from pipeline import build_audiosep
12
 
13
- CHECKPOINTS_DIR = Path("checkpoint")
14
 
15
- DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
16
 
17
- # The model will be loaded in the future
18
  MODEL_NAME = CHECKPOINTS_DIR / "audiosep_base_4M_steps.ckpt"
19
- MODEL = build_audiosep(
20
  config_yaml="config/audiosep_base.yaml",
21
- checkpoint_path=MODEL_NAME,
22
- device=DEVICE,
23
  )
24
 
25
 
26
- description = """
27
- # AudioSep: Separate Anything You Describe
28
- [[Project Page]](https://audio-agi.github.io/Separate-Anything-You-Describe) [[Paper]](https://audio-agi.github.io/Separate-Anything-You-Describe/AudioSep_arXiv.pdf) [[Code]](https://github.com/Audio-AGI/AudioSep)
29
 
30
- AudioSep is a foundation model for open-domain sound separation with natural language queries.
31
- AudioSep demonstrates strong separation performance and impressive zero-shot generalization ability on
32
- numerous tasks such as audio event separation, musical instrument separation, and speech enhancement.
33
  """
34
 
35
 
36
- def inference(audio_file_path: str, text: str, use_advanced: bool):
37
- print(f"Separate audio from [{audio_file_path}] with textual query [{text}] and advanced mode [{use_advanced}]")
38
- mixture, _ = librosa.load(audio_file_path, sr=32000, mono=True)
39
 
40
- with torch.no_grad():
41
- text = [text]
42
 
43
- conditions = MODEL.query_encoder.get_query_embed(
44
- modality="text", text=text, device=DEVICE
45
  )
46
 
47
  input_dict = {
48
- "mixture": torch.Tensor(mixture)[None, None, :].to(DEVICE),
49
- "condition": conditions,
50
  }
51
 
52
- if use_chunk:
53
- sep_segment = model.ss_model.chunk_inference(input_dict)
54
- sep_segment = np.squeeze(sep_segment)
55
- else:
56
- sep_segment = model.ss_model(input_dict)["waveform"]
57
- sep_segment = sep_segment.squeeze(0).squeeze(0).data.cpu().numpy()
58
 
59
- return 32000, np.round(sep_segment * 32767).astype(np.int16)
60
 
 
61
 
62
- with gr.Blocks(title="AudioSep") as demo:
63
- gr.Markdown(description)
64
- with gr.Row():
65
- with gr.Column():
 
66
  input_audio = gr.Audio(label="Mixture", type="filepath")
67
- text = gr.Textbox(label="Text Query")
68
- use_chunk = gr.Checkbox(label="Use Chunk Mode (Less Quality but Less Memory Used)", value=False)
69
- with gr.Column():
70
- output_audio = gr.Audio(label="Separation Result", scale=10)
71
- button = gr.Button(
72
- "Separate",
73
- variant="primary",
74
- scale=2,
75
- size="lg",
76
- interactive=True,
77
- )
78
- button.click(
79
- fn=inference, inputs=[input_audio, text, use_chunk], outputs=[output_audio]
80
- )
81
-
82
- gr.Markdown("## Examples")
83
- gr.Examples(examples=EXAMPLES, inputs=[input_audio, text])
84
-
85
- demo.queue().launch(share=True)
 
1
+ da pathlib importa Percorso
2
+ da threading importa Thread
3
 
4
+ importa gdown
5
+ importa gradio come gr
6
+ importare librosa
7
+ importa numpy come np
8
+ importare torcia
9
 
10
+ da gradio_examples importa ESEMPI
11
+ da pipeline importa build_audiosep
12
 
13
+ CHECKPOINTS_DIR = Percorso("punto di controllo")
14
 
15
+ DISPOSITIVO = torch.device("cuda" se torch.cuda.is_available() altrimenti "cpu")
16
 
17
+ # Il modello verrà caricato in futuro
18
  MODEL_NAME = CHECKPOINTS_DIR / "audiosep_base_4M_steps.ckpt"
19
+ MODELLO = build_audiosep(
20
  config_yaml="config/audiosep_base.yaml",
21
+ checkpoint_path=NOME_MODELLO,
22
+ dispositivo=DISPOSITIVO,
23
  )
24
 
25
 
26
+ descrizione = """
27
+ # AudioSep: Separa tutto ciò che descrivi
28
+ [[Pagina del progetto]](https://audio-agi.github.io/Separate-Anything-You-Describe) [[Documento]](https://audio-agi.github.io/Separate-Anything-You-Describe/AudioSep_arXiv.pdf) [[Codice]](https://github.com/Audio-AGI/AudioSep)
29
 
30
+ AudioSep è un modello fondamentale per la separazione dei suoni in dominio aperto mediante query in linguaggio naturale.
31
+ AudioSep dimostra una forte prestazione di separazione e un'impressionante capacità di generalizzazione zero-shot su
32
+ numerosi compiti quali la separazione degli eventi audio, la separazione degli strumenti musicali e il miglioramento della voce.
33
  """
34
 
35
 
36
+ def inference(percorso_file_audio: str, testo: str):
37
+ print(f"Separa l'audio da [{audio_file_path}] con query testuale [{text}]")
38
+ miscela, _ = librosa.load(percorso_file_audio, sr=32000, mono=True)
39
 
40
+ con torch.no_grad():
41
+ testo = [testo]
42
 
43
+ condizioni = MODELLO.query_encoder.get_query_embed(
44
+ modalità="testo", testo=testo, dispositivo=DISPOSITIVO
45
  )
46
 
47
  input_dict = {
48
+ "miscela": torch.Tensor(miscela)[Nessuno, Nessuno, :].to(DISPOSITIVO),
49
+ "condizione": condizioni,
50
  }
51
 
52
+ sep_segment = MODELLO.ss_model(input_dict)["forma d'onda"]
 
 
 
 
 
53
 
54
+ sep_segment = sep_segment.squeeze(0).squeeze(0).data.cpu().numpy()
55
 
56
+ restituisci 32000, np.round(sep_segment * 32767).astype(np.int16)
57
 
58
+
59
+ con gr.Blocks(title="AudioSep") come demo:
60
+ gr.Markdown(descrizione)
61
+ con gr.Row():
62
+ con gr.Column():
63
  input_audio = gr.Audio(label="Mixture", type="filepath")
64
+ testo = gr.Textbox(label="Query di testo")
65
+ con gr.Column():
66
+ con gr.Column():
67
+ output_audio = gr.Audio(label="Risultato di separazione", scale=10)
68
+ pulsante = gr.Pulsante(
69
+ "Separato",
70
+ variante="primario",
71
+ scala=2,
72
+ dimensione="lg",
73
+ interattivo=Vero,
74
+ )
75
+ pulsante.clicca(
76
+ fn=inferenza, input=[input_audio, testo], output=[output_audio]
77
+ )
78
+
79
+ gr.Markdown("## Esempi")
80
+ gr.Examples(esempi=ESEMPI, input=[input_audio, testo])
81
+
82
+ demo.queue().launch(condividi=True)