File size: 7,557 Bytes
e8baafb
34b50ed
4f6aeda
5e5b0f4
cf80216
5f9b0b9
2f4adf3
028fbc8
 
0ca1004
f313251
0ca1004
e433e8b
7949551
5963ebd
12a8720
 
dfd25b4
12a8720
 
 
dfd25b4
12a8720
 
86a1c17
12a8720
6431362
7949551
6431362
 
 
8b79b12
6431362
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8b79b12
6431362
 
bcd832e
1e9f5bb
 
f30bad7
 
 
761b657
6431362
 
 
f30bad7
6431362
 
 
ee09bc8
 
f30bad7
e433e8b
 
 
f92d6ef
 
 
 
 
 
05f20f7
f92d6ef
 
 
 
0d6b8a9
 
f92d6ef
 
 
ee09bc8
 
 
ec67940
 
db293cc
 
 
 
 
 
ee09bc8
 
 
 
 
 
 
 
 
 
 
 
7949551
f92d6ef
 
 
 
 
 
 
7949551
ee09bc8
845d3ac
ee09bc8
 
7f21918
845d3ac
ee09bc8
 
f30bad7
ee09bc8
c29a7fb
ee09bc8
 
845d3ac
ee09bc8
 
 
 
 
 
 
f92d6ef
ee09bc8
 
 
 
 
 
 
 
7e33ee0
ee09bc8
 
 
 
 
 
 
 
 
e8baafb
1a0a0ae
 
 
70cf4a6
e8baafb
c11f347
a1b5c45
 
465cd93
c11f347
b958d89
bcd832e
 
a1b5c45
9e6bbf4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import gradio as gr
from huggingface_hub import HfApi, list_files_info, get_full_repo_name
import requests
import json
import os
from PIL import Image
import urllib.request 
import uuid
uid=uuid.uuid4()
token = ""
source_dataset = "jbilcke-hf/ai-tube-index"
api = HfApi(token=token)
token_self = os.environ['HF_TOKEN']

def find_dataset(author=""):
    s_ist = (api.list_datasets(author=author,search="ai-tube"))
    #print(api.whoami())
    channels=[]
    videos=[]
    for space in s_ist:
        try:
            channels.append(space.id)
        except Exception:
            pass
    return (gr.Dropdown(label="Channels", choices=[s for s in channels],interactive=True))


