wi-lab commited on
Commit
ec91fe9
·
verified ·
1 Parent(s): 6cafefe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -7
app.py CHANGED
@@ -15,7 +15,7 @@ from sklearn.metrics import f1_score
15
  import seaborn as sns
16
 
17
  #################### BEAM PREDICTION #########################}
18
- def beam_prediction_task(data_percentage, task_complexity, user_mode="light"):
19
  # Folder naming convention based on input_type, data_percentage, and task_complexity
20
  raw_folder = f"images/raw_{data_percentage/100:.1f}_{task_complexity}"
21
  embeddings_folder = f"images/embedding_{data_percentage/100:.1f}_{task_complexity}"
@@ -24,7 +24,7 @@ def beam_prediction_task(data_percentage, task_complexity, user_mode="light"):
24
  raw_cm = compute_average_confusion_matrix(raw_folder)
25
  if raw_cm is not None:
26
  raw_cm_path = os.path.join(raw_folder, "confusion_matrix_raw.png")
27
- plot_confusion_matrix_beamPred(raw_cm, classes=np.arange(raw_cm.shape[0]), title=f"Raw Confusion Matrix\n({data_percentage}% data, {task_complexity} beams)", save_path=raw_cm_path, user_mode=user_mode)
28
  raw_img = Image.open(raw_cm_path)
29
  else:
30
  raw_img = None
@@ -33,7 +33,7 @@ def beam_prediction_task(data_percentage, task_complexity, user_mode="light"):
33
  embeddings_cm = compute_average_confusion_matrix(embeddings_folder)
34
  if embeddings_cm is not None:
35
  embeddings_cm_path = os.path.join(embeddings_folder, "confusion_matrix_embeddings.png")
36
- plot_confusion_matrix_beamPred(embeddings_cm, classes=np.arange(embeddings_cm.shape[0]), title=f"Embeddings Confusion Matrix\n({data_percentage}% data, {task_complexity} beams)", save_path=embeddings_cm_path, user_mode=user_mode)
37
  embeddings_img = Image.open(embeddings_cm_path)
38
  else:
39
  embeddings_img = None
@@ -59,12 +59,12 @@ def compute_f1_score(cm):
59
  f1 = np.nan_to_num(f1) # Replace NaN with 0
60
  return np.mean(f1) # Return the mean F1-score across all classes
61
 
62
- def plot_confusion_matrix_beamPred(cm, classes, title, save_path, user_mode="dark"):
63
  # Compute the average F1-score
64
  avg_f1 = compute_f1_score(cm)
65
 
66
  # Choose the color scheme based on the user's mode
67
- if user_mode == 'dark':
68
  plt.style.use('dark_background') # Use dark mode styling
69
  text_color = 'white'
70
  cmap = 'cividis' # Dark-mode-friendly colormap
@@ -474,6 +474,21 @@ def process_hdf5_file(uploaded_file, percentage):
474
  sys.stdout = sys.__stdout__ # Reset print statements
475
 
