-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathapp.py
More file actions
557 lines (467 loc) · 20.4 KB
/
app.py
File metadata and controls
557 lines (467 loc) · 20.4 KB
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
"""
NeuralNav - E2E LLM Deployment Recommendation System
A Streamlit application for AI-powered LLM deployment recommendations.
Usage:
streamlit run ui/app.py
"""
import logging
import sys
import time
from pathlib import Path
# Add ui/ to sys.path so modules can use flat imports
sys.path.insert(0, str(Path(__file__).parent))
import pandas as pd
import streamlit as st
import streamlit.components.v1 as components
from api_client import (
extract_business_context,
fetch_priority_weights,
fetch_ranked_recommendations,
load_206_models,
)
from components.deployment import render_deployment_tab
from components.deployment_management import render_deployment_management_tab
from components.dialogs import (
show_category_dialog,
show_full_table_dialog,
show_winner_details_dialog,
)
from components.extraction import (
render_extraction_edit_form,
render_extraction_result,
render_extraction_with_approval,
)
from components.recommendations import render_recommendation_result
from components.settings import render_configuration_tab
from components.slo import render_slo_with_approval
from state import init_session_state
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
datefmt="%Y-%m-%d %H:%M:%S",
)
logger = logging.getLogger(__name__)
# =============================================================================
# PAGE CONFIGURATION (must be first Streamlit call)
# =============================================================================
st.set_page_config(
page_title="NeuralNav",
page_icon="docs/neuralnav-logo-32.png",
layout="wide",
initial_sidebar_state="expanded",
)
# =============================================================================
# MINIMAL CSS OVERRIDES
# =============================================================================
st.markdown(
"""
<style>
/* Reduce top whitespace and align content with toolbar */
.block-container { padding-top: 0 !important; }
/* Transparent header so menu appears inline with content */
header[data-testid="stHeader"] { background: transparent; }
</style>
""",
unsafe_allow_html=True,
)
# =============================================================================
# SESSION STATE INIT
# =============================================================================
init_session_state()
# =============================================================================
# VISUAL COMPONENTS
# =============================================================================
def render_hero():
"""Render compact hero section with logo."""
logo_col, title_col = st.columns([1, 11], vertical_alignment="center")
with logo_col:
st.image("ui/static/neuralnav-logo.png", width=48)
with title_col:
st.title("NeuralNav")
st.caption(
"AI-Powered LLM Deployment Recommendations — From Natural Language to Production in Seconds"
)
# =============================================================================
# TAB FUNCTIONS
# =============================================================================
def render_use_case_input_tab(priority: str, models_df: pd.DataFrame):
"""Tab 1: Use case input interface."""
def clear_dialog_states():
"""Clear all dialog and expanded states when starting a new use case."""
st.session_state.show_full_table_dialog = False
st.session_state.show_category_dialog = False
st.session_state.show_winner_dialog = False
st.session_state.show_options_list_expanded = False
# Transfer pending input from button clicks before rendering the text_area widget
if "pending_user_input" in st.session_state:
st.session_state.user_input = st.session_state.pending_user_input
del st.session_state.pending_user_input
st.subheader("Describe your use case or select from 9 predefined scenarios")
# Input area
st.text_area(
"Your requirements:",
key="user_input",
height=120,
max_chars=2000,
placeholder="Describe your LLM use case in natural language...\n\nExample: I need a chatbot for customer support with 30 users. Low latency is important, and we have H100 GPUs available.",
label_visibility="collapsed",
)
# Row 1: 5 task buttons
col1, col2, col3, col4, col5 = st.columns(5)
with col1:
if st.button("Chat Completion", use_container_width=True, key="task_chat"):
clear_dialog_states()
st.session_state.pending_user_input = "Customer service chatbot for 30 users."
st.rerun()
with col2:
if st.button("Code Completion", use_container_width=True, key="task_code"):
clear_dialog_states()
st.session_state.pending_user_input = "IDE code completion tool for 300 developers."
st.rerun()
with col3:
if st.button("Document Q&A", use_container_width=True, key="task_rag"):
clear_dialog_states()
st.session_state.pending_user_input = (
"Document Q&A system for enterprise knowledge base, 300 users."
)
st.rerun()
with col4:
if st.button("Summarization", use_container_width=True, key="task_summ"):
clear_dialog_states()
st.session_state.pending_user_input = (
"News article summarization for 300 users, cost-effective solution preferred."
)
st.rerun()
with col5:
if st.button("Legal Analysis", use_container_width=True, key="task_legal"):
clear_dialog_states()
st.session_state.pending_user_input = (
"Legal document analysis for 300 lawyers, accuracy is critical."
)
st.rerun()
# Row 2: 4 more task buttons
col6, col7, col8, col9 = st.columns(4)
with col6:
if st.button("Translation", use_container_width=True, key="task_trans"):
clear_dialog_states()
st.session_state.pending_user_input = (
"Multi-language translation service for 300 users."
)
st.rerun()
with col7:
if st.button("Content Generation", use_container_width=True, key="task_content"):
clear_dialog_states()
st.session_state.pending_user_input = (
"Content generation tool for marketing team, 300 users."
)
st.rerun()
with col8:
if st.button("Long Doc Summary", use_container_width=True, key="task_longdoc"):
clear_dialog_states()
st.session_state.pending_user_input = (
"Long document summarization for research papers, 30 researchers, accuracy matters."
)
st.rerun()
with col9:
if st.button("Code Generation", use_container_width=True, key="task_codegen"):
clear_dialog_states()
st.session_state.pending_user_input = (
"Full code generation tool for implementing features, 30 developers."
)
st.rerun()
# Show character count
char_count = len(st.session_state.user_input) if st.session_state.user_input else 0
st.markdown(
f'<div style="text-align: right; font-size: 0.75rem; margin-top: -0.5rem;">{char_count}/2000 characters</div>',
unsafe_allow_html=True,
)
col1, col2, col3 = st.columns([1.5, 1, 2])
with col1:
analyze_disabled = (
len(st.session_state.user_input.strip()) < 10 if st.session_state.user_input else True
)
analyze_clicked = st.button(
"Analyze Use Case", type="primary", use_container_width=True, disabled=analyze_disabled
)
if (
analyze_disabled
and st.session_state.user_input
and len(st.session_state.user_input.strip()) < 10
):
st.caption("Please enter at least 10 characters")
with col2:
if st.button("Clear", use_container_width=True):
for key in [
"user_input",
"extraction_result",
"recommendation_result",
"extraction_approved",
"slo_approved",
"edited_extraction",
"custom_ttft",
"custom_itl",
"custom_e2e",
"custom_qps",
"used_priority",
]:
if key in st.session_state:
del st.session_state[key]
st.session_state.user_input = ""
st.rerun()
# Input validation before analysis
if (
analyze_clicked
and st.session_state.user_input
and len(st.session_state.user_input.strip()) >= 10
):
# Reset workflow state
st.session_state.extraction_approved = None
st.session_state.slo_approved = None
st.session_state.recommendation_result = None
st.session_state.edited_extraction = None
# Clear previous recommendation selection and deployment state
st.session_state.deployment_selected_config = None
st.session_state.deployment_selected_category = None
st.session_state.deployment_yaml_generated = False
st.session_state.deployment_yaml_files = {}
st.session_state.deployment_id = None
st.session_state.deployment_error = None
progress_container = st.empty()
with progress_container:
progress_bar = st.progress(0, text="Initializing extraction...")
try:
progress_bar.progress(20, text="Analyzing input text...")
extraction = extract_business_context(st.session_state.user_input)
progress_bar.progress(80, text="Extraction complete!")
if extraction:
st.session_state.recommendation_result = None
st.session_state.extraction_approved = None
st.session_state.slo_approved = None
st.session_state.edited_extraction = None
st.session_state.ranked_response = None
for key in [
"accuracy_priority",
"cost_priority",
"latency_priority",
"weight_accuracy",
"weight_cost",
"weight_latency",
]:
if key in st.session_state:
del st.session_state[key]
st.session_state.extraction_result = extraction
priority_config = fetch_priority_weights()
pw_map = priority_config.get("priority_weights", {}) if priority_config else {}
defaults_cfg = priority_config.get("defaults", {}) if priority_config else {}
default_weights = defaults_cfg.get(
"weights", {"accuracy": 5, "cost": 4, "latency": 2}
)
st.session_state.accuracy_priority = extraction.get("accuracy_priority", "medium")
st.session_state.cost_priority = extraction.get("cost_priority", "medium")
st.session_state.latency_priority = extraction.get("latency_priority", "medium")
st.session_state.weight_accuracy = pw_map.get("accuracy", {}).get(
st.session_state.accuracy_priority, default_weights["accuracy"]
)
st.session_state.weight_cost = pw_map.get("cost", {}).get(
st.session_state.cost_priority, default_weights["cost"]
)
st.session_state.weight_latency = pw_map.get("latency", {}).get(
st.session_state.latency_priority, default_weights["latency"]
)
logger.info(
f"Initialized priorities from extraction: accuracy={st.session_state.accuracy_priority}, "
f"cost={st.session_state.cost_priority}, latency={st.session_state.latency_priority}"
)
logger.info(
f"Initialized weights: accuracy={st.session_state.weight_accuracy}, "
f"cost={st.session_state.weight_cost}, latency={st.session_state.weight_latency}"
)
st.session_state.new_extraction_available = True
st.session_state.used_priority = extraction.get("priority", priority)
st.session_state.detected_use_case = extraction.get(
"use_case", "chatbot_conversational"
)
progress_bar.progress(100, text="Ready!")
else:
st.error("Could not extract business context. Please try rephrasing your input.")
progress_bar.empty()
except Exception:
st.error("An error occurred during analysis. Please try again.")
progress_bar.empty()
finally:
time.sleep(0.5)
progress_container.empty()
# Get the priority that was actually used
used_priority = st.session_state.get("used_priority", priority)
# Show extraction with approval if extraction exists but not approved
if st.session_state.extraction_result and st.session_state.extraction_approved is None:
render_extraction_with_approval(st.session_state.extraction_result, models_df)
return
# If editing, show edit form
if st.session_state.extraction_approved is False:
render_extraction_edit_form(st.session_state.extraction_result, models_df)
return
# If approved, show message to proceed to Technical Specifications tab
if st.session_state.extraction_approved is True:
render_extraction_result(st.session_state.extraction_result, used_priority)
st.markdown(
"""
<div style="padding: 0.75rem 1rem; border-radius: 8px; font-size: 1rem; margin-bottom: 0.75rem; max-width: 50%;">
<strong>Step 1 Complete</strong> · You can now go to Technical Specification
</div>
""",
unsafe_allow_html=True,
)
def render_technical_specs_tab():
"""Tab 2: Technical Specification (SLO targets and workload settings)."""
if not st.session_state.extraction_approved:
st.markdown(
"""
<div style="padding: 1.5rem; border-radius: 8px; text-align: center; ">
<strong style="font-size: 1.1rem;">Complete Step 1 First</strong><br>
<span style="font-size: 0.95rem; ">Go to the <strong>Define Use Case</strong> tab to describe your use case and approve the extraction.</span>
</div>
""",
unsafe_allow_html=True,
)
return
final_extraction = (
st.session_state.edited_extraction or st.session_state.extraction_result or {}
)
render_slo_with_approval(final_extraction)
if st.session_state.slo_approved is True:
st.markdown(
"""
<div style="padding: 0.75rem 1rem; border-radius: 8px; font-size: 1rem; margin-bottom: 0.75rem; max-width: 50%;">
<strong>Step 2 Complete</strong> · You can now view Recommendations
</div>
""",
unsafe_allow_html=True,
)
def render_results_tab(priority: str, models_df: pd.DataFrame):
"""Tab 3: Results display - Best Model Recommendations."""
used_priority = st.session_state.get("used_priority", priority)
if not st.session_state.slo_approved:
if not st.session_state.extraction_approved:
st.markdown(
"""
<div style="padding: 1.5rem; border-radius: 8px; text-align: center; ">
<strong style="font-size: 1.1rem;">Complete Previous Steps First</strong><br>
<span style="font-size: 0.95rem; ">1. Go to <strong>Define Use Case</strong> tab to describe your use case<br>
2. Then go to <strong>Technical Specification</strong> tab to set your SLO targets</span>
</div>
""",
unsafe_allow_html=True,
)
else:
st.markdown(
"""
<div style="padding: 1.5rem; border-radius: 8px; text-align: center; ">
<strong style="font-size: 1.1rem;">Complete Step 2 First</strong><br>
<span style="font-size: 0.95rem; ">Go to the <strong>Technical Specification</strong> tab to set your SLO targets and workload parameters.</span>
</div>
""",
unsafe_allow_html=True,
)
return
final_extraction = (
st.session_state.edited_extraction or st.session_state.extraction_result or {}
)
# Always regenerate recommendations to ensure fresh SLO filtering
st.session_state.recommendation_result = None
st.session_state.pop("ranked_response", None)
# Get all specification values from session state
use_case = final_extraction.get("use_case", "chatbot_conversational")
user_count = final_extraction.get("user_count", 1000)
ttft_target = st.session_state.get("custom_ttft") or st.session_state.get("input_ttft") or 500
itl_target = st.session_state.get("custom_itl") or st.session_state.get("input_itl") or 50
e2e_target = st.session_state.get("custom_e2e") or st.session_state.get("input_e2e") or 10000
qps_target = (
st.session_state.get("spec_expected_qps") or st.session_state.get("custom_qps") or 1
)
prompt_tokens = st.session_state.get("spec_prompt_tokens", 512)
output_tokens = st.session_state.get("spec_output_tokens", 256)
percentile = st.session_state.get("slo_percentile", "p95")
weights = {
"accuracy": st.session_state.get("weight_accuracy", 5),
"price": st.session_state.get("weight_cost", 4),
"latency": st.session_state.get("weight_latency", 2),
"complexity": 0,
}
preferred_gpu_types = final_extraction.get("preferred_gpu_types", [])
with st.spinner(f"Scoring {len(models_df)} models with MCDM..."):
recommendation = fetch_ranked_recommendations(
use_case=use_case,
user_count=user_count,
prompt_tokens=prompt_tokens,
output_tokens=output_tokens,
expected_qps=float(qps_target),
ttft_target_ms=int(ttft_target),
itl_target_ms=int(itl_target),
e2e_target_ms=int(e2e_target),
weights=weights,
include_near_miss=False,
percentile=percentile,
preferred_gpu_types=preferred_gpu_types,
)
if recommendation is None:
st.error("Unable to get recommendations. Please ensure backend is running.")
else:
st.session_state.recommendation_result = recommendation
if st.session_state.recommendation_result:
render_recommendation_result(
st.session_state.recommendation_result, used_priority, final_extraction
)
# =============================================================================
# MAIN APP
# =============================================================================
def main():
# Show dialogs if triggered (Streamlit only renders one at a time)
if st.session_state.show_winner_dialog and st.session_state.balanced_winner is not None:
show_winner_details_dialog()
elif st.session_state.show_category_dialog:
show_category_dialog()
elif st.session_state.show_full_table_dialog:
show_full_table_dialog()
# Load models
if st.session_state.models_df is None:
st.session_state.models_df = load_206_models()
models_df = st.session_state.models_df
priority = "balanced"
# Main Content - Compact hero
render_hero()
# Tab-based navigation (6 tabs)
tab1, tab2, tab3, tab4, tab5, tab6 = st.tabs(
[
"Define Use Case",
"Technical Specification",
"Recommendations",
"Deployment",
"Deployment Management",
"Configuration",
]
)
with tab1:
render_use_case_input_tab(priority, models_df)
with tab2:
render_technical_specs_tab()
with tab3:
render_results_tab(priority, models_df)
with tab4:
render_deployment_tab()
with tab5:
render_deployment_management_tab()
with tab6:
render_configuration_tab()
# Auto-switch to pending tab after rerun
pending_tab = st.session_state.pop("_pending_tab", None)
if pending_tab is not None:
components.html(
f"""<script>
var tabs = window.parent.document.querySelectorAll('[role="tab"]');
if (tabs.length > {pending_tab}) {{ tabs[{pending_tab}].click(); }}
</script>""",
height=0,
)
if __name__ == "__main__":
main()