AthuKawaleLogituit commited on
Commit
15dc827
·
verified ·
1 Parent(s): f1d31d5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -14
app.py CHANGED
@@ -1,9 +1,10 @@
1
  import os
2
  import requests
3
  import gradio as gr
4
- from huggingface_hub import Repository
5
 
6
  REPO_ID = "AthuKawaleLogituit/Faceswap"
 
7
  LOCAL_DIR = "models"
8
 
9
  urls = [
@@ -24,22 +25,36 @@ urls = [
24
  "https://huggingface.co/countfloyd/deepfake/resolve/main/lsdir_x4.onnx"
25
  ]
26
 
 
 
 
 
 
 
 
 
 
27
  def download_and_upload():
28
  os.makedirs(LOCAL_DIR, exist_ok=True)
 
29
 
30
  for url in urls:
31
- filename = os.path.join(LOCAL_DIR, os.path.basename(url))
32
- if not os.path.exists(filename):
33
- r = requests.get(url, allow_redirects=True)
34
- with open(filename, 'wb') as f:
35
- f.write(r.content)
36
-
37
- token = os.environ.get("HF_TOKEN")
38
- repo = Repository(local_dir=LOCAL_DIR, clone_from=REPO_ID, use_auth_token=token)
39
- repo.git_add()
40
- repo.git_commit("Upload model files from Gradio Space")
41
- repo.git_push()
42
-
43
- return "✅ Models downloaded and pushed to Faceswap repo."
 
 
 
 
44
 
45
  gr.Interface(fn=download_and_upload, inputs=[], outputs="text", title="Upload Faceswap Models").launch()
 
1
  import os
2
  import requests
3
  import gradio as gr
4
+ from huggingface_hub import upload_file, HfFolder
5
 
6
  REPO_ID = "AthuKawaleLogituit/Faceswap"
7
+ TOKEN = os.environ.get("HF_TOKEN")
8
  LOCAL_DIR = "models"
9
 
10
  urls = [
 
25
  "https://huggingface.co/countfloyd/deepfake/resolve/main/lsdir_x4.onnx"
26
  ]
27
 
28
+ def download_file(url, filepath):
29
+ print(f"Downloading {url}")
30
+ with requests.get(url, stream=True, allow_redirects=True) as r:
31
+ r.raise_for_status()
32
+ with open(filepath, 'wb') as f:
33
+ for chunk in r.iter_content(chunk_size=8192):
34
+ if chunk:
35
+ f.write(chunk)
36
+
37
  def download_and_upload():
38
  os.makedirs(LOCAL_DIR, exist_ok=True)
39
+ uploaded_files = []
40
 
41
  for url in urls:
42
+ filename = os.path.basename(url)
43
+ local_path = os.path.join(LOCAL_DIR, filename)
44
+
45
+ if not os.path.exists(local_path):
46
+ download_file(url, local_path)
47
+
48
+ # Upload to repo
49
+ upload_file(
50
+ path_or_fileobj=local_path,
51
+ path_in_repo=filename,
52
+ repo_id=REPO_ID,
53
+ repo_type="model",
54
+ token=TOKEN
55
+ )
56
+ uploaded_files.append(filename)
57
+
58
+ return f"✅ Uploaded files: {uploaded_files}"
59
 
60
  gr.Interface(fn=download_and_upload, inputs=[], outputs="text", title="Upload Faceswap Models").launch()