Spaces:
Running
Running
File size: 10,180 Bytes
c3c19d7 2ebe379 c63fa25 5bdc571 c3c19d7 d5467cc 004a9e9 3dc132c f8e687b 3dc132c e88c37c 3dc132c 435387c 2294092 435387c ec51248 ab48f00 848d988 3dc132c 848d988 f8e687b 435387c fe6c5d2 ed5f851 c651f5f c8500bd 61b7ec6 848d988 ab48f00 bae52ce 848d988 2ebe379 2080cd8 a1c1a15 2080cd8 2ebe379 2080cd8 2ebe379 848d988 2ebe379 848d988 3ed286b 3dc132c 8608479 c3c19d7 baf0070 236ae69 841e9d6 d02efcd 841e9d6 baf0070 d02efcd c3c19d7 6d18403 b625476 3dc132c 9e02b93 3dc132c b625476 3dc132c b625476 baf0070 841e9d6 3dc132c baf0070 3dc132c baf0070 c3c19d7 |
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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
import gradio as gr
import requests
import json
import os
from PIL import Image
import moviepy.video.io.ImageSequenceClip as ic
from pathlib import Path
import bs4
import datetime
import urllib.request
import uuid
import io
import base64
main_directory = "https://services.swpc.noaa.gov/images/animations/"
sdo_source = "https://sdo.gsfc.nasa.gov/assets/img/browse/"
sdo_source_format = "https://sdo.gsfc.nasa.gov/assets/img/browse/YEAR/MONTH/DAY/DATE_IDENT_SIZE_TOOL.jpg"
sdo_obj = ["HMIB","HMIBC","HMIIC","HMIIF","HMID","HMII","HMI171",
"0094","0131","0171","0193","0211",
"0304","0335","1600","1700","4500",
"211193171","211193171n","211193171rg",
"094335193","304211171"]
sdo_size= [256,512,1024,2048,4096]
html = """
<div>
PAGE_LINK
</div>
"""
css="""
.img_box{
display: flex;
flex-direction: column;
flex-flow: unset;
flex-wrap: wrap;
justify-content: space-around;
}
.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;
}
.img_box_soho{
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: space-between;
}
.img_class_soho{
background: #ffffff;
font-family: monospace;
border-top: #9300ff;
border-style: inset;
margin-top: 5px;
max-width: 48%;
}
.img_class_sdo{
background: #ffffff;
font-family: monospace;
border-top: #9300ff;
border-style: inset;
margin-top: 5px;
max-width: 25%;
font-size: small;
}
"""
def make_animation(url1="",url2="",url3="",url4="",url5="",url6="",url7="",url8="",cnt=1):
print("###################################################################")
get_url=f'{main_directory}{url1}{url2}{url3}{url4}{url5}{url6}{url7}{url8}'
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}')
link_box=[]
#get_url=f'{main_directory}images/animations/'
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('.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)
print("############################## LINK BOX ###############################")
print(link_box)
link_box.sort(reverse=True)
print(link_box[0])
#spl = feed1.text.split("href=")
files_out=[]
html_out=""
gif_box=[]
print("############################ LINES #######################################")
for line in link_box[:cnt]:
#try:
print(line)
if line.endswith('.png') or line.endswith('.gif') or line.endswith('.jpg'):
gif_box.append(f'{get_url}{line}')
#gif_box.append(f'{line}')
else: print("Passing: Not Image")
if gif_box:
print(gif_box)
gif_box.sort(reverse=False)
print("Making GIF...")
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')
fps=60
uid=uuid.uuid4()
clip = ic.ImageSequenceClip(frames, fps = fps)
# Save GIF to a temporary file
temp_gif_path = f"temp_{uid}.gif"
clip.write_gif(temp_gif_path, fps=fps)
# Read the GIF file into bytes
with open(temp_gif_path, 'rb') as gif_file:
gif_bytes = gif_file.read()
# Encode GIF bytes as base64 for HTML
gif_base64 = base64.b64encode(gif_bytes).decode('utf-8')
gif_data_url = f"data:image/gif;base64,{gif_base64}"
html_out=f"<style>{css}</style><div>"
html_out+=f'<div class="img_class"><img src="{gif_data_url}"></div>'
html_out+="</div>"
except Exception as e:
print("ERROR HAPPENED")
print(e)
else:
print("passed")
return html_out
def load_json(url1="",url2="",url3="",url4="",url5="",url6="",url7="",url8=""):
get_url=f'{main_directory}{url1}{url2}{url3}{url4}{url5}{url6}{url7}{url8}'
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="",url5="",url6="",url7="",url8=""):
link_box=[]
html_out=""
get_url=f'{main_directory}{url1}{url2}{url3}{url4}{url5}{url6}{url7}{url8}'
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">{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">{spl2}</a><br><img src={get_url2}{spl2}></div>'
else:
print(spl2)
html_out+="</div>"
return html_out
def run():
out=make_tree()
im_html=get_images()
return im_html
datestamp=["1","1","1"]
timestamp=["1","1","1"]
datetimestamp="1:1:1"
def get_time():
datetimestamp=str(datetime.datetime.now()).split(".")[0]
datestamp=datetimestamp.replace(" ","-").split("-")
timestamp=datestamp[3].split(":")
html_out=f"""<div style='font-size:xxx-large;'>Current: {datetimestamp} UTC</div>"""
year1=int(datestamp[0])
month1=int(datestamp[1])
day1=int(datestamp[2])
hour1=int(timestamp[0])
minute1=int(timestamp[1])
year2=int(datestamp[0])
month2=int(datestamp[1])
day2=int(datestamp[2])
hour2=int(timestamp[0])
minute2=int(timestamp[1])
return gr.update(value=html_out)
'''return (gr.update(value=html_out),gr.update(value=year1),gr.update(value=month1),
gr.update(value=day1),gr.update(value=hour1),gr.update(value=minute1),
gr.update(value=year2),gr.update(value=month2),gr.update(value=day2),gr.update(value=hour2),gr.update(value=minute2))'''
with gr.Blocks() as app:
#datetimestamp=gr.Textbox()
#datestamp=gr.Textbox()
#timestamp=gr.State()
datetimestamp=str(datetime.datetime.now()).split(".")[0]
datestamp=datetimestamp.replace(" ","-").split("-")
timestamp=datestamp[3].split(":")
time_html=gr.HTML(f"""<div style='font-size:xxx-large;'>Current: {datetimestamp} UTC</div>""")
with gr.Row():
gr.Markdown("<h5 style='font-size:xx-large;'>Frames:")
cnt=gr.Number(label="Count",minimum=1,maximum=1000,precision=0,value=1,min_width=10)
drop1=gr.Dropdown()
drop2=gr.Dropdown()
drop3=gr.Dropdown()
drop4=gr.Dropdown()
with gr.Row():
drop5=gr.Dropdown()
drop6=gr.Dropdown()
drop7=gr.Dropdown()
drop8=gr.Dropdown()
load_btn=gr.Button("Load")
anim_btn=gr.Button("Make Animation")
html_raw=gr.HTML()
links=gr.JSON()
#app.load(get_time,None,[time_html,year1,month1,day1,hour1,minute1,year2,month2,day2,hour2,minute2]).then(make_tree,None,drop1)
app.load(get_time,None,[time_html]).then(make_tree,None,drop1)
load_btn.click(load_json,[drop1,drop2,drop3,drop4,drop5,drop6,drop7,drop8],[html_raw,links])
anim_btn.click(make_animation,[drop1,drop2,drop3,drop4,drop5,drop6,drop7,drop8,cnt],[html_raw])
drop1.change(make_tree,drop1,[drop2])
drop2.change(make_tree,[drop1,drop2],[drop3])
drop3.change(make_tree,[drop1,drop2,drop3],[drop4])
drop4.change(make_tree,[drop1,drop2,drop3,drop4],[drop5])
drop5.change(make_tree,[drop1,drop2,drop3,drop4,drop5],[drop6])
drop6.change(make_tree,[drop1,drop2,drop3,drop4,drop5,drop6],[drop7])
drop7.change(make_tree,[drop1,drop2,drop3,drop4,drop5,drop6,drop7],[drop8])
#app.load(make_tree,None,drop1)
app.queue(default_concurrency_limit=10).launch()
|