Spaces:
Runtime error
Runtime error
File size: 11,784 Bytes
c759d25 22030f5 915df4b 0d89931 7318c6b 22030f5 892c58b 22030f5 892c58b 22030f5 5978528 22030f5 5978528 22030f5 5978528 22030f5 5978528 22030f5 5978528 22030f5 5978528 22030f5 5978528 22030f5 7318c6b 22030f5 5978528 22030f5 5978528 22030f5 5978528 22030f5 5167047 22030f5 5978528 3a83b97 f4fa1c7 5978528 f4fa1c7 5978528 22030f5 5167047 22030f5 5978528 22030f5 5167047 22030f5 5978528 22030f5 5978528 0d89931 d976f37 22030f5 7318c6b c759d25 7318c6b 22030f5 |
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 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
import gradio as gr
import time
from deepengineer.backend.gradio_tools import run_agent_stream
from deepengineer.common_path import DATA_DIR
# Custom CSS for hardware engineering theme
custom_css = """
.gradio-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
}
.main-header {
background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
color: white;
padding: 2rem;
border-radius: 10px;
margin-bottom: 2rem;
text-align: center;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
}
.main-header h1 {
margin: 0;
font-size: 2.5rem;
font-weight: 700;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}
.main-header p {
margin: 0.5rem 0 0 0;
font-size: 1.1rem;
opacity: 0.9;
}
.input-container {
background: transparent;
padding: 1rem;
border-radius: 10px;
margin-bottom: 1rem;
}
.log-container {
background: transparent;
border-radius: 10px;
padding: 1rem;
margin-bottom: 2rem;
}
.log-header {
display: flex;
align-items: center;
margin-bottom: 1rem;
padding: 0.5rem;
background: var(--background-fill-secondary);
border-radius: 5px;
}
.log-icon {
margin-right: 0.5rem;
color: var(--body-text-color);
}
.log-title {
font-weight: 600;
color: var(--body-text-color);
margin: 0;
}
.processing-indicator {
display: flex;
align-items: center;
justify-content: center;
padding: 1rem;
background: linear-gradient(135deg, #28a745, #20c997);
color: white;
border-radius: 10px;
margin-bottom: 1rem;
animation: pulse 2s infinite;
}
@keyframes pulse {
0% { opacity: 1; }
50% { opacity: 0.7; }
100% { opacity: 1; }
}
.spinner {
border: 0.1875rem solid #f3f3f3;
border-top: 0.1875rem solid #3498db;
border-radius: 50%;
width: 1.25rem;
height: 1.25rem;
animation: spin 1s linear infinite;
margin-right: 0.5rem;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
.response-container {
background: var(--background-fill-primary);
border: 1px solid var(--border-color-primary);
border-radius: 10px;
padding: 1.5rem;
margin-top: 1rem;
}
.send-button {
background: linear-gradient(135deg, #007bff, #0056b3) !important;
color: white !important;
border: none !important;
padding: 0.5rem 1.5rem !important;
border-radius: 0.5rem !important;
font-weight: 600 !important;
font-size: 0.95rem !important;
transition: all 0.3s ease !important;
box-shadow: 0 0.125rem 0.5rem rgba(0,123,255,0.3) !important;
min-width: 7.5rem !important;
max-width: 12.5rem !important;
}
.send-button:hover {
transform: translateY(-0.0625rem) !important;
box-shadow: 0 0.25rem 0.75rem rgba(0,123,255,0.4) !important;
}
.send-button:disabled {
background: #6c757d !important;
transform: none !important;
box-shadow: none !important;
}
.log-text {
font-family: 'Courier New', monospace;
font-size: 0.9rem;
line-height: 1.4;
color: var(--body-text-color);
background: var(--background-fill-secondary);
border: 0.0625rem solid var(--border-color-primary);
border-radius: 0.3125rem;
padding: 1rem;
max-height: 18.75rem;
overflow-y: auto;
}
.log-entry {
margin-bottom: 0.5rem;
padding: 0.25rem 0;
border-bottom: 1px solid var(--border-color-primary);
}
.log-entry:last-child {
border-bottom: none;
}
.timestamp {
color: var(--body-text-color-subdued);
font-size: 0.8rem;
}
.tool-name {
color: #007bff;
font-weight: 600;
}
.tool-description {
color: var(--body-text-color);
}
.hardware-icon {
font-size: 2rem;
margin-right: 1rem;
}
/* Dark mode specific adjustments */
.dark .input-container {
background: transparent;
}
.dark .log-container {
background: transparent;
}
.dark .response-container {
background: var(--background-fill-primary);
border-color: var(--border-color-primary);
}
.dark .log-text {
background: var(--background-fill-secondary);
border-color: var(--border-color-primary);
color: var(--body-text-color);
}
.dark .log-header {
background: var(--background-fill-secondary);
}
.dark .log-title {
color: var(--body-text-color);
}
.dark .log-icon {
color: var(--body-text-color);
}
.dark .timestamp {
color: var(--body-text-color-subdued);
}
.dark .tool-description {
color: var(--body-text-color);
}
.model-selector-compact {
background: transparent;
border: none;
padding: 0;
margin-bottom: 0.5rem;
}
.model-selector-compact .gr-dropdown {
background: var(--background-fill-primary);
color: var(--body-text-color);
border: 0.0625rem solid var(--border-color-primary);
border-radius: 0.375rem;
padding: 0.25rem 0.5rem;
font-size: 0.9rem;
max-width: 12.5rem !important;
}
.model-selector-compact .gr-dropdown:hover {
background: var(--background-fill-secondary);
border-color: var(--border-color-accent);
}
.model-selector-compact .gr-dropdown .gr-dropdown-label {
font-size: 0.85rem;
font-weight: 500;
margin-bottom: 0.25rem;
}
"""
def format_log_entry(log_text):
"""Format log entries with timestamps and better structure"""
if not log_text:
return ""
lines = log_text.split('\n')
formatted_lines = []
for line in lines:
if line.strip():
# Add timestamp and format tool calls
timestamp = time.strftime("%H:%M:%S")
if "Tool:" in line or "tool:" in line:
formatted_lines.append(f"<div class='log-entry'><span class='timestamp'>[{timestamp}]</span> <span class='tool-name'>π§ {line.strip()}</span></div>")
elif "Searching" in line or "searching" in line:
formatted_lines.append(f"<div class='log-entry'><span class='timestamp'>[{timestamp}]</span> <span class='tool-name'>π {line.strip()}</span></div>")
elif "Processing" in line or "processing" in line:
formatted_lines.append(f"<div class='log-entry'><span class='timestamp'>[{timestamp}]</span> <span class='tool-name'>βοΈ {line.strip()}</span></div>")
elif "Drawing" in line or "drawing" in line:
formatted_lines.append(f"<div class='log-entry'><span class='timestamp'>[{timestamp}]</span> <span class='tool-name'>π¨ {line.strip()}</span></div>")
else:
formatted_lines.append(f"<div class='log-entry'><span class='timestamp'>[{timestamp}]</span> <span class='tool-description'>{line.strip()}</span></div>")
return "".join(formatted_lines)
def run_agent_with_ui(user_input: str, model_id: str):
"""Enhanced agent runner with better UI feedback"""
if not user_input.strip():
return "", ""
# Initialize empty outputs
log_buffer = ""
agent_output = ""
# Stream from the agent
for agent_result, log_result in run_agent_stream(user_input, model_id):
if log_result:
log_buffer = log_result
if agent_result:
agent_output = agent_result
# Format the log for display
formatted_log = format_log_entry(log_buffer)
yield agent_output, formatted_log
# Create the Gradio interface
with gr.Blocks(css=custom_css, title="DeepDraft - Hardware Engineering Assistant") as demo:
# Header with hardware engineering theme
with gr.Row():
gr.HTML("""
<div class="main-header">
<h1>π§ DeepDraft</h1>
<p>Advanced Hardware Engineering Assistant</p>
<p>Powered by AI-driven analysis and visualization</p>
</div>
""")
# Model selection (compact)
with gr.Row():
with gr.Column(scale=1):
model_selector = gr.Dropdown(
choices=[
("π€ Mistral Medium", "mistral/mistral-medium-latest"),
("π§ DeepSeek Reasoner", "deepseek/deepseek-reasoner"),
("π DeepSeek Chat", "deepseek/deepseek-chat"),
("β‘ Mistral Small", "mistral/mistral-small-latest"),
],
value="mistral/mistral-medium-latest",
label="π€ AI Model",
info="Choose the AI model for analysis",
elem_classes=["model-selector-compact"],
scale=1
)
# Main input section
with gr.Row():
with gr.Column(scale=3):
gr.HTML('<div class="input-container">')
user_input = gr.Textbox(
label="π Describe your hardware engineering query",
placeholder="e.g., 'Design a thermal management system for a high-power LED array' or 'Analyze the stress distribution in a mechanical bracket'",
lines=3,
max_lines=5
)
gr.HTML('</div>')
# Send button
with gr.Row():
send_button = gr.Button("π Launch Analysis", variant="primary", elem_classes=["send-button"])
# Processing indicator (hidden by default)
processing_indicator = gr.HTML(
"""
<div class="processing-indicator" style="display: none;">
<div class="spinner"></div>
<strong>DeepDraft is analyzing your request...</strong>
</div>
""",
visible=False
)
# Log output section
with gr.Row():
with gr.Column(scale=1):
gr.HTML("""
<div class="log-container">
<div class="log-header">
<span class="log-icon">π</span>
<h3 class="log-title">Analysis Progress</h3>
</div>
</div>
""")
log_output = gr.HTML(
value="<div class='log-text'>Ready to analyze your hardware engineering query...</div>",
elem_classes=["log-text"]
)
# Agent response section
with gr.Row():
with gr.Column(scale=1):
gr.HTML("""
<div class="response-container">
<h3>π― Analysis Results</h3>
</div>
""")
agent_output = gr.Markdown(
value="Enter your query above to begin the analysis.",
elem_classes=["response-container"]
)
# Event handlers
def on_send_click(user_input, model_id):
if not user_input.strip():
return "", "", gr.HTML(visible=False)
# Show processing indicator
processing_html = """
<div class="processing-indicator">
<div class="spinner"></div>
<strong>DeepDraft is analyzing your request...</strong>
</div>
"""
return "", "", gr.HTML(processing_html, visible=True)
def on_generate_complete(agent_result, log_result):
# Hide processing indicator when complete
return agent_result, log_result, gr.HTML(visible=False)
# Connect the button click
send_button.click(
fn=on_send_click,
inputs=[user_input, model_selector],
outputs=[agent_output, log_output, processing_indicator]
).then(
fn=run_agent_with_ui,
inputs=[user_input, model_selector],
outputs=[agent_output, log_output],
concurrency_limit=10
).then(
fn=on_generate_complete,
inputs=[agent_output, log_output],
outputs=[agent_output, log_output, processing_indicator]
)
if __name__ == "__main__":
demo.launch(
allowed_paths=[DATA_DIR],
server_name="0.0.0.0",
server_port=7860,
share=False
)
|