476
  ######################## Define the Gradio interface ###############################
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
477
  with gr.Blocks(css="""
478
  .slider-container {
479
  display: inline-block;
@@ -534,6 +549,10 @@ with gr.Blocks(css="""
534
  # Tab for Beam Prediction Task
535
  with gr.Tab("Beam Prediction Task"):
536
  #gr.Markdown("### Beam Prediction Task")
 
 
 
 
537
 
538
  # Explanation section with creative spacing and minimal design
539
  gr.Markdown("""
@@ -563,8 +582,9 @@ with gr.Blocks(css="""
563
  embeddings_img_bp = gr.Image(label="Embeddings", type="pil", width=300, height=500)
564
 
565
  # Update the confusion matrices whenever sliders change
566
- data_percentage_slider.change(fn=beam_prediction_task, inputs=[data_percentage_slider, task_complexity_dropdown], outputs=[raw_img_bp, embeddings_img_bp])
567
- task_complexity_dropdown.change(fn=beam_prediction_task, inputs=[data_percentage_slider, task_complexity_dropdown], outputs=[raw_img_bp, embeddings_img_bp])
 
568
 
569
  # Add a conclusion section at the bottom
570
  gr.Markdown("""
 
15
  import seaborn as sns
16
 
17
  #################### BEAM PREDICTION #########################}
18
+ def beam_prediction_task(data_percentage, task_complexity, theme):
19
  # Folder naming convention based on input_type, data_percentage, and task_complexity
20
  raw_folder = f"images/raw_{data_percentage/100:.1f}_{task_complexity}"
21
  embeddings_folder = f"images/embedding_{data_percentage/100:.1f}_{task_complexity}"
 
24
  raw_cm = compute_average_confusion_matrix(raw_folder)
25
  if raw_cm is not None:
26
  raw_cm_path = os.path.join(raw_folder, "confusion_matrix_raw.png")
27
+ plot_confusion_matrix_beamPred(raw_cm, classes=np.arange(raw_cm.shape[0]), title=f"Raw Confusion Matrix\n({data_percentage}% data, {task_complexity} beams)", save_path=raw_cm_path, theme)
28
  raw_img = Image.open(raw_cm_path)
29
  else:
30
  raw_img = None
 
33
  embeddings_cm = compute_average_confusion_matrix(embeddings_folder)
34
  if embeddings_cm is not None:
35
  embeddings_cm_path = os.path.join(embeddings_folder, "confusion_matrix_embeddings.png")
36
+ plot_confusion_matrix_beamPred(embeddings_cm, classes=np.arange(embeddings_cm.shape[0]), title=f"Embeddings Confusion Matrix\n({data_percentage}% data, {task_complexity} beams)", save_path=embeddings_cm_path, theme)
37
  embeddings_img = Image.open(embeddings_cm_path)
38
  else:
39
  embeddings_img = None
 
59
  f1 = np.nan_to_num(f1) # Replace NaN with 0
60
  return np.mean(f1) # Return the mean F1-score across all classes
61
 
62
+ def plot_confusion_matrix_beamPred(cm, classes, title, save_path, theme):
63
  # Compute the average F1-score
64
  avg_f1 = compute_f1_score(cm)
65
 
66
  # Choose the color scheme based on the user's mode
67
+ if theme == 'dark':
68
  plt.style.use('dark_background') # Use dark mode styling
69
  text_color = 'white'
70
  cmap = 'cividis' # Dark-mode-friendly colormap
 
474
  sys.stdout = sys.__stdout__ # Reset print statements
475
 
476
  ######################## Define the Gradio interface ###############################
477
+ # JavaScript to detect dark/light mode and pass it to Gradio as 'light' or 'dark'
478
+ detect_theme_js = """
479
+ <script>
480
+ function detectTheme() {
481
+ if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
482
+ return 'dark';
483
+ } else {
484
+ return 'light';
485
+ }
486
+ }
487
+ document.getElementById('theme_output').value = detectTheme();
488
+ document.getElementById('theme_output').dispatchEvent(new Event('change'));
489
+ </script>
490
+ """
491
+
492
  with gr.Blocks(css="""
493
  .slider-container {
494
  display: inline-block;
 
549
  # Tab for Beam Prediction Task
550
  with gr.Tab("Beam Prediction Task"):
551
  #gr.Markdown("### Beam Prediction Task")
552
+ gr.HTML(detect_theme_js)
553
+
554
+ # Hidden textbox to store the detected theme
555
+ theme_output = gr.Textbox(label="Detected Theme", elem_id="theme_output", visible=False)
556
 
557
  # Explanation section with creative spacing and minimal design
558
  gr.Markdown("""
 
582
  embeddings_img_bp = gr.Image(label="Embeddings", type="pil", width=300, height=500)
583
 
584
  # Update the confusion matrices whenever sliders change
585
+ data_percentage_slider.change(fn=beam_prediction_task, inputs=[data_percentage_slider, task_complexity_dropdown, theme_output], outputs=[raw_img_bp, embeddings_img_bp])
586
+ task_complexity_dropdown.change(fn=beam_prediction_task, inputs=[data_percentage_slider, task_complexity_dropdown, theme_output], outputs=[raw_img_bp, embeddings_img_bp])
587
+ theme_output.change(fn=beam_prediction_task, inputs=[data_percentage_slider, task_complexity_dropdown, theme_output], outputs=[raw_img_bp, embeddings_img_bp])
588
 
589
  # Add a conclusion section at the bottom
590
  gr.Markdown("""