Keeby-smilyai commited on
Commit
0afb014
Β·
verified Β·
1 Parent(s): 102ac7f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -15
app.py CHANGED
@@ -1,7 +1,7 @@
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,
@@ -44,6 +44,21 @@ def get_project_choices(user_id):
44
  projects = get_user_projects(user_id)
45
  return [(f"[{p['id']}] {p['title']}", p['id']) for p in projects] if projects else []
46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  # --- CORE LOGIC HANDLERS FOR UI ---
48
 
49
  def login_and_setup(username, password, user_state):
@@ -73,7 +88,8 @@ def login_and_setup(username, password, user_state):
73
  gr.update(visible=True),
74
  gr.update(value=welcome_message),
75
  gr.update(value=format_projects_list(projects)),
76
- gr.update(choices=get_project_choices(user_id), value=None)
 
77
  )
78
  return user_state, gr.update(), gr.update()
79
 
@@ -82,11 +98,11 @@ def start_project_flow(prompt, user_state):
82
  """The complete, seamless flow for starting a new project."""
83
  user_id = user_state.get('user_id')
84
  if not user_id:
85
- return "Error: Not logged in.", "", "", None, gr.update()
86
 
87
  project_id = create_project(user_id, "New AI Project", prompt)
88
  if not project_id:
89
- return "Error: Could not create project.", "", "", None, gr.update()
90
 
91
  queue_job(project_id, user_id, prompt)
92
 
@@ -97,15 +113,21 @@ def start_project_flow(prompt, user_state):
97
  gr.update(value=format_projects_list(projects)),
98
  gr.update(choices=get_project_choices(user_id), value=project_id),
99
  gr.Tabs(selected="logs_tab"),
 
100
  )
101
 
102
  def refresh_all_projects(user_state):
103
  """Manually refresh project lists on both tabs."""
104
  user_id = user_state.get('user_id')
105
- if not user_id: return gr.update(), gr.update()
 
106
 
107
  projects = get_user_projects(user_id)
108
- return gr.update(value=format_projects_list(projects)), gr.update(choices=get_project_choices(user_id))
 
 
 
 
109
 
110
  # --- CUSTOM GENERATOR LOOPS FOR LIVE UPDATES ---
111
 
@@ -115,7 +137,6 @@ def system_stats_loop():
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:
@@ -189,9 +210,15 @@ with gr.Blocks(title="Code Agents Pro", theme=gr.themes.Soft()) as demo:
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
 
197
  # ------------------------------ EVENT WIRING ------------------------------
@@ -199,26 +226,51 @@ with gr.Blocks(title="Code Agents Pro", theme=gr.themes.Soft()) as demo:
199
  login_btn.click(
200
  fn=login_and_setup,
201
  inputs=[username_input, password_input, user_state],
202
- outputs=[user_state, login_ui, main_ui_container, welcome_msg, projects_display, project_selector]
 
 
 
 
 
 
 
 
203
  )
204
 
205
  start_btn.click(
206
  fn=start_project_flow,
207
  inputs=[project_prompt, user_state],
208
- outputs=[status_msg, project_prompt, projects_display, project_selector, tabs]
 
 
 
 
 
 
 
209
  )
210
 
