Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -52,6 +52,55 @@ def get_all_files(owner, repo, path="", is_hf=False):
|
|
52 |
except Exception as e:
|
53 |
return None
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
def get_repo_contents(url):
|
56 |
"""Parse URL and fetch repository contents."""
|
57 |
try:
|
@@ -67,7 +116,7 @@ def get_repo_contents(url):
|
|
67 |
# Add more known paths or implement HTML scraping if needed
|
68 |
]
|
69 |
#files = get_all_files(owner, repo, "", True) or known_files
|
70 |
-
files =
|
71 |
return owner, repo, files, True
|
72 |
else: # Assume GitHub URL
|
73 |
parts = url.rstrip('/').split('/')
|
|
|
52 |
except Exception as e:
|
53 |
return None
|
54 |
|
55 |
+
def get_hf_files(repo,name):
|
56 |
+
api = HfApi()
|
57 |
+
f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space"))
|
58 |
+
print (f_ist)
|
59 |
+
file_list = []
|
60 |
+
#file_out = []
|
61 |
+
|
62 |
+
if not os.path.exists(name):
|
63 |
+
os.makedirs(name)
|
64 |
+
for d_app in f_ist:
|
65 |
+
if "/" in d_app:
|
66 |
+
dir_1=d_app.split("/",1)[0]
|
67 |
+
rem_1=d_app.split("/",1)[1]
|
68 |
+
if not os.path.exists(f'{name}/{dir_1}'):
|
69 |
+
os.makedirs(f'{name}/{dir_1}')
|
70 |
+
|
71 |
+
if "/" in rem_1:
|
72 |
+
dir_2=rem_1.split("/",1)[0]
|
73 |
+
rem_2=rem_1.split("/",1)[1]
|
74 |
+
if not os.path.exists(f'{name}/{dir_1}/{dir_2}'):
|
75 |
+
os.makedirs(f'{name}/{dir_1}/{dir_2}')
|
76 |
+
sf=rem_2.split(".",1)[1]
|
77 |
+
pf=rem_2.split(".",1)[0]
|
78 |
+
f_name=f'{dir_1}/{dir2}/{pf}.{sf}'
|
79 |
+
|
80 |
+
else:
|
81 |
+
sf=rem_1.split(".",1)[1]
|
82 |
+
pf=rem_1.split(".",1)[0]
|
83 |
+
f_name=f'{dir_1}/{pf}.{sf}'
|
84 |
+
print(f_name)
|
85 |
+
|
86 |
+
else:
|
87 |
+
|
88 |
+
sf=d_app.split(".",1)[1]
|
89 |
+
pf=d_app.split(".",1)[0]
|
90 |
+
f_name=f'{pf}.{sf}'
|
91 |
+
pass
|
92 |
+
|
93 |
+
r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
|
94 |
+
print(d_app)
|
95 |
+
#print (r.text)
|
96 |
+
uid = uuid.uuid4()
|
97 |
+
file = open(f'{name}/{f_name}','wb')
|
98 |
+
file.write(r.content)
|
99 |
+
file.close()
|
100 |
+
file_list.append(Path(f'{name}/{f_name}'))
|
101 |
+
print(file_list)
|
102 |
+
return file_list
|
103 |
+
|
104 |
def get_repo_contents(url):
|
105 |
"""Parse URL and fetch repository contents."""
|
106 |
try:
|
|
|
116 |
# Add more known paths or implement HTML scraping if needed
|
117 |
]
|
118 |
#files = get_all_files(owner, repo, "", True) or known_files
|
119 |
+
files = get_hf_files(owner, repo)
|
120 |
return owner, repo, files, True
|
121 |
else: # Assume GitHub URL
|
122 |
parts = url.rstrip('/').split('/')
|