mos-eval / app.py
mails10's picture
Update app.py
6799003 verified
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()