Spaces:
Sleeping
Sleeping
Update player.py
Browse files
player.py
CHANGED
@@ -76,48 +76,144 @@ def show_vid(vid_file,list_hid):
|
|
76 |
</video>'''
|
77 |
return label_html
|
78 |
|
79 |
-
|
80 |
-
|
81 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
try:
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
json_out=r.json()
|
102 |
-
label_html=f'{label_html}{json_out["label"]}<br>{json_out["description"]}<br>'
|
103 |
-
print (r.json())
|
104 |
-
if vid_file.endswith(".mp4"):
|
105 |
-
#<source src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/videos/{vid_file}" type="video/mp4">
|
106 |
-
print(vid_file)
|
107 |
-
label_html=f'''{label_html}
|
108 |
-
<video src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/{vid_file}" controls>
|
109 |
-
</video>'''
|
110 |
-
#uid = uuid.uuid4()
|
111 |
-
#file = open(f'{name}/{f_name}','wb')
|
112 |
-
#file.write(r.content)
|
113 |
-
except Exception as e:
|
114 |
-
print (f'error:: {e}')
|
115 |
-
pass
|
116 |
-
except Exception:
|
117 |
-
pass
|
118 |
except Exception as e:
|
119 |
-
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
with gr.Blocks() as app:
|
122 |
with gr.Row():
|
123 |
channel = gr.Dropdown()
|
|
|
76 |
</video>'''
|
77 |
return label_html
|
78 |
|
79 |
+
##############################################
|
80 |
+
|
81 |
+
def build_space(t_space,t_title,t_description=None,t_name=None,t_redirect=None,t_image=None,t_image_url=None,token=None):
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
|
86 |
+
if t_description == None or t_description == "":
|
87 |
+
t_description = f'Embedded App'
|
88 |
+
if t_redirect == None or t_redirect =="":
|
89 |
+
t_redirect = t_space
|
90 |
+
if t_name == None or t_name =="":
|
91 |
+
print (t_space)
|
92 |
+
t_name = t_space.split("//",1)[1]
|
93 |
+
print (t_name)
|
94 |
+
t_name = t_name.strip(".hf.space")
|
95 |
+
print (f't_name::{t_name}')
|
96 |
+
|
97 |
+
|
98 |
+
model_id=t_name
|
99 |
+
#model_id="omnibus/fff"
|
100 |
+
if token==None or token=="":
|
101 |
+
token = token_self
|
102 |
+
else:
|
103 |
+
token = token
|
104 |
+
pass
|
105 |
+
api = HfApi(token=token)
|
106 |
+
repo_name = get_full_repo_name(model_id=model_id, token=token)
|
107 |
+
|
108 |
try:
|
109 |
+
print(t_space.rsplit('.hf', 1)[-1])
|
110 |
+
if t_space.rsplit('.hf', 1)[-1] == ".space":
|
111 |
+
repo_url = api.create_repo(
|
112 |
+
repo_id=model_id,
|
113 |
+
repo_type="space",
|
114 |
+
space_sdk="static",
|
115 |
+
private=False,
|
116 |
+
)
|
117 |
+
print(f"""Space Built at {repo_name}""")
|
118 |
+
else:
|
119 |
+
model_id=None
|
120 |
+
repo_name=None
|
121 |
+
t_space=None
|
122 |
+
t_image=None
|
123 |
+
t_image_url=None
|
124 |
+
api=None
|
125 |
+
pass
|
126 |
+
return gr.HTML.update(f"""Invalid Direct URL: must be a Huggingface Space<br>Be sure URL is in lowercase""")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
127 |
except Exception as e:
|
128 |
+
return gr.HTML(f"""{str(e)}""")
|
129 |
+
|
130 |
+
if t_image_url != None and t_image_url != "" and t_image==None:
|
131 |
+
t_image=t_image_url
|
132 |
+
print(f"Using image URL {t_image}")
|
133 |
+
pass
|
134 |
+
elif t_image != None:
|
135 |
+
img1 = Image.open(t_image)
|
136 |
+
img1.thumbnail((500,500), Image.Resampling.LANCZOS)
|
137 |
+
img1.save(f'tmpim-{uid}.png')
|
138 |
+
output_pro=cv2.imread(f'tmpim-{uid}.png')
|
139 |
+
output_pro2=cv2.imread(f'tmpim2-{uid}.png')
|
140 |
+
|
141 |
+
cv2.imwrite(f"./img-{uid}.png",output_pro)
|
142 |
+
cv2.imwrite(f"./img2-{uid}.png",output_pro2)
|
143 |
+
try:
|
144 |
+
api.upload_file(
|
145 |
+
path_or_fileobj=f"./img-{uid}.png",
|
146 |
+
path_in_repo="card_im.png",
|
147 |
+
repo_id=repo_name,
|
148 |
+
token=token,
|
149 |
+
repo_type="space",
|
150 |
+
)
|
151 |
+
t_image = f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/card_im.png"
|
152 |
+
os.remove(f"./img-{uid}.png")
|
153 |
+
|
154 |
+
print(f"Image Uploaded to: {t_image}")
|
155 |
+
except Exception as e:
|
156 |
+
return gr.HTML(f"""{str(e)}""")
|
157 |
+
try:
|
158 |
+
api.upload_file(
|
159 |
+
path_or_fileobj=f"./img2-{uid}.png",
|
160 |
+
path_in_repo="card_im_crop.png",
|
161 |
+
repo_id=repo_name,
|
162 |
+
token=token,
|
163 |
+
repo_type="space",
|
164 |
+
)
|
165 |
+
t_image2 = f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/card_im_crop.png"
|
166 |
+
os.remove(f"./img2-{uid}.png")
|
167 |
+
|
168 |
+
print(f"Image Uploaded to: {t_image2}")
|
169 |
+
except Exception as e:
|
170 |
+
return gr.HTML(f"""{str(e)}""")
|
171 |
+
|
172 |
+
pass
|
173 |
+
else:
|
174 |
+
print("Default Image")
|
175 |
+
t_image="https://huggingface.co/spaces/portal/bin/resolve/main/ai_demo_card.png"
|
176 |
+
pass
|
177 |
+
|
178 |
+
try:
|
179 |
+
api_url = f'https://huggingface.co/api/spaces/{model_id}'
|
180 |
+
t_iframe = t_space
|
181 |
+
t_link = f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/ai.html"
|
182 |
+
|
183 |
+
########## norm
|
184 |
+
with open("template/ai.html", "r") as f:
|
185 |
+
app = f.read()
|
186 |
+
app = app.replace("$space", t_space)
|
187 |
+
app = app.replace("$title", t_title)
|
188 |
+
app = app.replace("$description", t_description)
|
189 |
+
app = app.replace("$image", t_image)
|
190 |
+
app = app.replace("$redirect", t_redirect)
|
191 |
+
|
192 |
+
with open("ai.html", "w") as f:
|
193 |
+
f.write(app)
|
194 |
+
|
195 |
+
api.upload_file(
|
196 |
+
path_or_fileobj="ai.html",
|
197 |
+
path_in_repo="ai.html",
|
198 |
+
repo_id=repo_name,
|
199 |
+
token=token,
|
200 |
+
repo_type="space",
|
201 |
+
)
|
202 |
+
os.remove("ai.html")
|
203 |
+
|
204 |
+
|
205 |
+
return gr.HTML(f'''<center>Your Interactive Twitter Card Embed Link is:<br><a href="{t_link}" target="_blank">{t_link}</a><br>
|
206 |
+
Your Interactive Mastodon Card Embed Link is:<br><a href="{t_link3}" target="_blank">{t_link3}</a><br>
|
207 |
+
Your Interactive Gab Card Embed Link is:<br><a href="{t_link2}" target="_blank">{t_link2}</a><br>
|
208 |
+
<center><br>''')
|
209 |
+
except Exception as e:
|
210 |
+
return gr.HTML(f"""{str(e)}""")
|
211 |
+
|
212 |
+
|
213 |
+
#########################################
|
214 |
+
|
215 |
+
|
216 |
+
|
217 |
with gr.Blocks() as app:
|
218 |
with gr.Row():
|
219 |
channel = gr.Dropdown()
|