Omnibus commited on
Commit
a173f2a
1 Parent(s): 7f8ba04

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +124 -7
app.py CHANGED
@@ -8,7 +8,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
 
@@ -24,6 +24,112 @@ def slide_fn(val):
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
@@ -234,6 +340,7 @@ def build_space(t_name,t_space,t_title,t_description,t_redirect,t_image=None,t_i
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
@@ -281,19 +388,29 @@ with gr.Blocks() as build_app:
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()
 
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
 
 
24
  img2.save(f'tmpim2-{uid}.png')
25
  return img2
26
 
27
+ def make_files(t_name,t_space,t_title,t_description,t_redirect,t_image=None,t_image_url=None,token=None):
28
+ model_id=t_name
29
+ files=[]
30
+ if t_image_url != None and t_image_url != "" and t_image==None:
31
+ t_image=t_image_url
32
+ print(f"Using image URL {t_image}")
33
+ pass
34
+ elif t_image != None:
35
+ img1 = Image.open(t_image)
36
+ img1.thumbnail((500,500), Image.Resampling.LANCZOS)
37
+ img1.save(f'tmpim-{uid}.png')
38
+ output_pro=cv2.imread(f'tmpim-{uid}.png')
39
+ output_pro2=cv2.imread(f'tmpim2-{uid}.png')
40
+
41
+ cv2.imwrite(f"./img-{uid}.png",output_pro)
42
+ cv2.imwrite(f"./img2-{uid}.png",output_pro2)
43
+ files.append(f"./img-{uid}.png",f"./img2-{uid}.png")
44
+ pass
45
+
46
+ else:
47
+ print("Default Image")
48
+ t_image="https://huggingface.co/spaces/portal/bin/resolve/main/ai_demo_card.png"
49
+ files.append(t_image)
50
+ pass
51
+
52
+ try:
53
+ api_url = f'https://huggingface.co/api/spaces/{model_id}'
54
+ t_iframe = t_space
55
+ t_link3= f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/masto.html"
56
+ t_link2= f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/gab.html"
57
+ t_link = f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/ai.html"
58
+ t_space = f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/back.html"
59
+ t_space2 = f"https://{repo_name.replace('/','-').replace('_','-')}.hf.space/back_gab.html"
60
+
61
+
62
+ ########## norm
63
+ with open("template/ai.html", "r") as f:
64
+ app = f.read()
65
+ app = app.replace("$space", t_space)
66
+ app = app.replace("$title", t_title)
67
+ app = app.replace("$description", t_description)
68
+ app = app.replace("$image", t_image)
69
+ app = app.replace("$redirect", t_redirect)
70
+
71
+ with open("ai.html", "w") as f:
72
+ f.write(app)
73
+ files.append("ai.html")
74
+
75
+ ######## gab
76
+ with open("template/ai.html", "r") as f:
77
+ app = f.read()
78
+ app = app.replace("$space", t_space2)
79
+ app = app.replace("$title", t_title)
80
+ app = app.replace("$description", t_description)
81
+ app = app.replace("$image", t_image2)
82
+ app = app.replace("$redirect", t_redirect)
83
+
84
+ with open("gab.html", "w") as f:
85
+ f.write(app)
86
+ files.append("gab.html")
87
+
88
+ ######## masto
89
+ with open("template/ai.html", "r") as f:
90
+ app = f.read()
91
+ app = app.replace("$space", t_space)
92
+ app = app.replace("$title", t_title)
93
+ app = app.replace("$description", t_description)
94
+ app = app.replace("$image", t_image2)
95
+ app = app.replace("$redirect", t_redirect)
96
+
97
+ with open("masto.html", "w") as f:
98
+ f.write(app)
99
+ files.append("masto.html")
100
+
101
+
102
+ ##################
103
+ with open("template/front.html", "r") as f:
104
+ app = f.read()
105
+ app = app.replace("$iframe", t_iframe)
106
+
107
+ with open("index.html", "w") as f:
108
+ f.write(app)
109
+ files.append("index.html")
110
+
111
+ ######### norm
112
+ with open("template/back.html", "r") as f:
113
+ app = f.read()
114
+ app = app.replace("$iframe", t_iframe)
115
+ app = app.replace("$pad", "100%")
116
+ with open("back.html", "w") as f:
117
+ f.write(app)
118
+ files.append("back.html")
119
+
120
+ ####### gab
121
+ with open("template/back.html", "r") as f:
122
+ app = f.read()
123
+ app = app.replace("$iframe", t_iframe)
124
+ app = app.replace("$pad", "56%")
125
+ with open("back_gab.html", "w") as f:
126
+ f.write(app)
127
+ files.append("back_gab.html")
128
+ return files
129
+
130
+
131
+ except Exception as e:
132
+ return None
133
 
134
  def build_space(t_name,t_space,t_title,t_description,t_redirect,t_image=None,t_image_url=None,token=None):
135
  model_id=t_name
 
340
  <center><br>''')
341
  except Exception as e:
342
  return gr.HTML.update(f"""{str(e)}""")
343
+
344
  def d_im():
345
  output="https://huggingface.co/spaces/portal/bin/resolve/main/ai_demo_card.png"
346
  return output
 
388
  t_image_url = gr.Textbox(label="Image URL", placeholder="Optional URL Input")
389
  t_image = gr.Image(label="Twitter Card Image", type="filepath",interactive=True)
390
  with gr.Row():
391
+ with gr.Column():
392
+ c_image=gr.Image(label="Cropped Image", type="filepath",interactive=False)
393
+ def_im = gr.Button("Load Default Image")
394
  with gr.Column():
395
  c_slider=gr.Slider(0,220,value=0,step=1,label="Adjust Vertical Crop")
396
  c_upbtn=gr.Button("Update Crop")
397
+ with gr.Tab("Build"):
398
+ with gr.Row():
399
+ build_button = gr.Button("Build")
400
+ gr.Column()
401
+ output_html = gr.HTML("""""")
402
+ with gr.Tab("Download"):
403
+ with gr.Row():
404
+ dl_button = gr.Button("Download")
405
+ gr.Column()
406
+ files_out = gr.Files()
407
  gr.Column(scale=1)
408
 
409
 
410
  t_image.change(up_c,[t_image,c_slider],c_image)
411
  c_upbtn.click(slide_fn,c_slider,c_image)
412
+ build_button.click(build_space,[toggle_build,t_name,t_space,t_title,t_description,t_redirect,t_image,t_image_url,option_token],output_html)
413
+ dl_button.click(build_space,[toggle_build,t_name,t_space,t_title,t_description,t_redirect,t_image,t_image_url,option_token],files_out)
414
+
415
  def_im.click(d_im,None,t_image)
416
  build_app.queue(concurrency_count=10).launch()