Omnibus commited on
Commit
2b55725
·
verified ·
1 Parent(s): f796012

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -65
app.py CHANGED
@@ -35,72 +35,64 @@ def show_s(name,token):
35
  return(gr.update(label="Spaces", choices=[s for s in spaces]))
36
  #gr.update(choices=[s for s in spaces],interactive=True))
37
 
38
- def show_f(repo,name,token):
39
- api = HfApi(token=token)
40
- f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space"))
41
- print (f_ist)
42
- file_list = []
43
- #file_out = []
44
-
45
- if not os.path.exists(name):
46
- os.makedirs(name)
47
- for d_app in f_ist:
48
- if "/" in d_app:
49
- dir_1=d_app.split("/",1)[0]
50
- rem_1=d_app.split("/",1)[1]
51
- if not os.path.exists(f'{name}/{dir_1}'):
52
- os.makedirs(f'{name}/{dir_1}')
53
-
54
- if "/" in rem_1:
55
- dir_2=rem_1.split("/",1)[0]
56
- rem_2=rem_1.split("/",1)[1]
57
- if not os.path.exists(f'{name}/{dir_1}/{dir_2}'):
58
- os.makedirs(f'{name}/{dir_1}/{dir_2}')
59
- #sf=rem_2.split(".",1)[1]
60
- #pf=rem_2.split(".",1)[0]
61
- #f_name=f'{dir_1}/{dir2}/{pf}.{sf}'
62
- f_name=f'{dir_1}/{dir2}/{rem_2}'
63
-
64
  else:
65
- #sf=rem_1.split(".",1)[1]
66
- #pf=rem_1.split(".",1)[0]
67
- #f_name=f'{dir_1}/{pf}.{sf}'
68
- f_name=f'{dir_1}/{rem_1}'
69
- print(f_name)
70
-
71
- else:
72
 
73
- #sf=d_app.split(".",1)[1]
74
- #pf=d_app.split(".",1)[0]
75
- #f_name=f'{pf}.{sf}'
76
- f_name=f'{d_app}'
77
- pass
78
-
79
- r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
80
- print(d_app)
81
- #print (r.text)
82
- uid = uuid.uuid4()
83
- file = open(f'{name}/{f_name}','wb')
84
- #file = open(f'{name}/{f_name}','wb')
85
- file.write(r.content)
86
- file.close()
87
- file_list.append(f'{name}/{f_name}')
88
-
89
- with ZipFile(f"{name}.zip", "w") as zipObj:
90
- for idx, file in enumerate(f_ist):
91
- zipObj.write(f'{name}/{file}')
92
- file_list.append(f'{name}.zip')
93
-
94
- s_info=api.space_info(f'{repo}/{name}',files_metadata=True)
95
- '''
96
- with open(f'{uid}-tmp.json','w') as f:
97
- json.dump(s_info,f,indent=4)
98
- f.close()
99
- '''
100
- return(file_list,
101
- gr.update(choices=[f for f in f_ist],interactive=True),
102
- api.get_space_runtime(f'{repo}/{name}'))
103
-
104
  def show_f_cont(repo,name,file,token):
105
  html_text = '<html>\n<body>\n<div>\n'
106
  images=[".png" , ".jpg" , ".gif" , ".webm" , ".mp4"]
@@ -142,6 +134,7 @@ with gr.Blocks(css=css) as build:
142
  token = gr.Textbox(label="auth")
143
  s_btn = gr.Button("Show Spaces")
144
  with gr.Column(scale=1):
 
145
  files=gr.File(file_count="directory")
146
  with gr.Tab("View 1"):
147
  with gr.Row():
@@ -201,6 +194,6 @@ with gr.Blocks(css=css) as build:
201
 
202
  move_btn.click(move,[r_name,space_radio,dest,token]).then(show_s,[r_name,token],[space_radio])
203
  s_btn.click(show_s,[r_name,token],[space_radio])
204
- space_radio.change(show_f,[r_name,space_radio,token],[files,file_radio,space_info_json])
205
  file_radio.change(show_f_cont,[r_name,space_radio,file_radio,token],[file_contents])
