-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
183 lines (152 loc) · 6.33 KB
/
main.py
File metadata and controls
183 lines (152 loc) · 6.33 KB
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 asyncio
import os
import httpx
from fastapi import HTTPException
from fastapi.responses import RedirectResponse
from nicegui import app, ui
NASA_BASE_URL = 'https://images-api.nasa.gov/search'
BASE_URL = os.environ.get('BASE_URL', 'http://127.0.0.1:10000')
# ---------------------------
# FastAPI Endpoints via NiceGUI
# ---------------------------
@app.get('/search')
async def search_nasa_images(query: str, media_type: str = 'image'):
async with httpx.AsyncClient() as client:
response = await client.get(
NASA_BASE_URL, params={'q': query, 'media_type': media_type}
)
response.raise_for_status()
return response.json()
@app.get('/video/{video_id}')
async def get_video_stream(video_id: str):
try:
collection_url = (
f'https://images-assets.nasa.gov/video/{video_id}/collection.json'
)
async with httpx.AsyncClient() as client:
r = await client.get(collection_url)
r.raise_for_status()
files = r.json()
mp4 = next((f for f in files if f.endswith('~orig.mp4')), None)
if not mp4:
raise HTTPException(status_code=404, detail='Video file not found')
return RedirectResponse(mp4)
except Exception as e:
raise HTTPException(status_code=500, detail=f'Could not redirect to video: {e}')
# ---------------------------
# UI Page
# ---------------------------
@ui.page('/')
async def home():
ui.add_head_html("""
<style>
body::before {
content: '';
position: fixed;
top: 0; left: 0; right: 0; bottom: 0;
background: url('https://images.unsplash.com/photo-1679706292806-3a7d5eb2dd75') no-repeat center center;
background-size: cover;
filter: blur(5px);
z-index: -1;
}
.glass {
background: rgba(255, 255, 255, 0.1);
border-radius: 16px;
backdrop-filter: blur(10px);
border: 1px solid rgba(255, 255, 255, 0.2);
}
.glowing-card {
border: 1px solid rgba(255, 255, 255, 0.2);
box-shadow: 0 0 15px rgba(147, 197, 253, 0.3),
0 0 5px rgba(255, 255, 255, 0.2);
transition: box-shadow 0.3s ease, transform 0.3s ease;
}
.glowing-card:hover {
box-shadow: 0 0 20px rgba(147, 197, 253, 0.5),
0 0 10px rgba(255, 255, 255, 0.3);
transform: scale(1.03);
}
</style>
""")
with ui.column().classes('w-full items-center gap-6 p-8 min-h-screen text-white'):
ui.label('🌌 NASA Explorer').classes('text-4xl font-bold mb-2')
with ui.row().classes('gap-4 glass p-6 rounded-xl shadow-lg'):
query_input = ui.input(label='Search NASA Images', value='galaxy').classes(
'w-64 text-white bg-black/30 rounded-xl'
)
media_type = ui.select(
['image', 'video'], value='image', label='Media Type'
).classes('text-white bg-black/30 rounded-xl')
ui.button('Search', on_click=lambda: asyncio.create_task(search())).classes(
'search-btn bg-gradient-to-r from-indigo-500 to-purple-700 '
'hover:from-indigo-400 hover:to-purple-600 text-white px-6 py-2 '
'rounded-xl shadow-md transition-all duration-300'
)
result_area = ui.row(wrap=True).classes('gap-6 justify-center mt-6')
modal = ui.dialog().classes('backdrop-blur')
def show_modal(image_url: str, image_title: str):
modal.clear()
with modal:
with ui.column().classes('items-center p-4 text-white'):
ui.image(image_url).classes(
'max-w-full max-h-[75vh] rounded-xl shadow-lg'
)
ui.label(image_title).classes('mt-4 text-lg font-semibold')
ui.button(
'Download',
on_click=lambda: ui.run_javascript(
f"window.open('{image_url}', '_blank')"
),
).classes('mt-4')
modal.open()
async def search():
result_area.clear()
query = query_input.value
media = media_type.value
async with httpx.AsyncClient() as client:
try:
response = await client.get(
f'{BASE_URL}/search',
params={'query': query, 'media_type': media},
)
response.raise_for_status()
items = response.json().get('collection', {}).get('items', [])
except Exception as e:
with result_area:
ui.label(f'Error: {str(e)}').classes('text-red-400')
return
if not items:
with result_area:
ui.label('No results found.').classes('text-yellow-400')
return
for item in items[:12]:
data = item.get('data', [{}])[0]
links = item.get('links', [{}])
img_url = links[0].get('href') if links else ''
title = data.get('title', 'Untitled')
nasa_id = data.get('nasa_id', '')
with result_area:
with ui.card().classes(
'glass w-80 glowing-card text-white shadow-xl'
):
ui.label(title).classes('p-2 text-sm font-medium')
if media == 'image' and img_url.endswith(('.jpg', '.png')):
ui.image(img_url).on(
'click',
lambda e, url=img_url, title=title: show_modal(
url, title
),
).classes('rounded-t-xl max-h-64 w-full object-cover')
elif media == 'video' and nasa_id:
video_url = f'{BASE_URL}/video/{nasa_id}'
ui.video(video_url).classes('rounded-xl w-full max-h-64')
# ---------------------------
# Run the App
# ---------------------------
ui.run(
host='0.0.0.0',
port=int(os.environ.get('PORT', 8080)),
title='NASA Explorer',
dark=True,
reload='FLY_ALLOC_ID' not in os.environ,
)