Bryceeee commited on
Commit
a9fc8a6
Β·
verified Β·
1 Parent(s): cdf3c8a

Upload gradio_interface.py

Browse files
Files changed (1) hide show
  1. src/gradio_interface.py +22 -72
src/gradio_interface.py CHANGED
@@ -39,66 +39,44 @@ class GradioInterfaceBuilder:
39
 
40
  def create_interface(self):
41
  """Create the main Gradio interface"""
42
- print(" [1/6] Creating Gradio Blocks container...")
43
  with gr.Blocks(title="Mercedes E-class ADAS Manual Interface") as demo:
44
- print(" [2/6] Adding header markdown...")
45
  gr.Markdown("# πŸš— Mercedes E-class ADAS Manual Interface")
46
  gr.Markdown("Ask questions, explore knowledge maps, and test your understanding!")
47
 
48
  # Create tabs with proper order: Setup -> Ask Questions -> Knowledge Map -> Test -> Personalized Learning
49
  # Ask Questions is set as default selected tab
50
- print(" [3/6] Creating tabs container...")
 
 
 
 
 
 
 
 
51
  with gr.Tabs(selected=1 if self.user_profiling else 0) as tabs:
52
  # Tab 1: Setup (Cold Start/Onboarding) - only shown if user_profiling is available
53
  if self.user_profiling:
54
- print(" [4/6] Creating Setup tab...")
55
- try:
56
- with gr.TabItem("Setup"):
57
- self._create_onboarding_tab()
58
- print(" [4/6] βœ… Setup tab created")
59
- except Exception as e:
60
- print(f" [4/6] ⚠️ Error creating Setup tab: {e}")
61
 
62
  # Tab 2: Ask Questions (Default selected tab)
63
- print(" [5/6] Creating Ask Questions tab...")
64
- try:
65
- with gr.TabItem("Ask Questions"):
66
- self._create_qa_tab()
67
- print(" [5/6] βœ… Ask Questions tab created")
68
- except Exception as e:
69
- print(f" [5/6] ⚠️ Error creating Ask Questions tab: {e}")
70
- import traceback
71
- traceback.print_exc()
72
 
73
  # Tab 3: Knowledge Map
74
- print(" [6/7] Creating Knowledge Map tab...")
75
- try:
76
- with gr.TabItem("Knowledge Map"):
77
- self._create_knowledge_map_tab()
78
- print(" [6/7] βœ… Knowledge Map tab created")
79
- except Exception as e:
80
- print(f" [6/7] ⚠️ Error creating Knowledge Map tab: {e}")
81
 
82
  # Tab 4: Test Your Knowledge
83
- print(" [7/8] Creating Test Your Knowledge tab...")
84
- try:
85
- with gr.TabItem("Test Your Knowledge"):
86
- self._create_test_tab()
87
- print(" [7/8] βœ… Test Your Knowledge tab created")
88
- except Exception as e:
89
- print(f" [7/8] ⚠️ Error creating Test Your Knowledge tab: {e}")
90
 
91
  # Tab 5: Personalized Learning Path (if available)
92
  if self.adaptive_engine:
93
- print(" [8/8] Creating Personalized Learning tab...")
94
- try:
95
- with gr.TabItem("Personalized Learning"):
96
- self._create_learning_path_tab()
97
- print(" [8/8] βœ… Personalized Learning tab created")
98
- except Exception as e:
99
- print(f" [8/8] ⚠️ Error creating Personalized Learning tab: {e}")
100
 
101
- print(" βœ… All tabs created, returning demo object")
102
  return demo
103
 
104
  def _create_qa_tab(self):
@@ -426,14 +404,7 @@ class GradioInterfaceBuilder:
426
  gr.Markdown("## πŸ“ Test Your Understanding of Mercedes E-class ADAS")
427
  gr.Markdown("Select a topic to test your knowledge with multiple-choice questions based on Bloom's taxonomy levels.")
428
 
429
- # Use default files instead of calling API during interface creation
430
- # This avoids blocking during initialization
431
- default_files = [
432
- "Function of Active Distance Assist DISTRONIC.pdf",
433
- "Function of Active Lane Change Assist.pdf",
434
- "Function of Active Steering Assist.pdf",
435
- "Function of Active Stop-and-Go Assist.pdf"
436
- ]
437
 
438
  with gr.Row():
439
  test_questions = gr.State(None)
@@ -443,34 +414,13 @@ class GradioInterfaceBuilder:
443
 
