sereich commited on
Commit
479ecdf
·
verified ·
1 Parent(s): 7b84bc5

Update for gradio 6.3.0

Browse files
Files changed (1) hide show
  1. app.py +88 -85
app.py CHANGED
@@ -1,86 +1,89 @@
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 v3)","AM_Radio_SR.th"],
22
- ["Telephone Super Resolution (Beta v2)","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")
30
- audioFileSelect.upload(validator=lambda audio: gr.validators.is_audio_correct_length(audio, min_length=1, max_length=360))
31
- with gr.Column():
32
- audioOutput = gr.Audio(buttons=["download"], label="Restored Audio:", sources=[])
33
- with gr.Row():
34
- with gr.Column():
35
- submit = gr.Button("Process Audio", variant="primary", interactive=False)
36
- with gr.Row():
37
- with gr.Accordion("More Information:", open=False):
38
- with open("html/information.html", "r") as informationHtml:
39
- gr.Markdown(informationHtml.read())
40
-
41
- @audioFileSelect.input(inputs=audioFileSelect, outputs=[submit, audioFileSelect])
42
- def audioFileSelectChanged(audioData: gr.Audio):
43
- #Audio exists and is mono or stereo
44
- if audioData is None:
45
- return gr.update(interactive=False), None
46
- if len(audioData[1].shape) == 1:
47
- return gr.update(interactive=True), audioData
48
- if audioData[1].shape[1] > 2:
49
- gr.Warning("Audio with more than 2 channels is not supported.")
50
- return gr.update(interactive=False), None
51
- return gr.update(interactive=True), audioData
52
-
53
-
54
- @submit.click(inputs=[modelSelect, audioFileSelect], outputs=audioOutput)
55
- def processAudio(model: gr.Dropdown, audioData: gr.Audio):
56
- if audioData is None:
57
- raise gr.Error("Load an audio file.")
58
- return None
59
- elif len(audioData[1].shape) == 1: #Convert mono to stereo
60
- lrAudio = torch.tensor(np.array([
61
- audioData[1].copy().astype(np.float32)/32768,
62
- audioData[1].copy().astype(np.float32)/32768
63
- ]))
64
- elif audioData[1].shape[1] > 2:
65
- raise gr.Error("Audio with more than 2 channels is not supported.")
66
- return None
67
- else: #re-order channel data from [samples, 2] to [2, samples]
68
- lrAudio = torch.tensor(audioData[1].copy().astype(np.float32)/32768).transpose(0,1)
69
- if audioData[0] != 44100:
70
- lrAudio = resample(lrAudio, audioData[0], 44100)
71
- model_name, experiment_file = getModelInfo(model)
72
- hrAudio=upscaleAudio(lrAudio, model, model_name=model_name, experiment_file=experiment_file)
73
- hrAudio=hrAudio / max(hrAudio.abs().max().item(), 1)
74
- outAudio=(hrAudio*32767).numpy().astype(np.int16).transpose(1,0)
75
- return tuple([44100, outAudio])
76
-
77
- def getModelInfo(modelFilename: str):
78
- if(modelFilename == "FM_Radio_SR.th"):
79
- return "aero", "aero_441-441_512_256.yaml"
80
- if(modelFilename == "AM_Radio_SR.th"):
81
- return "aero", "aero_441-441_512_256.yaml"
82
- if(modelFilename == "Telephone_SR.th"):
83
- return "aero", "aero_441-441_512_256.yaml"
84
- return "aero", "aero_441-441_512_256.yaml"
85
-
 
 
 
86
  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 v3)","AM_Radio_SR.th"],
22
+ ["Telephone Super Resolution (Beta v2)","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")
30
+ with gr.Column():
31
+ audioOutput = gr.Audio(buttons=["download"], label="Restored Audio:", sources=[])
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.upload(inputs=audioFileSelect, outputs=[submit, audioFileSelect])
41
+ def audioFileSelectChanged(audioData: gr.Audio):
42
+ #Audio exists and is mono or stereo
43
+ if audioData is None or audioData.value is None:
44
+ return gr.update(interactive=False), None
45
+ if not gr.validators.is_audio_correct_length(audioData.value, min_length=None, max_length=360)["is_valid"]:
46
+ gr.Warning("Audio must be shorter than 360 seconds.")
47
+ return gr.update(interactive=False), None
48
+ if len(audioData.value[1].shape) == 1:
49
+ return gr.update(interactive=True), audioData
50
+ if audioData.value[1].shape[1] > 2:
51
+ gr.Warning("Audio with more than 2 channels is not supported.")
52
+ return gr.update(interactive=False), None
53
+ return gr.update(interactive=True), audioData
54
+
55
+
56
+ @submit.click(inputs=[modelSelect, audioFileSelect], outputs=audioOutput)
57
+ def processAudio(model: gr.Dropdown, audioData: gr.Audio):
58
+ if audioData is None or audioData.value is None:
59
+ raise gr.Error("Load an audio file.")
60
+ elif not gr.validators.is_audio_correct_length(audioData.value, min_length=None, max_length=360)["is_valid"]:
61
+ raise gr.Error("Audio must be shorter than 360 seconds.")
62
+ elif len(audioData.value[1].shape) == 1: #Convert mono to stereo
63
+ lrAudio = torch.tensor(np.array([
64
+ audioData.value[1].copy().astype(np.float32)/32768,
65
+ audioData.value[1].copy().astype(np.float32)/32768
66
+ ]))
67
+ elif audioData.value[1].shape[1] > 2:
68
+ raise gr.Error("Audio with more than 2 channels is not supported.")
69
+ return None
70
+ else: #re-order channel data from [samples, 2] to [2, samples]
71
+ lrAudio = torch.tensor(audioData.value[1].copy().astype(np.float32)/32768).transpose(0,1)
72
+ if audioData.value[0] != 44100:
73
+ lrAudio = resample(lrAudio, audioData.value[0], 44100)
74
+ model_name, experiment_file = getModelInfo(model.value)
75
+ hrAudio=upscaleAudio(lrAudio, model.value, model_name=model_name, experiment_file=experiment_file)
76
+ hrAudio=hrAudio / max(hrAudio.abs().max().item(), 1)
77
+ outAudio=(hrAudio*32767).numpy().astype(np.int16).transpose(1,0)
78
+ return tuple([44100, outAudio])
79
+
80
+ def getModelInfo(modelFilename: str):
81
+ if(modelFilename == "FM_Radio_SR.th"):
82
+ return "aero", "aero_441-441_512_256.yaml"
83
+ if(modelFilename == "AM_Radio_SR.th"):
84
+ return "aero", "aero_441-441_512_256.yaml"
85
+ if(modelFilename == "Telephone_SR.th"):
86
+ return "aero", "aero_441-441_512_256.yaml"
87
+ return "aero", "aero_441-441_512_256.yaml"
88
+
89
  layout.launch()