mails10 commited on
Commit
e22eb8e
·
verified ·
1 Parent(s): b6646e9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -14
app.py CHANGED
@@ -17,48 +17,48 @@ def submit(intelligibility, naturalness, expressiveness):
17
  debug_info.append(f"Current working directory: {os.getcwd()}")
18
  debug_info.append(f"Target directory: {csv_dir}")
19
  debug_info.append(f"Target file: {csv_file_path}")
20
-
21
  try:
22
  # Create directory if it doesn't exist
23
  debug_info.append("Trying to create directory...")
24
  os.makedirs(csv_dir, exist_ok=True)
25
  debug_info.append(f"Directory created or already exists: {os.path.exists(csv_dir)}")
26
-
27
  # List directory contents
28
  if os.path.exists(csv_dir):
29
  debug_info.append(f"Directory contents: {os.listdir(csv_dir)}")
30
-
31
  # Check if file exists to add headers if new
32
  file_exists = os.path.isfile(csv_file_path)
33
  debug_info.append(f"File exists: {file_exists}")
34
-
35
  # Try writing to the file
36
  debug_info.append("Attempting to write to file...")
37
  with open(csv_file_path, "a", newline="") as f:
38
  writer = csv.writer(f)
39
-
40
  # Add headers if file is new
41
  if not file_exists:
42
  writer.writerow(["Audio A", "Audio B", "Intelligibility", "Naturalness", "Expressiveness"])
43
-
44
  writer.writerow([audio_a_path, audio_b_path, intelligibility, naturalness, expressiveness])
45
  f.flush() # Force write to disk
46
  os.fsync(f.fileno()) # Ensure it's written to the physical disk
47
-
48
  # Verify the file was written
49
  debug_info.append(f"File exists after write: {os.path.isfile(csv_file_path)}")
50
  if os.path.isfile(csv_file_path):
51
  debug_info.append(f"File size: {os.path.getsize(csv_file_path)} bytes")
52
-
53
  # Read and display CSV content
54
  csv_content = ""
55
  if os.path.isfile(csv_file_path):
56
  with open(csv_file_path, "r") as f:
57
  csv_content = f.read()
58
  debug_info.append(f"\nCSV Content:\n{csv_content}")
59
-
60
  return "Thank you for your feedback! Data saved successfully.\n\nDebug info:\n" + "\n".join(debug_info)
61
-
62
  except Exception as e:
63
  exc_type, exc_value, exc_traceback = sys.exc_info()
64
  tb_str = traceback.format_exception(exc_type, exc_value, exc_traceback)
@@ -67,23 +67,36 @@ def submit(intelligibility, naturalness, expressiveness):
67
  debug_info.append("".join(tb_str))
68
  return "Error occurred. Debug info:\n" + "\n".join(debug_info)
69
 
 
 
 
 
 
 
 
70
  with gr.Blocks() as demo:
71
  gr.Markdown("# 🎧 MOS Evaluation")
72
  gr.Markdown("Compare Audio A and Audio B, then rate them below.")
73
-
74
  with gr.Row():
75
  with gr.Column():
76
  gr.Audio(audio_a_path, label="Audio A", type="filepath")
77
  with gr.Column():
78
  gr.Audio(audio_b_path, label="Audio B", type="filepath")
79
-
80
  intelligibility = gr.Slider(1, 5, value=3, step=1, label="Intelligibility")
81
  naturalness = gr.Slider(1, 5, value=3, step=1, label="Naturalness")
82
  expressiveness = gr.Slider(1, 5, value=3, step=1, label="Expressiveness")
83
-
84
  submit_btn = gr.Button("Submit")
85
  output = gr.Textbox(label="Status", lines=15) # Increased size for debug and CSV output
86
-
87
  submit_btn.click(fn=submit, inputs=[intelligibility, naturalness, expressiveness], outputs=output)
 
 
 
 
 
 
88
 
89
  demo.launch()
 
17
  debug_info.append(f"Current working directory: {os.getcwd()}")