444
  topic_dropdown = gr.Dropdown(
445
  label="Select a Topic",
446
- choices=default_files,
447
- value=default_files[0] if default_files else None,
448
  interactive=True
449
  )
450
 
451
- # Add a button to refresh files from vector store (lazy loading)
452
- refresh_files_btn = gr.Button("πŸ”„ Refresh Topic List", variant="secondary", size="sm")
453
-
454
  start_test_btn = gr.Button("Start Test", variant="primary")
455
 
456
- # Function to refresh files from vector store (called when button is clicked)
457
- def refresh_topic_list():
458
- try:
459
- topic_files = self.rag_engine.get_files_from_vector_store()
460
- if topic_files:
461
- return gr.Dropdown.update(choices=topic_files, value=topic_files[0] if topic_files else None)
462
- else:
463
- return gr.Dropdown.update(choices=default_files, value=default_files[0] if default_files else None)
464
- except Exception as e:
465
- print(f"⚠️ Error refreshing files: {e}")
466
- return gr.Dropdown.update(choices=default_files, value=default_files[0] if default_files else None)
467
-
468
- refresh_files_btn.click(
469
- fn=refresh_topic_list,
470
- inputs=None,
471
- outputs=topic_dropdown
472
- )
473
-
474
  # Progress indicator
475
  with gr.Column(visible=False) as progress_container:
476
  progress_html = gr.HTML()
 
39
 
40
  def create_interface(self):
41
  """Create the main Gradio interface"""
 
42
  with gr.Blocks(title="Mercedes E-class ADAS Manual Interface") as demo:
 
43
  gr.Markdown("# πŸš— Mercedes E-class ADAS Manual Interface")
44
  gr.Markdown("Ask questions, explore knowledge maps, and test your understanding!")
45
 
46
  # Create tabs with proper order: Setup -> Ask Questions -> Knowledge Map -> Test -> Personalized Learning
47
  # Ask Questions is set as default selected tab
48
+ # Note: In Gradio 4.0+, use gr.Tab instead of gr.TabItem
49
+ # For compatibility, check if TabItem exists (older versions) or use Tab (newer versions)
50
+ try:
51
+ # Try new API first (Gradio 4.0+)
52
+ TabComponent = gr.Tab
53
+ except AttributeError:
54
+ # Fallback to old API (Gradio 3.x)
55
+ TabComponent = gr.TabItem
56
+
57
  with gr.Tabs(selected=1 if self.user_profiling else 0) as tabs:
58
  # Tab 1: Setup (Cold Start/Onboarding) - only shown if user_profiling is available
59
  if self.user_profiling:
60
+ with TabComponent("Setup"):
61
+ self._create_onboarding_tab()
 
 
 
 
 
62
 
63
  # Tab 2: Ask Questions (Default selected tab)
64
+ with TabComponent("Ask Questions"):
65
+ self._create_qa_tab()
 
 
 
 
 
 
 
66
 
67
  # Tab 3: Knowledge Map
68
+ with TabComponent("Knowledge Map"):
69
+ self._create_knowledge_map_tab()
 
 
 
 
 
70
 
71
  # Tab 4: Test Your Knowledge
72
+ with TabComponent("Test Your Knowledge"):
73
+ self._create_test_tab()
 
 
 
 
 
74
 
75
  # Tab 5: Personalized Learning Path (if available)
76
  if self.adaptive_engine:
77
+ with TabComponent("Personalized Learning"):
78
+ self._create_learning_path_tab()
 
 
 
 
 
79
 
 
80
  return demo
81
 
82
  def _create_qa_tab(self):
 
404
  gr.Markdown("## πŸ“ Test Your Understanding of Mercedes E-class ADAS")
405
  gr.Markdown("Select a topic to test your knowledge with multiple-choice questions based on Bloom's taxonomy levels.")
406
 
407
+ topic_files = self.rag_engine.get_files_from_vector_store()
 
 
 
 
 
 
 
408
 
409
  with gr.Row():
410
  test_questions = gr.State(None)
 
414
 
415
  topic_dropdown = gr.Dropdown(
416
  label="Select a Topic",
417
+ choices=topic_files,
418
+ value=topic_files[0] if topic_files else None,
419
  interactive=True
420
  )
421
 
 
 
 
422
  start_test_btn = gr.Button("Start Test", variant="primary")
423
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
424
  # Progress indicator
425
  with gr.Column(visible=False) as progress_container:
426
  progress_html = gr.HTML()