Spaces:
Runtime error
Runtime error
File size: 6,258 Bytes
511891d 9ddd211 7748011 6b9f87a d997d39 ce783e0 f2edad2 511891d 6f2a024 0b73570 6f2a024 bd24d05 49f5e2d bd24d05 49f5e2d 4507214 49f5e2d bd24d05 288aecc 5b2b36d bd24d05 56000e1 bd24d05 c721296 7839cc0 bd24d05 49f5e2d bd24d05 67949b6 25ec869 6f2a024 0d0db28 5b2b36d c721296 7839cc0 e4e5afb 7839cc0 c721296 bd24d05 01f064c 1cb17a2 7748011 01f064c eea38a1 121c044 a582447 6e5e3c6 07eb494 7748011 01f064c 7748011 6e5ae84 7748011 aea9da6 7748011 6def092 aea9da6 7748011 77b6da3 05a6079 67462d9 05a6079 67462d9 f7f6a9c 7fffb8e 6b9f87a f7f6a9c d997d39 67462d9 05a6079 0171a9f b21b75e 7748011 0b73570 cb3b6d4 01f064c e8ad872 01f064c 7748011 b21b75e 01f064c 56000e1 e4e5afb 01f064c 7748011 b21b75e 01f064c 288aecc 01f064c e8ad872 |
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 |
import gradio as gr
import requests
import json
import os
from PIL import Image
import moviepy.video.io.ImageSequenceClip as ic
import urllib.request
import uuid
main_directory = "https://services.swpc.noaa.gov/"
html = """
<div>
PAGE_LINK
</div>
"""
css="""
.img_box{
display: flex;
flex-direction: column;
flex-wrap: wrap;
height: 20000px;
}
.img_class{
background: #ffffff;
max-width: 48%;
font-family: monospace;
border-top: #9300ff;
border-style: inset;
margin-top: 5px;
}
.img_class_raw{
background: #ffffff;
width: 100%;
font-family: monospace;
border-top: #9300ff;
border-style: inset;
margin-top: 5px;
display:flex;
flex-direction:column;
}
"""
def load_json(url1="",url2="",url3="",url4=""):
get_url=f'{main_directory}{url1}{url2}{url3}{url4}'
print(f'{get_url}')
get_url=get_url.split("None")[0]
print(f'{get_url}')
get_url=get_url.split("[]")[0]
print(f'{get_url}')
if get_url.endswith('.json'):
feed1 = requests.get(get_url)
return None, feed1.text
elif get_url.endswith(".png") or get_url.endswith(".gif") or get_url.endswith(".jpg"):
html_out=f"<style>{css}</style><div>"
html_out+=f'<div class="img_class_raw"><a href="{get_url}" target="_blank">{get_url}</a><br><img src={get_url}></div></div>'
return html_out, None
return None,None
def make_tree(url1="",url2="",url3="",url4=""):
link_box=[]
html_out=""
get_url=f'{main_directory}{url1}{url2}{url3}{url4}'
print(f'######### :: {get_url}')
if not get_url.endswith('.json'):
feed1 = requests.get(get_url)
spl = feed1.text.split("href=")
for line in spl:
spl2 = line.split(">")[0]
print(spl2)
if spl2.endswith('/"') or spl2.endswith('.json"') or spl2.endswith('.png"') or spl2.endswith('.gif"') or spl2.endswith('.jpg"'):
fin=line.split(">")[0].strip('""')
link_box.append(fin)
#html_out=html_out+html.replace("PAGE_LINK",fin)
return gr.update(choices=[l for l in link_box],interactive=True)
else:
return None
def get_images():
html_out=f"<style>{css}</style><div class='img_box'>"
get_url=f'{main_directory}images/geospace/'
feed1 = requests.get(get_url)
spl = feed1.text.split("href=")
for line in spl:
spl2 = line.split(">")[0].strip('""')
if spl2.endswith(".png") or spl2.endswith(".gif") or spl2.endswith(".jpg"):
print(spl2)
html_out+=f'<div class="img_class"><a href="{get_url}{spl2}" target="_blank">{get_url}{spl2}</a><br><img src={get_url}{spl2}></div>'
else:
print(spl2)
get_url2=f'{main_directory}images/'
feed2 = requests.get(get_url2)
spl = feed2.text.split("href=")
for line2 in spl:
spl2 = line2.split(">")[0].strip('""')
if spl2.endswith(".png") or spl2.endswith(".gif") or spl2.endswith(".jpg"):
print(spl2)
html_out+=f'<div class="img_class"><a href="{get_url2}{spl2}" target="_blank">{get_url2}{spl2}</a><br><img src={get_url2}{spl2}></div>'
else:
print(spl2)
html_out+="</div>"
return html_out
def make_animation():
html_out=f"<style>{css}</style><div class='img_box'>"
get_url=f'{main_directory}images/animations/'
feed1 = requests.get(get_url)
spl = feed1.text.split("href=")
for line in spl:
gif_box=[]
spl2 = line.split(">")[0].strip('""')
if spl2.endswith("/"):
feed2 = requests.get(f'{get_url}{spl2}')
spl3 = feed2.text.split("href=")
for line2 in spl3:
spl3 = line2.split(">")[0].strip('""')
print("$$$$$$$$$$$$$$")
print(spl3)
if spl3.endswith(".png") or spl3.endswith(".gif") or spl3.endswith(".jpg"):
gif_box.append(f'{get_url}{spl2}{spl3}')
if gif_box:
try:
#frames = [Image.open(image) for image in glob.glob(f"{frame_folder}/*.JPG")]
frames = []
for i,ea in enumerate(gif_box):
print("############")
print(ea)
urllib.request.urlretrieve(ea,f'tmp{i}.png')
frames.append(f'tmp{i}.png')
#frames.append(Image.open(f'tmp{i}.png'))
#frame_one = frames[0]
#filename1=f'{uuid.uuid4()}.gif'
#filename=os.path.abspath(filename1)
print("**************")
#print(filename)
fps=10
uid=uuid.uuid4()
clip = ic.ImageSequenceClip(frames, fps = fps)
clip.to_gif(f"{uid}.gif",fps=fps)
#frame_one.save(filename, format="GIF", append_images=frames, save_all=True, duration=100, loop=0)
html_out+=f'<div class="img_class"><img src="{uid}.gif"></div>'
except Exception as e:
print(e)
html_out+='</div>'
return html_out,gr.update()
def run():
out=make_tree()
im_html=get_images()
return out, im_html
with gr.Blocks() as app:
with gr.Tab("Images"):
html_im=gr.HTML()
with gr.Tab("Animations"):
anim_btn=gr.Button()
anim_out=gr.HTML()
file_explor=gr.FileExplorer()
with gr.Tab("Raw"):
with gr.Row():
drop1=gr.Dropdown()
drop2=gr.Dropdown()
drop3=gr.Dropdown()
drop4=gr.Dropdown()
load_btn=gr.Button("Load")
html_raw=gr.HTML()
links=gr.JSON()
###### Images ##########
load_btn.click(load_json,[drop1,drop2,drop3,drop4],[html_raw,links])
anim_btn.click(make_animation,None,[anim_out,file_explor])
####### Raw ############
drop1.change(make_tree,drop1,[drop2])
drop2.change(make_tree,[drop1,drop2],[drop3])
drop3.change(make_tree,[drop1,drop2,drop3],[drop4])
######################
app.load(run,None,[drop1,html_im])
app.launch() |