Blane187 commited on
Commit
d2de53f
·
verified ·
1 Parent(s): 2df4229

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +83 -2
app.py CHANGED
@@ -1,4 +1,5 @@
1
- import os
 
2
  import sys
3
  from dotenv import load_dotenv
4
  from applio import *
@@ -7,11 +8,13 @@ sys.path.append(now_dir)
7
  load_dotenv()
8
  load_dotenv("sha256.env")
9
 
 
 
 
10
  if sys.platform == "darwin":
11
  os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
12
 
13
  from infer.modules.vc import VC, show_info, hash_similarity
14
- from infer.modules.uvr5.modules import uvr
15
  from infer.lib.train.process_ckpt import (
16
  change_info,
17
  extract_small_model,
@@ -799,6 +802,84 @@ def change_f0_method(f0method8):
799
  return {"visible": visible, "__type__": "update"}
800
 
801
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
802
 
803
 
804
 
 
1
+ import os, shutil
2
+ import subprocess, glob
3
  import sys
4
  from dotenv import load_dotenv
5
  from applio import *
 
8
  load_dotenv()
9
  load_dotenv("sha256.env")
10
 
11
+
12
+
13
+
14
  if sys.platform == "darwin":
15
  os.environ["PYTORCH_ENABLE_MPS_FALLBACK"] = "1"
16
 
17
  from infer.modules.vc import VC, show_info, hash_similarity
 
18
  from infer.lib.train.process_ckpt import (
19
  change_info,
20
  extract_small_model,
 
802
  return {"visible": visible, "__type__": "update"}
803
 
804
 
805
+ def show_available(filepath,format=None):
806
+ if format:
807
+ print(f"Format: {format}")
808
+ files = []
809
+ for file in os.listdir(filepath):
810
+ if file.endswith(format):
811
+ print(f"Matches format: {file}")
812
+ files.append(file)
813
+ else:
814
+ print(f"Does not match format: {file}")
815
+ print(f"Matches: {files}")
816
+ if len(files) < 1:
817
+ return ['']
818
+ return files
819
+ if len(os.listdir(filepath)) < 1:
820
+ return ['']
821
+ return os.listdir(filepath)
822
+
823
+
824
+ def download_from_url(url, model):
825
+ if model =='':
826
+ try:
827
+ model = url.split('/')[-1].split('?')[0]
828
+ except:
829
+ return "You need to name your model. For example: My-Model", {"choices":show_available("assets/weights"),"__type__":"update"}
830
+ url=url.replace('/blob/main/','/resolve/main/')
831
+ model=model.replace('.pth','').replace('.index','').replace('.zip','')
832
+ print(f"Model name: {model}")
833
+ if url == '':
834
+ return "URL cannot be left empty.", {"choices":show_available("assets/weights"),"__type__":"update"}
835
+ url = url.strip()
836
+ zip_dirs = ["zips", "unzips"]
837
+ for directory in zip_dirs:
838
+ if os.path.exists(directory):
839
+ shutil.rmtree(directory)
840
+ os.makedirs("zips", exist_ok=True)
841
+ os.makedirs("unzips", exist_ok=True)
842
+ zipfile = model + '.zip'
843
+ zipfile_path = './zips/' + zipfile
844
+ try:
845
+ if url.endswith('.pth'):
846
+ subprocess.run(["wget", url, "-O", f'./assets/weights/{model}.pth'])
847
+ return f"Sucessfully downloaded as {model}.pth", {"choices":show_available("assets/weights"),"__type__":"update"}
848
+ elif url.endswith('.index'):
849
+ if not os.path.exists(f'./logs/{model}'): os.makedirs(f'./logs/{model}')
850
+ subprocess.run(["wget", url, "-O", f'./logs/{model}/added_{model}.index'])
851
+ return f"Successfully downloaded as added_{model}.index", {"choices":show_available("assets/weights"),"__type__":"update"}
852
+ if "drive.google.com" in url:
853
+ subprocess.run(["gdown", url, "--fuzzy", "-O", zipfile_path])
854
+ elif "mega.nz" in url:
855
+ m = Mega()
856
+ m.download_url(url, './zips')
857
+ else:
858
+ subprocess.run(["wget", url, "-O", zipfile_path])
859
+ for filename in os.listdir("./zips"):
860
+ if filename.endswith(".zip"):
861
+ zipfile_path = os.path.join("./zips/",filename)
862
+ shutil.unpack_archive(zipfile_path, "./unzips", 'zip')
863
+ for root, dirs, files in os.walk('./unzips'):
864
+ for file in files:
865
+ file_path = os.path.join(root, file)
866
+ if file.endswith(".index"):
867
+ os.mkdir(f'./logs/{model}')
868
+ shutil.copy2(file_path,f'./logs/{model}')
869
+ elif "G_" not in file and "D_" not in file and file.endswith(".pth"):
870
+ shutil.copy(file_path,f'./assets/weights/{model}.pth')
871
+ elif filename.endswith(".pth"):
872
+ shutil.copy2(os.path.join("./zips/",filename),f'./assets/weights/{model}.pth')
873
+ elif filename.endswith(".index"):
874
+ os.mkdir(f'./logs/{model}')
875
+ shutil.copy2(os.path.join("./zips/",filename),f'./logs/{model}/')
876
+ else:
877
+ return "No zipfile found.", {"choices":show_available("assets/weights"),"__type__":"update"}
878
+ shutil.rmtree("zips")
879
+ shutil.rmtree("unzips")
880
+ return "Success.", {"choices":show_available("assets/weights"),"__type__":"update"}
881
+ except:
882
+ return "There's been an error.", {"choices":show_available("assets/weights"),"__type__":"update"}
883
 
884
 
885