Omnibus commited on
Commit
7f8ba04
·
0 Parent(s):

Duplicate from Omnibus/Spaces-on-Twitter-test2

Browse files
.gitattributes ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tflite filter=lfs diff=lfs merge=lfs -text
29
+ *.tgz filter=lfs diff=lfs merge=lfs -text
30
+ *.wasm filter=lfs diff=lfs merge=lfs -text
31
+ *.xz filter=lfs diff=lfs merge=lfs -text
32
+ *.zip filter=lfs diff=lfs merge=lfs -text
33
+ *.zst filter=lfs diff=lfs merge=lfs -text
34
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
README.md ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ title: Interactive Spaces on Twitter
3
+ emoji: ⚡
4
+ colorFrom: blue
5
+ colorTo: indigo
6
+ sdk: gradio
7
+ sdk_version: 3.19.1
8
+ app_file: app.py
9
+ duplicated_from: Omnibus/Spaces-on-Twitter-test2
10
+ ---
11
+
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,299 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import sys
3
+ import cv2
4
+ import uuid
5
+ from PIL import Image
6
+ import gradio as gr
7
+ from pathlib import Path
8
+ from huggingface_hub import (create_repo,get_full_repo_name,upload_file,CommitOperationAdd,HfApi)
9
+ uid = uuid.uuid4()
10
+
11
+ token_self = os.environ['HF_TOKEN']
12
+
13
+
14
+
15
+ def slide_fn(val):
16
+ im =(f'tmpim22-{uid}.png')
17
+
18
+ v=int(val)
19
+ img1 = Image.open(im)
20
+ #img1.thumbnail((500,500), Image.Resampling.LANCZOS)
21
+ #ww=500
22
+ #hh=int(500*0.5625)
23
+ img2 = img1.crop((0,0+v,500,280+v))
24
+ img2.save(f'tmpim2-{uid}.png')
25
+ return img2
26
+
27
+
28
+ def build_space(t_name,t_space,t_title,t_description,t_redirect,t_image=None,t_image_url=None,token=None):
29
+ model_id=t_name
30
+ #model_id="omnibus/fff"
31
+ if token==None or token=="":
32
+ token = token_self
33
+ else:
34
+ token = token
35
+ pass
36
+ api = HfApi(token=token)
37
+ repo_name = get_full_repo_name(model_id=model_id, token=token)
38
+
39
+ try:
40
+ print(t_space.rsplit('.hf', 1)[-1])
41
+ if t_space.rsplit('.hf', 1)[-1] == ".space":
42
+ repo_url = api.create_repo(
43
+ repo_id=model_id,
44
+ repo_type="space",
45
+ space_sdk="static",
46
+ private=False,
47
+ )
48
+ print(f"""Space Built at {repo_name}""")
49
+ else:
50
+ model_id=None
51
+ repo_name=None
52
+ t_space=None
53
+ t_image=None
54
+ t_image_url=None
55
+ api=None
56
+ pass
57
+ return gr.HTML.update(f"""Invalid Direct URL: must be a Huggingface Space<br>Be sure URL is in lowercase""")
58
+ except Exception as e:
59
+ return gr.HTML.update(f"""{str(e)}""")
60
+
61
+ if t_image_url != None and t_image_url != "" and t_image==None:
62
+ t_image=t_image_url
63
+ print(f"Using image URL {t_image}")
64
+ pass
65
+ elif t_image != None:
66
+ img1 = Image.open(t_image)
67
+ img1.thumbnail((500,500), Image.Resampling.LANCZOS)
68
+ img1.save(f'tmpim-{uid}.png')
69
+ output_pro=cv2.imread(f'tmpim-{uid}.png')
70
+ output_pro2=cv2.imread(f'tmpim2-{uid}.png')
71
+
72
+ cv2.imwrite(f"./img-{uid}.png",output_pro)
73
+ cv2.imwrite(f"./img2-{uid}.png",output_pro2)
74
+ try:
75
+ api.upload_file(
76
+ path_or_fileobj=f"./img-{uid}.png",
77
+ path_in_repo="card_im.png",
78
+ repo_id=repo_name,
79
+ token=token,
80
+ repo_type="space",
81
+ )
82
+ t_image = f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/card_im.png"
83
+ os.remove(f"./img-{uid}.png")
84
+
85
+ print(f"Image Uploaded to: {t_image}")
86
+ except Exception as e:
87
+ return gr.HTML.update(f"""{str(e)}""")
88
+ try:
89
+ api.upload_file(
90
+ path_or_fileobj=f"./img2-{uid}.png",
91
+ path_in_repo="card_im_crop.png",
92
+ repo_id=repo_name,
93
+ token=token,
94
+ repo_type="space",
95
+ )
96
+ t_image2 = f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/card_im_crop.png"
97
+ os.remove(f"./img2-{uid}.png")
98
+
99
+ print(f"Image Uploaded to: {t_image2}")
100
+ except Exception as e:
101
+ return gr.HTML.update(f"""{str(e)}""")
102
+
103
+ pass
104
+ else:
105
+ print("Default Image")
106
+ t_image="https://huggingface.co/spaces/portal/bin/resolve/main/ai_demo_card.png"
107
+ pass
108
+
109
+ try:
110
+ api_url = f'https://huggingface.co/api/spaces/{model_id}'
111
+ t_iframe = t_space
112
+ t_link3= f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/masto.html"
113
+ t_link2= f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/gab.html"
114
+ t_link = f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/ai.html"
115
+ t_space = f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/back.html"
116
+ t_space2 = f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/back_gab.html"
117
+
118
+
119
+ ########## norm
120
+ with open("template/ai.html", "r") as f:
121
+ app = f.read()
122
+ app = app.replace("$space", t_space)
123
+ app = app.replace("$title", t_title)
124
+ app = app.replace("$description", t_description)
125
+ app = app.replace("$image", t_image)
126
+ app = app.replace("$redirect", t_redirect)
127
+
128
+ with open("ai.html", "w") as f:
129
+ f.write(app)
130
+
131
+ api.upload_file(
132
+ path_or_fileobj="ai.html",
133
+ path_in_repo="ai.html",
134
+ repo_id=repo_name,
135
+ token=token,
136
+ repo_type="space",
137
+ )
138
+ os.remove("ai.html")
139
+ ######## gab
140
+ with open("template/ai.html", "r") as f:
141
+ app = f.read()
142
+ app = app.replace("$space", t_space2)
143
+ app = app.replace("$title", t_title)
144
+ app = app.replace("$description", t_description)
145
+ app = app.replace("$image", t_image2)
146
+ app = app.replace("$redirect", t_redirect)
147
+
148
+ with open("gab.html", "w") as f:
149
+ f.write(app)
150
+
151
+ api.upload_file(
152
+ path_or_fileobj="gab.html",
153
+ path_in_repo="gab.html",
154
+ repo_id=repo_name,
155
+ token=token,
156
+ repo_type="space",
157
+ )
158
+ os.remove("gab.html")
159
+ ######## masto
160
+ with open("template/ai.html", "r") as f:
161
+ app = f.read()
162
+ app = app.replace("$space", t_space)
163
+ app = app.replace("$title", t_title)
164
+ app = app.replace("$description", t_description)
165
+ app = app.replace("$image", t_image2)
166
+ app = app.replace("$redirect", t_redirect)
167
+
168
+ with open("masto.html", "w") as f:
169
+ f.write(app)
170
+
171
+ api.upload_file(
172
+ path_or_fileobj="masto.html",
173
+ path_in_repo="masto.html",
174
+ repo_id=repo_name,
175
+ token=token,
176
+ repo_type="space",
177
+ )
178
+ os.remove("masto.html")
179
+
180
+ with open("template/front.html", "r") as f:
181
+ app = f.read()
182
+ app = app.replace("$iframe", t_iframe)
183
+
184
+ with open("front.html", "w") as f:
185
+ f.write(app)
186
+
187
+ api.upload_file(
188
+ path_or_fileobj="front.html",
189
+ path_in_repo="index.html",
190
+ repo_id=repo_name,
191
+ token=token,
192
+ repo_type="space",
193
+ )
194
+ os.remove("front.html")
195
+
196
+ ######### norm
197
+ with open("template/back.html", "r") as f:
198
+ app = f.read()
199
+ app = app.replace("$iframe", t_iframe)
200
+ app = app.replace("$pad", "100%")
201
+ # 3. save the new app.py file
202
+ with open("back.html", "w") as f:
203
+ f.write(app)
204
+
205
+ api.upload_file(
206
+ path_or_fileobj="back.html",
207
+ path_in_repo="back.html",
208
+ repo_id=repo_name,
209
+ token=token,
210
+ repo_type="space",
211
+ )
212
+ os.remove("back.html")
213
+ ####### gab
214
+ with open("template/back.html", "r") as f:
215
+ app = f.read()
216
+ app = app.replace("$iframe", t_iframe)
217
+ app = app.replace("$pad", "56%")
218
+ # 3. save the new app.py file
219
+ with open("back_gab.html", "w") as f:
220
+ f.write(app)
221
+
222
+ api.upload_file(
223
+ path_or_fileobj="back_gab.html",
224
+ path_in_repo="back_gab.html",
225
+ repo_id=repo_name,
226
+ token=token,
227
+ repo_type="space",
228
+ )
229
+ os.remove("back_gab.html")
230
+
231
+ return gr.HTML.update(f'''<center>Your Interactive Twitter Card Embed Link is:<br><a href="{t_link}" target="_blank">{t_link}</a><br>
232
+ Your Interactive Mastodon Card Embed Link is:<br><a href="{t_link3}" target="_blank">{t_link3}</a><br>
233
+ Your Interactive Gab Card Embed Link is:<br><a href="{t_link2}" target="_blank">{t_link2}</a><br>
234
+ <center><br>''')
235
+ except Exception as e:
236
+ return gr.HTML.update(f"""{str(e)}""")
237
+ def d_im():
238
+ output="https://huggingface.co/spaces/portal/bin/resolve/main/ai_demo_card.png"
239
+ return output
240
+ def up_c(im,c_slider):
241
+ img1 = Image.open(im)
242
+ img1.thumbnail((500,500), Image.Resampling.LANCZOS)
243
+ img1.save(f'tmpim22-{uid}.png')
244
+ out = slide_fn(c_slider)
245
+ return out
246
+ with gr.Blocks() as build_app:
247
+ gr.Markdown("""<center><h1>Interactive Twitter Card Builder</h1></center>""")
248
+
249
+ with gr.Row():
250
+ gr.Column(scale=1)
251
+ with gr.Column(scale=3):
252
+ with gr.Accordion("Details",open=False):
253
+ with gr.Tab("Description"):
254
+ gr.Markdown("""<h7>This space will build an Interactive Twitter Card<br>
255
+ where Twitter (and other) users can run your Gradio demo<br>
256
+ right on their Social Media feed.<br><br>
257
+ *Only works through browser, not Twitter App<br><br>
258
+ Enter the required information and paste the link that is returned<br>
259
+ as the only link, along with your message and tags, in your Tweet.<br>
260
+ When posted, the Interactive Twitter Card should have a Play button<br>
261
+ which will display the Space you provided.<br><br>
262
+ Twitter will remember this card associated with the link you post,<br>
263
+ so any changes will often require a differently named link.<br><br>
264
+ You can test your link first by omitting your Write token<br>
265
+ which will assign the default REPO: "portal"<br>(Link: portal-NAME.hf.space)<br><br>
266
+ This Space is a Demo, and is a Work in Progress<br>
267
+ No Warranties, Guarantees, or Permissions implied
268
+ """)
269
+ with gr.Tab("Credits"):
270
+ gr.Markdown("""<h7><a href='https://huggingface.co/spaces/anzorq/sd-space-creator'>anzorq/sd-space-creator</a><br>""")
271
+
272
+
273
+ with gr.Box():
274
+ option_token = gr.Textbox(label="HF Write Token to write to your REPO", placeholder="Optional")
275
+ t_name = gr.Textbox(label="NAME (Link will be: repo-NAME.hf.space)")
276
+ t_space = gr.Textbox(label="Direct URL to your Space (https://you-repo-you-space.hf.space)")
277
+ t_title = gr.Textbox(label="Title for Twitter Card")
278
+ t_description = gr.Textbox(label="Short Description for Twitter Card")
279
+ t_redirect = gr.Textbox(label="Redirect URL for Twitter Card Click")
280
+ gr.Markdown("""Image for your Card (min: 300x300 [Square]""")
281
+ t_image_url = gr.Textbox(label="Image URL", placeholder="Optional URL Input")
282
+ t_image = gr.Image(label="Twitter Card Image", type="filepath",interactive=True)
283
+ with gr.Row():
284
+ c_image=gr.Image(label="Cropped Image", type="filepath",interactive=False)
285
+ with gr.Column():
286
+ c_slider=gr.Slider(0,220,value=0,step=1,label="Adjust Vertical Crop")
287
+ c_upbtn=gr.Button("Update Crop")
288
+ with gr.Row():
289
+ input_button = gr.Button()
290
+ def_im = gr.Button("Load Default Image")
291
+ output_html = gr.HTML("""""")
292
+ gr.Column(scale=1)
293
+
294
+
295
+ t_image.change(up_c,[t_image,c_slider],c_image)
296
+ c_upbtn.click(slide_fn,c_slider,c_image)
297
+ input_button.click(build_space,[t_name,t_space,t_title,t_description,t_redirect,t_image,t_image_url,option_token],output_html)
298
+ def_im.click(d_im,None,t_image)
299
+ build_app.queue(concurrency_count=10).launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ opencv-python
sauce.css ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #component-1{
2
+ color:white;
3
+ }
4
+ #component-5{
5
+ background:linear-gradient(337deg, #1f2937, #324974);
6
+ color:white;
7
+ }
8
+ #component-6{
9
+ color:white;
10
+ }
11
+ #component-7{
12
+ background:#3d5a8f;
13
+ color:white;
14
+ }
15
+ #component-8{
16
+ background:linear-gradient(90deg, #00000038, #6495ed26);
17
+ color:white;
18
+ }
19
+ #component-9{
20
+ background:linear-gradient(90deg, #00000038, #6495ed26);
21
+ color:white;
22
+ }
23
+ #component-10{
24
+ background:linear-gradient(90deg, #00000038, #6495ed26);
25
+ color:white;
26
+ }
27
+ #component-11{
28
+ background:linear-gradient(90deg, #00000038, #6495ed26);
29
+ color:white;
30
+ }
31
+ #component-12{
32
+ background:linear-gradient(90deg, #00000038, #6495ed26);
33
+ color:white;
34
+ }
35
+ #component-13{
36
+ background:linear-gradient(90deg, #00000038, #6495ed26);
37
+ color:white;
38
+ }
39
+ #component-14{
40
+ color:white;
41
+ }
42
+ #component-15{
43
+ background:linear-gradient(90deg, #00000038, #6495ed26);
44
+ color:white;
45
+ }
46
+ #component-16{
47
+ background:#3a588f;
48
+ color:white;
49
+ }
50
+ #component-18{
51
+ background:radial-gradient(#6495ed26,#00000091);
52
+ color:white;
53
+ }
54
+ #component-19{
55
+ background:radial-gradient(#6495ed26,#00000091);
56
+ color:white;
57
+ }
58
+ .gradio-container-3-19-1 .prose *{
59
+ color:white;
60
+ }
61
+ .app.svelte-1cuuugq>.main.svelte-1cuuugq{
62
+ background:radial-gradient(circle farthest-side, #c5d6f5, #324975);
63
+ }
64
+ .gradio-container-3-19-1 .prose h1{
65
+ color:white;
66
+ }
67
+ .wrap.svelte-1hmcgg5{
68
+ color:white;
69
+ }
70
+ span.svelte-1l2rj76{
71
+ color:white;
72
+ }
73
+ div.svelte-1fwqiwq{
74
+ border:none;
75
+ }
76
+ div.svelte-1fwqiwq{
77
+ background:none;
78
+ }
79
+ .gradio-container-3-19-1 .prose>*:first-child{
80
+ color:white;
81
+ }
82
+ footer.svelte-t2gwjj.svelte-t2gwjj.svelte-t2gwjj{
83
+ color:white;
84
+ }
template/Default-AI.png ADDED
template/ai.html ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width" />
6
+ <meta name="twitter:card" content="player"/>
7
+ <meta name="twitter:site" content=""/>
8
+ <meta name="twitter:player" content="$space"/>
9
+ <meta name="twitter:player:stream" content="$space"/>
10
+ <meta name="twitter:player:width" content="100%"/>
11
+ <meta name="twitter:player:height" content="100%"/>
12
+ <meta property="og:title" content="$title"/>
13
+ <meta property="og:description" content="$description"/>
14
+ <meta property="og:image" content="$image"/>
15
+ <meta http-equiv="refresh" content="0; url=$redirect">
16
+ </head>
17
+ <body>
18
+ </body>
19
+ </html>
template/ai_demo_card.png ADDED
template/back.html ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+
5
+ <style>
6
+ .container {
7
+ position: relative;
8
+ overflow: hidden;
9
+ width: 100%;
10
+ padding-top: $pad; /* 16:9 Aspect Ratio (divide 9 by 16 = 0.5625) */
11
+ }
12
+ .responsive-iframe {
13
+ position: absolute;
14
+ top: 0;
15
+ left: 0;
16
+ bottom: 0;
17
+ right: 0;
18
+ width: 100%;
19
+ height: 100%;
20
+ }
21
+ </style>
22
+ </head>
23
+ <body>
24
+ <div class="container">
25
+ <iframe
26
+ class="responsive-iframe"
27
+ src="$iframe"
28
+ frameborder="0"
29
+ ></iframe>
30
+ </div>
31
+
32
+ </body>
33
+ </html>
template/front.html ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ </head>
5
+ <body>
6
+ <iframe
7
+ src="$iframe"
8
+ frameborder="0"
9
+ width="100%"
10
+ height="2000"
11
+ ></iframe>
12
+ </body>
13
+ </html>