Spaces:
Runtime error
Runtime error
File size: 11,004 Bytes
7c1ca6d 9eb402c 5c6eb51 d3d626e f38368c f04fa4e d3d626e 035cde8 9193b16 39bc928 9193b16 39bc928 f04fa4e 39bc928 f04fa4e 39bc928 f04fa4e 39bc928 5c6eb51 f04fa4e 5c6eb51 39bc928 f04fa4e 39bc928 f04fa4e 7c1ca6d f04fa4e f38368c f04fa4e 7c1ca6d 035cde8 b59f462 f04fa4e 7c1ca6d f04fa4e f38368c f04fa4e 39bc928 f04fa4e 91bc418 f38368c 9eb402c 91bc418 f04fa4e 39bc928 91bc418 f04fa4e 91bc418 f04fa4e 5c6eb51 f04fa4e f38368c f04fa4e f38368c f04fa4e 91bc418 9193b16 39bc928 9193b16 f04fa4e d3d626e f04fa4e 7c1ca6d f04fa4e 5c6eb51 f04fa4e 9193b16 f04fa4e f38368c f04fa4e f38368c f04fa4e 7c1ca6d f04fa4e f38368c f04fa4e f38368c f04fa4e 7a9f16f f04fa4e 286665b 7a9f16f f04fa4e 035cde8 7c1ca6d | 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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 | import gradio as gr
import time
import os
from backend import (
init_db,
create_user,
get_user_by_username,
verify_password,
create_project,
get_user_projects,
get_project,
queue_job,
)
import psutil
import torch
# --- UI HELPER FUNCTIONS ---
def get_system_stats():
"""Returns a markdown string with current RAM and VRAM usage."""
ram_used = round(psutil.virtual_memory().used / (1024**3), 2)
ram_total = round(psutil.virtual_memory().total / (1024**3), 2)
stats_str = f"π System RAM: {ram_used}GB / {ram_total}GB"
if torch.cuda.is_available():
vram_reserved = round(torch.cuda.memory_reserved() / (1024**3), 2)
vram_total = round(torch.cuda.get_device_properties(0).total_memory / (1024**3), 2)
stats_str += f" | VRAM: {vram_reserved}GB / {vram_total}GB"
return stats_str
def format_projects_list(projects):
"""Formats a list of project records into a readable markdown string."""
if not projects:
return "No projects yet. Start one on the left!"
project_lines = []
for p in projects:
status_icon = {"queued": "π", "running": "βοΈ", "completed": "β
", "failed": "β"}.get(p['status'], "β")
project_lines.append(f"{status_icon} **[{p['id']}] {p['title']}** ({p['status']})")
return "\n".join(project_lines)
def get_project_choices(user_id):
"""Fetches user projects and formats them for a dropdown."""
if not user_id: return []
projects = get_user_projects(user_id)
return [(f"[{p['id']}] {p['title']}", p['id']) for p in projects] if projects else []
def format_files_list(projects):
"""Formats completed projects into downloadable file links."""
completed_projects = [p for p in projects if p['status'] == 'completed']
if not completed_projects:
return "No completed projects yet. Your finished work will appear here!"
file_lines = []
for p in completed_projects:
zip_path = p.get('zip_path')
if zip_path and os.path.exists(zip_path):
file_lines.append(f"π₯ **[{p['id']}] {p['title']}** - [Download ZIP]({zip_path})")
else:
file_lines.append(f"β οΈ **[{p['id']}] {p['title']}** - ZIP file missing")
return "\n".join(file_lines)
# --- CORE LOGIC HANDLERS FOR UI ---
def login_and_setup(username, password, user_state):
"""Handles login/signup and prepares the main UI if successful."""
user = get_user_by_username(username)
user_id, welcome_message = None, ""
if user and verify_password(password, user['password_hash']):
user_id = user['id']
welcome_message = f"Welcome back, {username}! π¨βπ»"
else:
new_user_id = create_user(username, password)
if new_user_id:
user_id = new_user_id
welcome_message = f"Account created! Welcome, {username}!"
else:
return user_state, gr.update(), gr.update(value="Login failed or username is taken.")
if user_id:
user_state['user_id'] = user_id
user_state['username'] = username
projects = get_user_projects(user_id)
return (
user_state,
gr.update(visible=False),
gr.update(visible=True),
gr.update(value=welcome_message),
gr.update(value=format_projects_list(projects)),
gr.update(choices=get_project_choices(user_id), value=None),
gr.update(value=format_files_list(projects))
)
return user_state, gr.update(), gr.update()
def start_project_flow(prompt, user_state):
"""The complete, seamless flow for starting a new project."""
user_id = user_state.get('user_id')
if not user_id:
return "Error: Not logged in.", "", "", gr.update(value=""), gr.update(), gr.update(), gr.update()
project_id = create_project(user_id, "New AI Project", prompt)
if not project_id:
return "Error: Could not create project.", "", "", gr.update(value=""), gr.update(), gr.update(), gr.update()
queue_job(project_id, user_id, prompt)
projects = get_user_projects(user_id)
return (
f"β
Project #{project_id} started! Switching to logs...",
gr.update(value=""),
gr.update(value=format_projects_list(projects)),
gr.update(choices=get_project_choices(user_id), value=project_id),
gr.Tabs(selected="logs_tab"),
gr.update(value=format_files_list(projects))
)
def refresh_all_projects(user_state):
"""Manually refresh project lists on both tabs."""
user_id = user_state.get('user_id')
if not user_id:
return gr.update(), gr.update(), gr.update()
projects = get_user_projects(user_id)
return (
gr.update(value=format_projects_list(projects)),
gr.update(choices=get_project_choices(user_id)),
gr.update(value=format_files_list(projects))
)
# NEW: Refresh files tab button
def refresh_files(user_state):
user_id = user_state.get('user_id')
if not user_id: return gr.update()
projects = get_user_projects(user_id)
return gr.update(value=format_files_list(projects))
# --- CUSTOM GENERATOR LOOPS FOR LIVE UPDATES ---
def system_stats_loop():
"""A generator that yields system stats every 5 seconds, forever."""
while True:
yield get_system_stats()
time.sleep(5)
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
yield "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."
if project['status'] == 'completed':
final_log = logs + "\n\nβ
**Project Completed!** Your file is ready for download."
zip_path = project['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
elif project['status'] == 'failed':
yield logs, gr.update(visible=False)
break
yield logs, gr.update(visible=False)
else:
yield f"Error: Project #{project_id} not found.", gr.update(visible=False)
break
time.sleep(3)
# --- GRADIO UI DEFINITION ---
with gr.Blocks(title="Code Agents Pro", theme=gr.themes.Soft()) as demo:
user_state = gr.State({"user_id": None, "username": ""})
gr.Markdown("# π€ Code Agents Pro β Your Automated AI Software Team")
with gr.Group(visible=True) as login_ui:
gr.Markdown("### π Login or Sign Up")
username_input = gr.Textbox(label="Username", placeholder="Choose a unique username")
password_input = gr.Textbox(label="Password", type="password", placeholder="Enter your password")
login_btn = gr.Button("Login / Sign Up", variant="primary")
login_msg = gr.Markdown()
with gr.Group(visible=False) as main_ui_container:
welcome_msg = gr.Markdown()
with gr.Tabs() as tabs:
with gr.Tab("π Dashboard", id="dashboard_tab"):
with gr.Row():
with gr.Column(scale=2):
gr.Markdown("### β¨ Start a New Project")
project_prompt = gr.Textbox(label="Describe your project goal in detail", lines=8, placeholder="e.g., 'Build a Python FastAPI server...'")
start_btn = gr.Button("π Start AI Team Work", variant="primary")
status_msg = gr.Markdown()
with gr.Column(scale=3):
gr.Markdown("### π Your Projects")
refresh_all_btn = gr.Button("π Refresh Project List")
projects_display = gr.Markdown("Login to see your projects.")
with gr.Tab("π§ Agent Logs", id="logs_tab"):
gr.Markdown("### π View Agent Conversation Logs")
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"])
download_zip_ui = gr.File(label="Download Project ZIP", visible=False, interactive=False)
with gr.Tab("π Files", id="files_tab"):
gr.Markdown("### π¦ Download Completed Projects")
refresh_files_btn = gr.Button("π Refresh Files")
files_display = gr.Markdown("Login to see your completed projects.")
ram_monitor = gr.Markdown()
# ------------------------------ EVENT WIRING ------------------------------
login_btn.click(
fn=login_and_setup,
inputs=[username_input, password_input, user_state],
outputs=[
user_state,
login_ui,
main_ui_container,
welcome_msg,
projects_display,
project_selector,
files_display
]
)
start_btn.click(
fn=start_project_flow,
inputs=[project_prompt, user_state],
outputs=[
status_msg,
project_prompt,
projects_display,
project_selector,
tabs,
files_display
]
)
refresh_all_btn.click(
fn=refresh_all_projects,
inputs=[user_state],
outputs=[
projects_display,
project_selector,
files_display
]
)
refresh_files_btn.click(
fn=refresh_files,
inputs=[user_state],
outputs=[files_display]
)
project_selector.change(
fn=stream_logs,
inputs=project_selector,
outputs=[logs_display, download_zip_ui]
)
demo.load(
fn=system_stats_loop,
outputs=ram_monitor
)
demo.load(init_db, inputs=None, outputs=None, show_progress=False)
gr.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>
""")
demo.queue().launch(server_name="0.0.0.0", server_port=7860) |