File size: 4,115 Bytes
982bef2 3b5f346 982bef2 3b5f346 5017c1f 902699b 982bef2 3b5f346 1e21ddc e22eb8e 902699b 5017c1f 1e21ddc 5017c1f 1e21ddc e22eb8e 3b5f346 e4b17e1 e22eb8e 5017c1f 1e21ddc e22eb8e 3b5f346 1e21ddc 902699b e22eb8e 5017c1f e22eb8e 902699b 3b5f346 e22eb8e 3b5f346 1e21ddc 3b5f346 6799003 e22eb8e b6646e9 1e21ddc e22eb8e 3b5f346 e22eb8e 902699b 3b5f346 982bef2 e22eb8e 982bef2 e22eb8e 982bef2 e22eb8e 982bef2 e22eb8e 982bef2 b6646e9 e22eb8e 982bef2 e22eb8e b6646e9 5017c1f |
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 |
import gradio as gr
import csv
import os
import sys
import traceback
# Audio file paths
audio_a_path = "audio/audio_a.wav"
audio_b_path = "audio/audio_b.wav"
# Use absolute paths that are definitely writable
csv_dir = os.path.join(os.getcwd(), "mos-eval")
csv_file_path = os.path.join(csv_dir, "mos_scores.csv")
def submit(intelligibility, naturalness, expressiveness):
debug_info = []
# debug_info.append(f"Current working directory: {os.getcwd()}")
# debug_info.append(f"Target directory: {csv_dir}")
# debug_info.append(f"Target file: {csv_file_path}")
try:
# Create directory if it doesn't exist
# debug_info.append("Trying to create directory...")
os.makedirs(csv_dir, exist_ok=True)
# debug_info.append(f"Directory created or already exists: {os.path.exists(csv_dir)}")
# List directory contents
if os.path.exists(csv_dir):
debug_info.append(f"Directory contents: {os.listdir(csv_dir)}")
# Check if file exists to add headers if new
file_exists = os.path.isfile(csv_file_path)
# debug_info.append(f"File exists: {file_exists}")
# Try writing to the file
# debug_info.append("Attempting to write to file...")
with open(csv_file_path, "a", newline="") as f:
writer = csv.writer(f)
# Add headers if file is new
if not file_exists:
writer.writerow(["Audio A", "Audio B", "Intelligibility", "Naturalness", "Expressiveness"])
writer.writerow([audio_a_path, audio_b_path, intelligibility, naturalness, expressiveness])
f.flush() # Force write to disk
os.fsync(f.fileno()) # Ensure it's written to the physical disk
# Verify the file was written
# debug_info.append(f"File exists after write: {os.path.isfile(csv_file_path)}")
if os.path.isfile(csv_file_path):
debug_info.append(f"File size: {os.path.getsize(csv_file_path)} bytes")
# Read and display CSV content
csv_content = ""
if os.path.isfile(csv_file_path):
with open(csv_file_path, "r") as f:
csv_content = f.read()
# debug_info.append(f"\nCSV Content:\n{csv_content}")
return "Thank you for your feedback! Data saved successfully.\n\nDebug info:\n" + "\n".join(debug_info)
except Exception as e:
exc_type, exc_value, exc_traceback = sys.exc_info()
tb_str = traceback.format_exception(exc_type, exc_value, exc_traceback)
debug_info.append(f"Error: {e}")
debug_info.append("Traceback:")
debug_info.append("".join(tb_str))
return "Error occurred. Debug info:\n" + "\n".join(debug_info)
# Add this function for downloading the CSV
def get_csv_data():
if os.path.exists(csv_file_path):
# Return the path to the file for download
return csv_file_path
return None
with gr.Blocks() as demo:
gr.Markdown("# 🎧 MOS Evaluation")
gr.Markdown("Compare Audio A and Audio B, then rate them below.")
with gr.Row():
with gr.Column():
gr.Audio(audio_a_path, label="Audio A", type="filepath")
with gr.Column():
gr.Audio(audio_b_path, label="Audio B", type="filepath")
intelligibility = gr.Slider(1, 5, value=3, step=1, label="Intelligibility")
naturalness = gr.Slider(1, 5, value=3, step=1, label="Naturalness")
expressiveness = gr.Slider(1, 5, value=3, step=1, label="Expressiveness")
submit_btn = gr.Button("Submit")
output = gr.Textbox(label="Status", lines=15) # Increased size for debug and CSV output
submit_btn.click(fn=submit, inputs=[intelligibility, naturalness, expressiveness], outputs=output)
# Add download section - ADD THIS NEW SECTION
gr.Markdown("## Download Results")
download_btn = gr.Button("Download Results CSV")
csv_output = gr.File(label="CSV File")
download_btn.click(fn=get_csv_data, inputs=[], outputs=csv_output)
demo.launch() |