Kangarroar commited on
Commit
270269e
1 Parent(s): 4a634bc

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +123 -0
app.py ADDED
@@ -0,0 +1,123 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ import matplotlib.pyplot as plt
5
+ import json
6
+ import os
7
+ import tempfile
8
+ import shutil
9
+ import requests
10
+ from pathlib import Path
11
+ temp_dir = os.path.expanduser("~/app")
12
+ global ckpt_temp_file
13
+ global audio_temp_file
14
+ global config_temp_file
15
+ ###################################################
16
+ from utils.hparams import hparams
17
+ from preprocessing.data_gen_utils import get_pitch_parselmouth,get_pitch_crepe
18
+ import numpy as np
19
+ import matplotlib.pyplot as plt
20
+ import IPython.display as ipd
21
+ import utils
22
+ import librosa
23
+ import torchcrepe
24
+ from infer import *
25
+ import logging
26
+ from infer_tools.infer_tool import *
27
+ import io
28
+
29
+ clip_completed = False
30
+ def render_audio(ckpt_temp_file, config_temp_file, audio_temp_file, title):
31
+ logging.getLogger('numba').setLevel(logging.WARNING)
32
+ title = int(title)
33
+ project_name = "Unnamed"
34
+ model_path = ckpt_temp_file
35
+ config_path= config_temp_file
36
+ hubert_gpu=True
37
+ svc_model = Svc(project_name,config_path,hubert_gpu, model_path)
38
+ print('model loaded')
39
+ wav_fn = audio_temp_file
40
+ demoaudio, sr = librosa.load(wav_fn)
41
+ key = title # 音高调整,支持正负(半音)
42
+ # 加速倍数
43
+ pndm_speedup = 20
44
+ wav_gen='queeeeee.wav'#直接改后缀可以保存不同格式音频,如flac可无损压缩
45
+
46
+ # Show the spinner and run the run_clip function inside the 'with' block
47
+ with st.spinner("Rendering Audio..."):
48
+ f0_tst, f0_pred, audio = run_clip(svc_model,file_path=wav_fn, key=key, acc=pndm_speedup, use_crepe=True, use_pe=True, thre=0.05,
49
+ use_gt_mel=False, add_noise_step=500,project_name=project_name,out_path=wav_gen)
50
+ clip_completed = True
51
+ if clip_completed:
52
+ # If the 'run_clip' function has completed, use the st.audio function to show an audio player for the file stored in the 'wav_gen' variable
53
+ st.audio(wav_gen)
54
+
55
+ #######################################################
56
+ st.set_page_config(
57
+ page_title="DiffSVC Render",
58
+ page_icon="🧊",
59
+ initial_sidebar_state="expanded",
60
+ )
61
+ ############
62
+ st.title('DIFF-SVC Render')
63
+
64
+ ###CKPT LOADER
65
+ with tempfile.TemporaryDirectory(dir=os.path.expanduser("~/app")) as temp_dir:
66
+ ckpt = st.file_uploader("Choose your CKPT", type= 'ckpt')
67
+ # Check if user uploaded a CKPT file
68
+ if ckpt is not None:
69
+ #TEMP FUNCTION
70
+ with tempfile.NamedTemporaryFile(mode="wb", suffix='.ckpt', delete=False, dir=temp_dir) as temp:
71
+ # Get the file contents as bytes
72
+ bytes_data = ckpt.getvalue()
73
+ # Write the bytes to the temporary file
74
+ temp.write(bytes_data)
75
+ ckpt_temp_file = temp.name
76
+ # Print the temporary file name
77
+ print(temp.name)
78
+
79
+ # Display the file path
80
+ if "ckpt_temp_file" in locals():
81
+ st.success("File saved to: {}".format(ckpt_temp_file))
82
+
83
+ # File uploader
84
+ config = st.file_uploader("Choose your config", type= 'yaml')
85
+
86
+ # Check if user uploaded a config file
87
+ if config is not None:
88
+ #TEMP FUNCTION
89
+ with tempfile.NamedTemporaryFile(mode="wb", suffix='.yaml', delete=False) as temp:
90
+ # Get the file contents as bytes
91
+ bytes_data = config.getvalue()
92
+ # Write the bytes to the temporary file
93
+ temp.write(bytes_data.decode())
94
+ config_temp_file = temp.name
95
+ # Print the temporary file name
96
+ print(temp.name)
97
+
98
+ # Display the file path
99
+ if "config_temp_file" in locals():
100
+ st.success("File saved to: {}".format(config_temp_file))
101
+
102
+ audio = st.file_uploader("Choose your audio", type=["wav", "mp3"])
103
+
104
+ # Check if user uploaded an audio file
105
+ if audio is not None:
106
+ #TEMP FUNCTION
107
+ with tempfile.NamedTemporaryFile(mode="wb", suffix='.wav', delete=False, dir=temp_dir) as temp:
108
+ # Get the file contents as bytes
109
+ bytes_data = audio.getvalue()
110
+ # Write the bytes to the temporary file
111
+ temp.write(bytes_data)
112
+ audio_temp_file = temp.name
113
+ # Print the temporary file name
114
+ print(temp.name)
115
+
116
+ # Display the file path
117
+ if "audio_temp_file" in locals():
118
+ st.success("File saved to: {}".format(audio_temp_file))
119
+ # Add a text input for the title with a default value of 0
120
+ title = st.text_input("Key", value="0")
121
+ # Add a button to start the rendering process
122
+ if st.button("Render audio"):
123
+ render_audio(ckpt_temp_file, config_temp_file, audio_temp_file, title)