211
  refresh_all_btn.click(
212
  fn=refresh_all_projects,
213
  inputs=[user_state],
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(
 
1
+ # app.py β€” FINAL VERSION (with Download and Files Tab)
2
  import gradio as gr
3
  import time
4
+ import os
5
  from backend import (
6
  create_user,
7
  get_user_by_username,
 
44
  projects = get_user_projects(user_id)
45
  return [(f"[{p['id']}] {p['title']}", p['id']) for p in projects] if projects else []
46
 
47
+ def format_files_list(projects):
48
+ """Formats completed projects into downloadable file links."""
49
+ completed_projects = [p for p in projects if p['status'] == 'completed']
50
+ if not completed_projects:
51
+ return "No completed projects yet. Your finished work will appear here!"
52
+
53
+ file_lines = []
54
+ for p in completed_projects:
55
+ zip_path = p.get('zip_path')
56
+ if zip_path and os.path.exists(zip_path):
57
+ file_lines.append(f"πŸ“₯ **[{p['id']}] {p['title']}** - [Download ZIP]({zip_path})")
58
+ else:
59
+ file_lines.append(f"⚠️ **[{p['id']}] {p['title']}** - ZIP file missing")
60
+ return "\n".join(file_lines)
61
+
62
  # --- CORE LOGIC HANDLERS FOR UI ---
63
 
64
  def login_and_setup(username, password, user_state):
 
88
  gr.update(visible=True),
89
  gr.update(value=welcome_message),
90
  gr.update(value=format_projects_list(projects)),
91
+ gr.update(choices=get_project_choices(user_id), value=None),
92
+ gr.update(value=format_files_list(projects)) # For files tab
93
  )
94
  return user_state, gr.update(), gr.update()
95
 
 
98
  """The complete, seamless flow for starting a new project."""
99
  user_id = user_state.get('user_id')
100
  if not user_id:
101
+ return "Error: Not logged in.", "", "", None, gr.update(), gr.update()
102
 
103
  project_id = create_project(user_id, "New AI Project", prompt)
104
  if not project_id:
105
+ return "Error: Could not create project.", "", "", None, gr.update(), gr.update()
106
 
107
  queue_job(project_id, user_id, prompt)
108
 
 
113
  gr.update(value=format_projects_list(projects)),
114
  gr.update(choices=get_project_choices(user_id), value=project_id),
115
  gr.Tabs(selected="logs_tab"),
116
+ gr.update(value=format_files_list(projects)) # Update files tab
117
  )
118
 
119
  def refresh_all_projects(user_state):
120
  """Manually refresh project lists on both tabs."""
121
  user_id = user_state.get('user_id')
122
+ if not user_id:
123
+ return gr.update(), gr.update(), gr.update()
124
 
125
  projects = get_user_projects(user_id)
126
+ return (
127
+ gr.update(value=format_projects_list(projects)),
128
+ gr.update(choices=get_project_choices(user_id)),
129
+ gr.update(value=format_files_list(projects))
130
+ )
131
 
132
  # --- CUSTOM GENERATOR LOOPS FOR LIVE UPDATES ---
133
 
 
137
  yield get_system_stats()
138
  time.sleep(5)
139
 
 
140
  def stream_logs(project_id):
141
  """A generator that streams logs and provides a download link when complete."""
142
  if not project_id:
 
210
  with gr.Group(elem_classes=["logs-container"]):
211
  logs_display = gr.Markdown("Logs will appear here in real-time.", elem_classes=["monospace"])
212
 
213
+ # File download component, initially hidden
214
  download_zip_ui = gr.File(label="Download Project ZIP", visible=False, interactive=False)
215
+
216
+ # NEW FILES TAB
217
+ with gr.Tab("πŸ“ Files", id="files_tab"):
218
+ gr.Markdown("### πŸ“¦ Download Completed Projects")
219
+ refresh_files_btn = gr.Button("πŸ”„ Refresh Files")
220
+ files_display = gr.Markdown("Login to see your completed projects.")
221
+
222
  ram_monitor = gr.Markdown()
223
 
224
  # ------------------------------ EVENT WIRING ------------------------------
 
226
  login_btn.click(
227
  fn=login_and_setup,
228
  inputs=[username_input, password_input, user_state],
229
+ outputs=[
230
+ user_state,
231
+ login_ui,
232
+ main_ui_container,
233
+ welcome_msg,
234
+ projects_display,
235
+ project_selector,
236
+ files_display # New files tab content
237
+ ]
238
  )
239
 
240
  start_btn.click(
241
  fn=start_project_flow,
242
  inputs=[project_prompt, user_state],
243
+ outputs=[
244
+ status_msg,
245
+ project_prompt,
246
+ projects_display,
247
+ project_selector,
248
+ tabs,
249
+ files_display # Update files tab when new project starts
250
+ ]
251
  )
252
 
253
  refresh_all_btn.click(
254
  fn=refresh_all_projects,
255
  inputs=[user_state],
256
+ outputs=[
257
+ projects_display,
258
+ project_selector,
259
+ files_display # Also refresh files tab
260
+ ]
261
+ )
262
+
263
+ # NEW: Refresh files tab button
264
+ refresh_files_btn.click(
265
+ fn=lambda user_state: gr.update(value=format_files_list(get_user_projects(user_state.get('user_id', 0)))) if user_state.get('user_id') else gr.update(),
266
+ inputs=[user_state],
267
+ outputs=[files_display]
268
  )
269
 
 
270
  project_selector.change(
271
  fn=stream_logs,
272
  inputs=project_selector,
273
+ outputs=[logs_display, download_zip_ui]
274
  )
275
 
276
  demo.load(