Spaces:
Sleeping
Sleeping
Update app.py
Browse files11234567107 unmodified lines11511611711811911812012112012212312412512612712812913012612712812913113213313413513613713813914014114214314414514613113214714814915013415115215315433 unmodified lines18818919019119219319419519617 unmodified lines2142152162172182192202002212222232243 unmodified lines228229230210231232233234# app.py — FINAL VERSION# app.py — FINAL VERSION (with Download)import gradio as grimport timeimport os # ### NEW ### - Needed for checking if the zip file existsfrom backend import ( create_user, get_user_by_username,107 unmodified lines yield get_system_stats() time.sleep(5)
### MODIFIED ###def stream_logs(project_id): """A generator that streams logs for a selected project every 3 seconds.""" """A generator that streams logs and provides a download link when complete.""" if not project_id: yield "⬅️ Select a project from the dropdown to see its live logs." yield "⬅️ Select a project from the dropdown to see its live logs.", gr.update(visible=False) return
# On first run for a new selection, ensure the download button is hidden yield "Fetching logs...", gr.update(visible=False)
while True: project = get_project(project_id) if project: if not project['logs']: yield f"🕒 Project #{project_id} is queued or starting... Logs will appear here shortly." else: yield project['logs'] logs = project['logs'] if project['logs'] else f"🕒 Project #{project_id} is queued or starting... Logs will appear here shortly." # Check for terminal states if project['status'] == 'completed': final_log = logs + "\n\n✅ **Project Completed!** Your file is ready for download." zip_path = project.get('zip_path') if zip_path and os.path.exists(zip_path): yield final_log, gr.update(value=zip_path, visible=True) else: yield logs + "\n\n⚠️ **Warning:** Project completed, but the ZIP file was not found.", gr.update(visible=False) break # Stop the loop elif project['status'] == 'failed': yield logs, gr.update(visible=False) break # Stop the loop if project['status'] in ['completed', 'failed']: break # Otherwise, just stream the current logs yield logs, gr.update(visible=False)
else: yield f"Error: Project #{project_id} not found." yield f"Error: Project #{project_id} not found.", gr.update(visible=False) break time.sleep(3)
33 unmodified lines project_selector = gr.Dropdown(label="Select Project to View", choices=[], interactive=True) with gr.Group(elem_classes=["logs-container"]): logs_display = gr.Markdown("Logs will appear here in real-time.", elem_classes=["monospace"]) # ### NEW ### - File download component, initially hidden download_zip_ui = gr.File(label="Download Project ZIP", visible=False, interactive=False) ram_monitor = gr.Markdown()
17 unmodified lines outputs=[projects_display, project_selector] ) ### MODIFIED ### project_selector.change( fn=stream_logs, inputs=project_selector, outputs=logs_display outputs=[logs_display, download_zip_ui] # Now updates both logs and the download component )
demo.load(3 unmodified lines
gr.HTML(""" <style> .logs-container { height: 600px; overflow-y: auto; border: 1px solid #E5E7EB; border-radius: 8px; padding: 8px; background-color: #F9FAFB; } .logs-container { height: 550px; overflow-y: auto; border: 1px solid #E5E7EB; border-radius: 8px; padding: 8px; background-color: #F9FAFB; } .monospace { font-family: 'SF Mono', 'Consolas', 'Courier New', monospace; font-size: 14px; white-space: pre-wrap; word-wrap: break-word; } </style> """)app.py+33-12