sereich commited on
Commit
416692d
·
verified ·
1 Parent(s): cde33de

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -84
app.py CHANGED
@@ -1,85 +1,85 @@
1
- import gradio as gr
2
- import torch
3
- import numpy as np
4
- from torchaudio.functional import resample
5
-
6
- from processAudio import upscaleAudio
7
-
8
- class Object(object):
9
- pass
10
-
11
- with gr.Blocks(theme=gr.themes.Default().set(body_background_fill="#CCEEFF")) as layout:
12
- with gr.Row():
13
- gr.Markdown("<h2>Broadcast Audio Upscaler</h2>")
14
- with gr.Row():
15
- with open("html/directions.html", "r") as directionsHtml:
16
- gr.Markdown(directionsHtml.read())
17
- with gr.Row():
18
- modelSelect = gr.Dropdown(
19
- [
20
- ["FM Radio Super Resolution","FM_Radio_SR.th"],
21
- ["AM Radio Super Resolution (Beta)","AM_Radio_SR.th"],
22
- ["Telephone Super Resolution (Beta)","Telephone_SR.th"]
23
- ],
24
- label="Select Model:",
25
- value="FM_Radio_SR.th",
26
- )
27
- with gr.Row():
28
- with gr.Column():
29
- audioFileSelect = gr.Audio(label="Audio File (Mono or Stereo, Max 6 Minutes):",sources="upload", max_length=360)
30
- with gr.Column():
31
- audioOutput = gr.Audio(show_download_button=True, label="Restored Audio:", sources=[], max_length=360)
32
- with gr.Row():
33
- with gr.Column():
34
- submit = gr.Button("Process Audio", variant="primary", interactive=False)
35
- with gr.Row():
36
- with gr.Accordion("More Information:", open=False):
37
- with open("html/information.html", "r") as informationHtml:
38
- gr.Markdown(informationHtml.read())
39
-
40
- @audioFileSelect.input(inputs=audioFileSelect, outputs=[submit, audioFileSelect])
41
- def audioFileSelectChanged(audioData: gr.Audio):
42
- #Audio exists and is mono or stereo
43
- if audioData is None:
44
- return gr.update(interactive=False), None
45
- if len(audioData[1].shape) == 1:
46
- return gr.update(interactive=True), audioData
47
- if audioData[1].shape[1] > 2:
48
- gr.Warning("Audio with more than 2 channels is not supported.")
49
- return gr.update(interactive=False), None
50
- return gr.update(interactive=True), audioData
51
-
52
-
53
- @submit.click(inputs=[modelSelect, audioFileSelect], outputs=audioOutput)
54
- def processAudio(model: gr.Dropdown, audioData: gr.Audio):
55
- if audioData is None:
56
- raise gr.Error("Load an audio file.")
57
- return None
58
- elif len(audioData[1].shape) == 1: #Convert mono to stereo
59
- lrAudio = torch.tensor(np.array([
60
- audioData[1].copy().astype(np.float32)/32768,
61
- audioData[1].copy().astype(np.float32)/32768
62
- ]))
63
- elif audioData[1].shape[1] > 2:
64
- raise gr.Error("Audio with more than 2 channels is not supported.")
65
- return None
66
- else: #re-order channel data from [samples, 2] to [2, samples]
67
- lrAudio = torch.tensor(audioData[1].copy().astype(np.float32)/32768).transpose(0,1)
68
- if audioData[0] != 44100:
69
- lrAudio = resample(lrAudio, audioData[0], 44100)
70
- model_name, experiment_file = getModelInfo(model)
71
- hrAudio=upscaleAudio(lrAudio, model, model_name=model_name, experiment_file=experiment_file)
72
- hrAudio=hrAudio / max(hrAudio.abs().max().item(), 1)
73
- outAudio=(hrAudio*32767).numpy().astype(np.int16).transpose(1,0)
74
- return tuple([44100, outAudio])
75
-
76
- def getModelInfo(modelFilename: str):
77
- if(modelFilename == "FM_Radio_SR.th"):
78
- return "aero", "aero_441-441_512_256.yaml"
79
- if(modelFilename == "AM_Radio_SR.th"):
80
- return "aero", "aero_441-441_512_256.yaml"
81
- if(modelFilename == "Telephone_SR.th"):
82
- return "aero", "aero_441-441_512_256.yaml"
83
- return "aero", "aero_441-441_512_256.yaml"
84
-
85
  layout.launch()
 