206
  build.queue(default_concurrency_limit=10).launch(show_api=False)
 
35
  return(gr.update(label="Spaces", choices=[s for s in spaces]))
36
  #gr.update(choices=[s for s in spaces],interactive=True))
37
 
38
+ def show_f(repo,name,token,tog):
39
+ if tog:
40
+ api = HfApi(token=token)
41
+ f_ist = (api.list_repo_files(repo_id=f'{repo}/{name}', repo_type="space"))
42
+ print (f_ist)
43
+ file_list = []
44
+ #file_out = []
45
+
46
+ if not os.path.exists(name):
47
+ os.makedirs(name)
48
+ for d_app in f_ist:
49
+ if "/" in d_app:
50
+ dir_1=d_app.split("/",1)[0]
51
+ rem_1=d_app.split("/",1)[1]
52
+ if not os.path.exists(f'{name}/{dir_1}'):
53
+ os.makedirs(f'{name}/{dir_1}')
54
+ if "/" in rem_1:
55
+ dir_2=rem_1.split("/",1)[0]
56
+ rem_2=rem_1.split("/",1)[1]
57
+ if not os.path.exists(f'{name}/{dir_1}/{dir_2}'):
58
+ os.makedirs(f'{name}/{dir_1}/{dir_2}')
59
+ f_name=f'{dir_1}/{dir2}/{rem_2}'
60
+
61
+ else:
62
+ f_name=f'{dir_1}/{rem_1}'
63
+ print(f_name)
64
  else:
65
+ f_name=f'{d_app}'
66
+ pass
 
 
 
 
 
67
 
68
+ r = requests.get(f'https://huggingface.co/spaces/{repo}/{name}/raw/main/{d_app}')
69
+ print(d_app)
70
+ #print (r.text)
71
+ uid = uuid.uuid4()
72
+ file = open(f'{name}/{f_name}','wb')
73
+ #file = open(f'{name}/{f_name}','wb')
74
+ file.write(r.content)
75
+ file.close()
76
+ file_list.append(f'{name}/{f_name}')
77
+
78
+ with ZipFile(f"{name}.zip", "w") as zipObj:
79
+ for idx, file in enumerate(f_ist):
80
+ zipObj.write(f'{name}/{file}')
81
+ file_list.append(f'{name}.zip')
82
+
83
+ s_info=api.space_info(f'{repo}/{name}',files_metadata=True)
84
+ '''
85
+ with open(f'{uid}-tmp.json','w') as f:
86
+ json.dump(s_info,f,indent=4)
87
+ f.close()
88
+ '''
89
+ return(file_list,
90
+ gr.update(choices=[f for f in f_ist],interactive=True),
91
+ api.get_space_runtime(f'{repo}/{name}'))
92
+ else:
93
+ return(None,
94
+ None,
95
+ api.get_space_runtime(f'{repo}/{name}'))
 
 
 
96
  def show_f_cont(repo,name,file,token):
97
  html_text = '<html>\n<body>\n<div>\n'
98
  images=[".png" , ".jpg" , ".gif" , ".webm" , ".mp4"]
 
134
  token = gr.Textbox(label="auth")
135
  s_btn = gr.Button("Show Spaces")
136
  with gr.Column(scale=1):
137
+ tog=gr.Checkbox(label="Show Files",value=True)
138
  files=gr.File(file_count="directory")
139
  with gr.Tab("View 1"):
140
  with gr.Row():
 
194
 
195
  move_btn.click(move,[r_name,space_radio,dest,token]).then(show_s,[r_name,token],[space_radio])
196
  s_btn.click(show_s,[r_name,token],[space_radio])
197
+ space_radio.change(show_f,[r_name,space_radio,token,tog],[files,file_radio,space_info_json])
198
  file_radio.change(show_f_cont,[r_name,space_radio,file_radio,token],[file_contents])
199
  build.queue(default_concurrency_limit=10).launch(show_api=False)