Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
-
# app.py — FINAL VERSION
|
| 2 |
import gradio as gr
|
| 3 |
import time
|
|
|
|
| 4 |
from backend import (
|
| 5 |
create_user,
|
| 6 |
get_user_by_username,
|
|
@@ -114,24 +115,40 @@ def system_stats_loop():
|
|
| 114 |
yield get_system_stats()
|
| 115 |
time.sleep(5)
|
| 116 |
|
|
|
|
| 117 |
def stream_logs(project_id):
|
| 118 |
-
"""A generator that streams logs
|
| 119 |
if not project_id:
|
| 120 |
-
yield "⬅️ Select a project from the dropdown to see its live logs."
|
| 121 |
return
|
| 122 |
|
|
|
|
|
|
|
|
|
|
| 123 |
while True:
|
| 124 |
project = get_project(project_id)
|
| 125 |
if project:
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
|
| 132 |
-
|
|
|
|
| 133 |
else:
|
| 134 |
-
yield f"Error: Project #{project_id} not found."
|
| 135 |
break
|
| 136 |
time.sleep(3)
|
| 137 |
|
|
@@ -171,6 +188,9 @@ with gr.Blocks(title="Code Agents Pro", theme=gr.themes.Soft()) as demo:
|
|
| 171 |
project_selector = gr.Dropdown(label="Select Project to View", choices=[], interactive=True)
|
| 172 |
with gr.Group(elem_classes=["logs-container"]):
|
| 173 |
logs_display = gr.Markdown("Logs will appear here in real-time.", elem_classes=["monospace"])
|
|
|
|
|
|
|
|
|
|
| 174 |
|
| 175 |
ram_monitor = gr.Markdown()
|
| 176 |
|
|
@@ -194,10 +214,11 @@ with gr.Blocks(title="Code Agents Pro", theme=gr.themes.Soft()) as demo:
|
|
| 194 |
outputs=[projects_display, project_selector]
|
| 195 |
)
|
| 196 |
|
|
|
|
| 197 |
project_selector.change(
|
| 198 |
fn=stream_logs,
|
| 199 |
inputs=project_selector,
|
| 200 |
-
outputs=logs_display
|
| 201 |
)
|
| 202 |
|
| 203 |
demo.load(
|
|
@@ -207,7 +228,7 @@ with gr.Blocks(title="Code Agents Pro", theme=gr.themes.Soft()) as demo:
|
|
| 207 |
|
| 208 |
gr.HTML("""
|
| 209 |
<style>
|
| 210 |
-
.logs-container { height:
|
| 211 |
.monospace { font-family: 'SF Mono', 'Consolas', 'Courier New', monospace; font-size: 14px; white-space: pre-wrap; word-wrap: break-word; }
|
| 212 |
</style>
|
| 213 |
""")
|
|
|
|
| 1 |
+
# app.py — FINAL VERSION (with Download)
|
| 2 |
import gradio as gr
|
| 3 |
import time
|
| 4 |
+
import os # ### NEW ### - Needed for checking if the zip file exists
|
| 5 |
from backend import (
|
| 6 |
create_user,
|
| 7 |
get_user_by_username,
|
|
|
|
| 115 |
yield get_system_stats()
|
| 116 |
time.sleep(5)
|
| 117 |
|
| 118 |
+
### MODIFIED ###
|
| 119 |
def stream_logs(project_id):
|
| 120 |
+
"""A generator that streams logs and provides a download link when complete."""
|
| 121 |
if not project_id:
|
| 122 |
+
yield "⬅️ Select a project from the dropdown to see its live logs.", gr.update(visible=False)
|
| 123 |
return
|
| 124 |
|
| 125 |
+
# On first run for a new selection, ensure the download button is hidden
|
| 126 |
+
yield "Fetching logs...", gr.update(visible=False)
|
| 127 |
+
|
| 128 |
while True:
|
| 129 |
project = get_project(project_id)
|
| 130 |
if project:
|
| 131 |
+
logs = project['logs'] if project['logs'] else f"🕒 Project #{project_id} is queued or starting... Logs will appear here shortly."
|
| 132 |
+
|
| 133 |
+
# Check for terminal states
|
| 134 |
+
if project['status'] == 'completed':
|
| 135 |
+
final_log = logs + "\n\n✅ **Project Completed!** Your file is ready for download."
|
| 136 |
+
zip_path = project.get('zip_path')
|
| 137 |
+
if zip_path and os.path.exists(zip_path):
|
| 138 |
+
yield final_log, gr.update(value=zip_path, visible=True)
|
| 139 |
+
else:
|
| 140 |
+
yield logs + "\n\n⚠️ **Warning:** Project completed, but the ZIP file was not found.", gr.update(visible=False)
|
| 141 |
+
break # Stop the loop
|
| 142 |
+
|
| 143 |
+
elif project['status'] == 'failed':
|
| 144 |
+
yield logs, gr.update(visible=False)
|
| 145 |
+
break # Stop the loop
|
| 146 |
|
| 147 |
+
# Otherwise, just stream the current logs
|
| 148 |
+
yield logs, gr.update(visible=False)
|
| 149 |
+
|
| 150 |
else:
|
| 151 |
+
yield f"Error: Project #{project_id} not found.", gr.update(visible=False)
|
| 152 |
break
|
| 153 |
time.sleep(3)
|
| 154 |
|
|
|
|
| 188 |
project_selector = gr.Dropdown(label="Select Project to View", choices=[], interactive=True)
|
| 189 |
with gr.Group(elem_classes=["logs-container"]):
|
| 190 |
logs_display = gr.Markdown("Logs will appear here in real-time.", elem_classes=["monospace"])
|
| 191 |
+
|
| 192 |
+
# ### NEW ### - File download component, initially hidden
|
| 193 |
+
download_zip_ui = gr.File(label="Download Project ZIP", visible=False, interactive=False)
|
| 194 |
|
| 195 |
ram_monitor = gr.Markdown()
|
| 196 |
|
|
|
|
| 214 |
outputs=[projects_display, project_selector]
|
| 215 |
)
|
| 216 |
|
| 217 |
+
### MODIFIED ###
|
| 218 |
project_selector.change(
|
| 219 |
fn=stream_logs,
|
| 220 |
inputs=project_selector,
|
| 221 |
+
outputs=[logs_display, download_zip_ui] # Now updates both logs and the download component
|
| 222 |
)
|
| 223 |
|
| 224 |
demo.load(
|
|
|
|
| 228 |
|
| 229 |
gr.HTML("""
|
| 230 |
<style>
|
| 231 |
+
.logs-container { height: 550px; overflow-y: auto; border: 1px solid #E5E7EB; border-radius: 8px; padding: 8px; background-color: #F9FAFB; }
|
| 232 |
.monospace { font-family: 'SF Mono', 'Consolas', 'Courier New', monospace; font-size: 14px; white-space: pre-wrap; word-wrap: break-word; }
|
| 233 |
</style>
|
| 234 |
""")
|