def load_vid(channel):
    out_html=""
    label_html=""
    vid_list=[]
    sync_list=[]
    try:
        file_box = [info.path for info in list_files_info(f'{channel}', repo_type="dataset")]
        ai_tube_box = [info.path for info in list_files_info(f'{source_dataset}', repo_type="dataset")]
        for file in file_box:
            if file.startswith("prompt_"):
                prompt_slug = file.split("prompt_",1)[1].split(".",1)[0]
                out_html=f'{out_html}<pre>  • {file}</pre>'
                #print(prompt_slug)
                for vid_file in ai_tube_box:
                    try:
                        vid_slug=vid_file.split("videos/",1)[1].split(".",1)[0]
                        #print(vid_slug)
                        try:
                            if vid_slug == prompt_slug:
                                out_html=f'{out_html}<pre>    + {vid_file}</pre>'
                                
                                if vid_file.endswith(".json"): 
                                    r = requests.get(f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/raw/main/videos/{vid_slug}.json')
                                    #r = requests.get(f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/blob/main/videos/00b4bcda-7b4a-40f8-9833-e490425a7b91.mp4')
                                    json_out=r.json()
                                    label_html=f'{label_html}{json_out["label"]}<br>{json_out["description"]}<br>'
                                    #print (r.json())
                                if vid_file.endswith(".mp4"):
                                    vid_list.append(json_out["label"])
                                    sync_list.append(vid_file)
                                    #<source src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/videos/{vid_file}" type="video/mp4">
                                    print(vid_file)
                                    label_html=f'''{label_html}
                                    <video src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/{vid_file}" controls>
                                    </video>'''
                                #uid = uuid.uuid4()
                                #file = open(f'{name}/{f_name}','wb')
                                #file.write(r.content)
                        except Exception as e:
                            print (f'error:: {e}')
                            pass
                    except Exception:
                        pass
    except Exception as e:
        print(e)
    return(out_html,gr.Dropdown(label="Videos",type="index", choices=[s for s in vid_list],interactive=True),sync_list)


def show_vid(vid_file,list_hid):
    print(vid_file)
    print(list_hid)
    vid_list=[]
    for i,ea in enumerate(list_hid):
        vid_list.append(ea)
    label_html=""
    #vid_slug=vid_file.split("videos/",1)[1].split(".",1)[0]

    label_html=f'''{label_html}
    <video src="https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/{vid_list[int(vid_file)]}" controls>
    </video>'''
    return label_html

##############################################

#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):
def build_space(index_sel, vid_file, t_name=None, token=None):
    if token == None or token == "":
        token=token_self
    vid_src = vid_file[int(index_sel)]
    print(vid_src)
    vid_slug=vid_src.split("videos/",1)[1].split(".",1)[0]
    print(vid_slug)
    #json_in = f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/blob/main/videos/{vid_slug}.json'
    video_in = f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/videos/{vid_slug}.mp4'
    thumb_in = f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/resolve/main/videos/{vid_slug}.webp'

    r = requests.get(f'https://huggingface.co/datasets/jbilcke-hf/ai-tube-index/raw/main/videos/{vid_slug}.json')
    json_out=r.json()
    
    t_name="ai-tube-test1"  
    t_title = json_out["label"]
    t_description = json_out["description"]
    t_image = json_out["thumbnailUrl"]
    f_name = json_out["id"]
    
    if t_description == None or t_description == "":
        t_description = f'Embedded App'
    #if t_redirect == None or t_redirect =="":
    t_redirect = "https://jbilcke-hf-ai-tube.hf.space/"
    #if t_name == None or t_name =="":
        #print (t_space)
        #t_name = t_space.split("//",1)[1]
        #print (t_name)
        #t_name = t_name.strip(".hf.space")
        #print (f't_name::{t_name}')

    
    model_id=t_name
    #model_id="omnibus/fff"
    if token==None or token=="":
        token = token_self
    else:
        token = token
        pass
    api = HfApi(token=token)
    repo_name = get_full_repo_name(model_id=model_id, token=token)

    try:
        repo_url = api.create_repo(
            repo_id=model_id,
            repo_type="space",
            space_sdk="static",
            private=False,
            exist_ok=True,
        )
    except Exception as e:
        return gr.HTML(f"""{str(e)}""")
           
    try:
        api_url = f'https://huggingface.co/api/spaces/{model_id}'
        #t_iframe = t_space
        t_link = f"https://{repo_name.replace('/','-').replace('_','-')}.static.hf.space/{f_name}.html"
        
########## norm 
        with open("template_front.html", "r") as f:
            app = f.read()
        app = app.replace("$space", video_in)
        app = app.replace("$title", t_title)
        app = app.replace("$description", t_description)
        app = app.replace("$image", thumb_in)
        app = app.replace("$redirect", t_redirect)

        with open("ai.html", "w") as f:
            f.write(app)

        api.upload_file(
            path_or_fileobj="ai.html",
            path_in_repo=f"{f_name}.html",
            repo_id=repo_name,
            token=token,
            repo_type="space",
        )
        os.remove("ai.html")


        return gr.HTML(f'''<center>Your Interactive Twitter Card Embed Link is:<br><a href="{t_link}" target="_blank">{t_link}</a><br>

                                  <center><br>''')
    except Exception as e:
        return gr.HTML(f"""{str(e)}""")

    
#########################################    



with gr.Blocks() as app:
    with gr.Row():
        channel = gr.Dropdown()
        videos = gr.Dropdown()
    button = gr.Button("Build")
    output = gr.HTML()
    label_output = gr.HTML()
    links_output = gr.HTML()

    list_hid=gr.JSON(visible=False)

    app.load(find_dataset,None,channel)
    channel.change(load_vid,channel,[output,videos,list_hid])
    videos.change(show_vid,[videos,list_hid],[label_output])
    button.click(build_space,[videos,list_hid],links_output)
app.launch(show_api=False,share=False)