Update for gradio 6.3.0
Browse files
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 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
return gr.update(interactive=
|
| 48 |
-
if audioData[1].shape
|
| 49 |
-
gr.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 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()
|