Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456107 unmodified lines11411511611711811912012112212312412512612712812913013113213313413513613733 unmodified lines17117217317417517617 unmodified lines1941951961971981992002012022033 unmodified lines207208209210211212213# app.py — FINAL VERSIONimport gradio as grimport timefrom backend import (create_user,get_user_by_username,107 unmodified linesyield get_system_stats()time.sleep(5)def stream_logs(project_id):"""A generator that streams logs for a selected project every 3 seconds."""if not project_id:yield "⬅️ Select a project from the dropdown to see its live logs."returnwhile 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']if project['status'] in ['completed', 'failed']:breakelse:yield f"Error: Project #{project_id} not found."breaktime.sleep(3)33 unmodified linesproject_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"])ram_monitor = gr.Markdown()17 unmodified linesoutputs=[projects_display, project_selector])project_selector.change(fn=stream_logs,inputs=project_selector,outputs=logs_display)demo.load(3 unmodified linesgr.HTML("""<style>.logs-container { height: 600px; 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>""")1234567107 unmodified lines11511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315433 unmodified lines18818919019119219319419519617 unmodified lines2142152162172182192202212222232243 unmodified lines228229230231232233234# 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 linesyield get_system_stats()time.sleep(5)### MODIFIED ###def stream_logs(project_id):"""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.", gr.update(visible=False)return# On first run for a new selection, ensure the download button is hiddenyield "Fetching logs...", gr.update(visible=False)while True:project = get_project(project_id)if project:logs = project['logs'] if project['logs'] else f"🕒 Project #{project_id} is queued or starting... Logs will appear here shortly."# Check for terminal statesif 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 loopelif project['status'] == 'failed':yield logs, gr.update(visible=False)break # Stop the loop# Otherwise, just stream the current logsyield logs, gr.update(visible=False)else:yield f"Error: Project #{project_id} not found.", gr.update(visible=False)breaktime.sleep(3)33 unmodified linesproject_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 hiddendownload_zip_ui = gr.File(label="Download Project ZIP", visible=False, interactive=False)ram_monitor = gr.Markdown()17 unmodified linesoutputs=[projects_display, project_selector])### MODIFIED ###project_selector.change(fn=stream_logs,inputs=project_selector,outputs=[logs_display, download_zip_ui] # Now updates both logs and the download component)demo.load(3 unmodified linesgr.HTML("""<style>.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