18
  debug_info.append(f"Target directory: {csv_dir}")
19
  debug_info.append(f"Target file: {csv_file_path}")
20
+
21
  try:
22
  # Create directory if it doesn't exist
23
  debug_info.append("Trying to create directory...")
24
  os.makedirs(csv_dir, exist_ok=True)
25
  debug_info.append(f"Directory created or already exists: {os.path.exists(csv_dir)}")
26
+
27
  # List directory contents
28
  if os.path.exists(csv_dir):
29
  debug_info.append(f"Directory contents: {os.listdir(csv_dir)}")
30
+
31
  # Check if file exists to add headers if new
32
  file_exists = os.path.isfile(csv_file_path)
33
  debug_info.append(f"File exists: {file_exists}")
34
+
35
  # Try writing to the file
36
  debug_info.append("Attempting to write to file...")
37
  with open(csv_file_path, "a", newline="") as f:
38
  writer = csv.writer(f)
39
+
40
  # Add headers if file is new
41
  if not file_exists:
42
  writer.writerow(["Audio A", "Audio B", "Intelligibility", "Naturalness", "Expressiveness"])
43
+
44
  writer.writerow([audio_a_path, audio_b_path, intelligibility, naturalness, expressiveness])
45
  f.flush() # Force write to disk
46
  os.fsync(f.fileno()) # Ensure it's written to the physical disk
47
+
48
  # Verify the file was written
49
  debug_info.append(f"File exists after write: {os.path.isfile(csv_file_path)}")
50
  if os.path.isfile(csv_file_path):
51
  debug_info.append(f"File size: {os.path.getsize(csv_file_path)} bytes")
52
+
53
  # Read and display CSV content
54
  csv_content = ""
55
  if os.path.isfile(csv_file_path):
56
  with open(csv_file_path, "r") as f:
57
  csv_content = f.read()
58
  debug_info.append(f"\nCSV Content:\n{csv_content}")
59
+
60
  return "Thank you for your feedback! Data saved successfully.\n\nDebug info:\n" + "\n".join(debug_info)
61
+
62
  except Exception as e:
63
  exc_type, exc_value, exc_traceback = sys.exc_info()
64
  tb_str = traceback.format_exception(exc_type, exc_value, exc_traceback)
 
67
  debug_info.append("".join(tb_str))
68
  return "Error occurred. Debug info:\n" + "\n".join(debug_info)
69
 
70
+ # Add this function for downloading the CSV
71
+ def get_csv_data():
72
+ if os.path.exists(csv_file_path):
73
+ # Return the path to the file for download
74
+ return csv_file_path
75
+ return None
76
+
77
  with gr.Blocks() as demo:
78
  gr.Markdown("# 🎧 MOS Evaluation")
79
  gr.Markdown("Compare Audio A and Audio B, then rate them below.")
80
+
81
  with gr.Row():
82
  with gr.Column():
83
  gr.Audio(audio_a_path, label="Audio A", type="filepath")
84
  with gr.Column():
85
  gr.Audio(audio_b_path, label="Audio B", type="filepath")
86
+
87
  intelligibility = gr.Slider(1, 5, value=3, step=1, label="Intelligibility")
88
  naturalness = gr.Slider(1, 5, value=3, step=1, label="Naturalness")
89
  expressiveness = gr.Slider(1, 5, value=3, step=1, label="Expressiveness")
90
+
91
  submit_btn = gr.Button("Submit")
92
  output = gr.Textbox(label="Status", lines=15) # Increased size for debug and CSV output
93
+
94
  submit_btn.click(fn=submit, inputs=[intelligibility, naturalness, expressiveness], outputs=output)
95
+
96
+ # Add download section - ADD THIS NEW SECTION
97
+ gr.Markdown("## Download Results")
98
+ download_btn = gr.Button("Download Results CSV")
99
+ csv_output = gr.File(label="CSV File")
100
+ download_btn.click(fn=get_csv_data, inputs=[], outputs=csv_output)
101
 
102
  demo.launch()