emmajoanne commited on
Commit
156b564
1 Parent(s): a1fe125

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -0
app.py ADDED
@@ -0,0 +1,126 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import HfApi
2
+ from huggingface_hub import CommitOperationAdd
3
+ from urllib.parse import urlparse
4
+ import os
5
+ import random
6
+ import requests
7
+ import shutil
8
+ import gradio as gr
9
+
10
+ hd_total, hd_used, hd_free = shutil.disk_usage("/")
11
+ hd_text = "Total " + str(hd_total // (2 ** 30)) + " GiB\nUsed " + str(hd_used // (2 ** 30)) + " GiB\nFree " + str(hd_free // (2 ** 30)) + " GiB"
12
+
13
+ def copy(url, repo, token, override, dest):
14
+ try:
15
+ if not url:
16
+ return "Oops, you forgot the remote URL!"
17
+ if not repo:
18
+ return "Oops, you forgot the repo name!"
19
+ if not token:
20
+ return "Oops, you forgot the token for write access!"
21
+
22
+ # Create download directory
23
+ localPath = os.path.join(os.getcwd(), generateRandomHexString(16))
24
+ os.mkdir(localPath)
25
+
26
+ # Determine filename
27
+ if dest:
28
+ filename = dest
29
+ else:
30
+ filename = getFilename(url)
31
+
32
+ filePath = os.path.join(localPath, filename)
33
+ r = requests.get(url, allow_redirects=True)
34
+ open(filePath, 'wb').write(r.content)
35
+ print('File downloaded as: ' + filePath)
36
+
37
+ # Hf login
38
+ api = getHfApi(token)
39
+ user = api.whoami()
40
+ repoId = user['name'] + '/' + repo
41
+
42
+ # Create the repo if not exist
43
+ api.create_repo(
44
+ repo_id=repoId,
45
+ private=True,
46
+ exist_ok=True
47
+ )
48
+
49
+ # Delete file if already exists on repo and override is set to True
50
+ if fileExistsOnRepo(api, repoId, filename) and override:
51
+ deleteFileOnRepo(api, repoId, filename)
52
+
53
+ # Upload the file
54
+ api.create_commit(
55
+ repo_id=repoId,
56
+ operations=[
57
+ CommitOperationAdd(
58
+ path_in_repo=filename,
59
+ path_or_fileobj=filePath,
60
+ )
61
+ ],
62
+ commit_message='Uploaded from ' + url
63
+ )
64
+ return 'Done!'
65
+ except Exception as e:
66
+ return e
67
+
68
+ def deleteFileOnRepo(api, repoId, filename):
69
+ try:
70
+ api.delete_file(
71
+ repo_id=repoId,
72
+ path_in_repo=filename,
73
+ )
74
+ print('File ' + filename + ' deleted from repo')
75
+ except BaseException as e:
76
+ raise Exception('Failed to delete file ' + filename + ' on repo!')
77
+ print(repr(e))
78
+
79
+ def generateRandomHexString(size):
80
+ return ''.join(random.choices('0123456789abcdef', k=size))
81
+
82
+ def getFilename(url):
83
+ r = requests.head(url, allow_redirects=True)
84
+ u = urlparse(r.url)
85
+ filename = os.path.basename(u.path)
86
+ if len(filename) == 0 or len(filename) > 128:
87
+ return generateRandomHexString(8) + ".to_rename"
88
+ return filename
89
+
90
+ def getHfApi(token):
91
+ try:
92
+ api = HfApi(token=token)
93
+ return api
94
+ except BaseException as e:
95
+ raise Exception('Authorisation error')
96
+ print(repr(e))
97
+
98
+ def fileExistsOnRepo(api, repoId, filename):
99
+ repoFiles = api.list_repo_files(repo_id=repoId)
100
+ for repoFile in repoFiles:
101
+ if (repoFile == filename):
102
+ return True
103
+ return False
104
+
105
+ with gr.Blocks() as app:
106
+ gr.Markdown(hd_text)
107
+
108
+ url = gr.Textbox(label="Remote URL", placeholder="Enter the full remote URL here...")
109
+ repo = gr.Textbox(label="Repo name (model)")
110
+ token = gr.Textbox(label="Write token")
111
+ override = gr.Checkbox(label="Override if same file name exists, otherwise abort")
112
+ destinationName = gr.Textbox(label="File name (include path) to save on the repo. Leave blank if you want to use the name from the URL. (random string will be used if fail to retrieve valid name from the URL")
113
+ submitBtn = gr.Button("Copy Now")
114
+
115
+ submitBtn.click(
116
+ copy,
117
+ [
118
+ url,
119
+ repo,
120
+ token,
121
+ override,
122
+ destinationName,
123
+ ],
124
+ outputs=gr.Textbox(label="Result", lines=4)
125
+ )
126
+ app.launch()