1
+ import gradio as gr
2
+ import torch
3
+ import numpy as np
4
+ from torchaudio.functional import resample
5
+
6
+ from processAudio import upscaleAudio
7
+
8
+ class Object(object):
9
+ pass
10
+
11
+ with gr.Blocks(theme=gr.themes.Default().set(body_background_fill="#CCEEFF")) as layout:
12
+ with gr.Row():
13
+ gr.Markdown("<h2>Broadcast Audio Upscaler</h2>")
14
+ with gr.Row():
15
+ with open("html/directions.html", "r") as directionsHtml:
16
+ gr.Markdown(directionsHtml.read())
17
+ with gr.Row():
18
+ modelSelect = gr.Dropdown(
19
+ [
20
+ ["FM Radio Super Resolution","FM_Radio_SR.th"],
21
+ ["AM Radio Super Resolution (Beta v2)","AM_Radio_SR.th"],
22
+ ["Telephone Super Resolution (Beta)","Telephone_SR.th"]
23
+ ],
24
+ label="Select Model:",
25
+ value="FM_Radio_SR.th",
26
+ )
27
+ with gr.Row():
28
+ with gr.Column():
29
+ audioFileSelect = gr.Audio(label="Audio File (Mono or Stereo, Max 6 Minutes):",sources="upload", max_length=360)
30
+ with gr.Column():
31
+ audioOutput = gr.Audio(show_download_button=True, label="Restored Audio:", sources=[], max_length=360)
32
+ with gr.Row():
33
+ with gr.Column():
34
+ submit = gr.Button("Process Audio", variant="primary", interactive=False)
35
+ with gr.Row():
36
+ with gr.Accordion("More Information:", open=False):
37
+ with open("html/information.html", "r") as informationHtml:
38
+ gr.Markdown(informationHtml.read())
39
+
40
+ @audioFileSelect.input(inputs=audioFileSelect, outputs=[submit, audioFileSelect])
41
+ def audioFileSelectChanged(audioData: gr.Audio):
42
+ #Audio exists and is mono or stereo
43
+ if audioData is None:
44
+ return gr.update(interactive=False), None
45
+ if len(audioData[1].shape) == 1:
46
+ return gr.update(interactive=True), audioData
47
+ if audioData[1].shape[1] > 2:
48
+ gr.Warning("Audio with more than 2 channels is not supported.")
49
+ return gr.update(interactive=False), None
50
+ return gr.update(interactive=True), audioData
51
+
52
+
53
+ @submit.click(inputs=[modelSelect, audioFileSelect], outputs=audioOutput)
54
+ def processAudio(model: gr.Dropdown, audioData: gr.Audio):
55
+ if audioData is None:
56
+ raise gr.Error("Load an audio file.")
57
+ return None
58
+ elif len(audioData[1].shape) == 1: #Convert mono to stereo
59
+ lrAudio = torch.tensor(np.array([
60
+ audioData[1].copy().astype(np.float32)/32768,
61
+ audioData[1].copy().astype(np.float32)/32768
62
+ ]))
63
+ elif audioData[1].shape[1] > 2:
64
+ raise gr.Error("Audio with more than 2 channels is not supported.")
65
+ return None
66
+ else: #re-order channel data from [samples, 2] to [2, samples]
67
+ lrAudio = torch.tensor(audioData[1].copy().astype(np.float32)/32768).transpose(0,1)
68
+ if audioData[0] != 44100:
69
+ lrAudio = resample(lrAudio, audioData[0], 44100)
70
+ model_name, experiment_file = getModelInfo(model)
71
+ hrAudio=upscaleAudio(lrAudio, model, model_name=model_name, experiment_file=experiment_file)
72
+ hrAudio=hrAudio / max(hrAudio.abs().max().item(), 1)
73
+ outAudio=(hrAudio*32767).numpy().astype(np.int16).transpose(1,0)
74
+ return tuple([44100, outAudio])
75
+
76
+ def getModelInfo(modelFilename: str):
77
+ if(modelFilename == "FM_Radio_SR.th"):
78
+ return "aero", "aero_441-441_512_256.yaml"
79
+ if(modelFilename == "AM_Radio_SR.th"):
80
+ return "aero", "aero_441-441_512_256.yaml"
81
+ if(modelFilename == "Telephone_SR.th"):
82
+ return "aero", "aero_441-441_512_256.yaml"
83
+ return "aero", "aero_441-441_512_256.yaml"
84
+
85
  layout.launch()