grg's picture
Adding additional descriptions (improving the text) and adding link to the huggingface dataset.
d7c0630
raw
history blame
5.39 kB
import os
import pandas as pd
import utils
import base64
from flask import Flask, render_template, request, redirect, url_for
from postmarker.core import PostmarkClient
from werkzeug.utils import secure_filename
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads' # Directory where files will be stored
app.config['ALLOWED_EXTENSIONS'] = {'zip'}
def allowed_file(filename):
return '.' in filename and \
filename.rsplit('.', 1)[1].lower() in app.config['ALLOWED_EXTENSIONS']
@app.route('/')
def index():
# Load the CSV file into a DataFrame
df = pd.read_csv('static/leaderboard.csv')
df = df.round(3)
df.insert(0, '#', '')
up_arrow, down_arrow = "↑", "↓"
# up_arrow, down_arrow = "⬆️", "⬇️"
# up_arrow, down_arrow = "▲", "▼"
df = df.rename(columns={
"Ordinal (Win rate)": f"Ordinal - Win rate ({up_arrow})",
"Cardinal (Score)": f"Cardinal - Score ({up_arrow})",
"RO Stability": f"RO Stability ({up_arrow})",
"Stress": f"Stress ({down_arrow})",
"Rank Distance": f"Rank Distance ({down_arrow})",
"Separability": f"Separability ({up_arrow})",
"CFI": f"CFI ({up_arrow})",
"SRMR": f"SRMR ({down_arrow})",
"RMSEA": f"RMSEA ({down_arrow})",
"Cronbach alpha": f"Cronbach alpha ({up_arrow})"
})
# Generate full table HTML with clickable model names
################
main_table_metrics = [
"#",
"Model",
f"Ordinal - Win rate ({up_arrow})",
f"Cardinal - Score ({up_arrow})",
f"RO Stability ({up_arrow})"
]
main_df = df[main_table_metrics].copy()
main_table_html = utils.metrics_df_to_table_html(main_df, additional_class="main-table")
full_table_html = utils.metrics_df_to_table_html(df, additional_class="full-table")
# Render the template with the table HTML
return render_template('index.html', main_table_html=main_table_html, full_table_html=full_table_html)
@app.route('/model/<model_name>')
def model_detail(model_name):
df = pd.read_csv(f'static/models_data/{model_name}/cfa_metrics.csv')
df = df.round(3)
df.insert(0, '#', '')
up_arrow, down_arrow = "↑", "↓"
metrics_to_show = [df.columns[0], df.columns[1], "CFI", "SRMR", "RMSEA"]
df = df[metrics_to_show].copy()
df = df.rename(columns={
"CFI": f"CFI ({up_arrow})",
"SRMR": f"SRMR ({down_arrow})",
"RMSEA": f"RMSEA ({down_arrow})",
})
classes = 'table table-striped table-bordered'
cfa_table_html = df.to_html(classes=classes, escape=False, index=False)
# Load model specific description
model_detail_file = f'static/models_data/{model_name}/model_detail.html'
if os.path.exists(model_detail_file):
# If the file exists, open and read the HTML file as a string
with open(model_detail_file, 'r', encoding='utf-8') as file:
model_detail = file.read()
else:
model_detail = "<p>No additional detail is provided for this model.</p>"
return render_template(
'model_detail.html',
model_name=model_name,
model_detail=model_detail,
cfa_table_html=cfa_table_html
)
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/new_model')
def new_model():
return render_template('new_model.html')
@app.route('/model_submitted')
def model_submitted():
return render_template('model_submitted.html')
@app.route('/failed_submission')
def failed_submission():
return render_template('failed_submission.html')
@app.route('/submit_model', methods=['POST'])
def submit_model():
model_name = request.form['model_name']
pull_request_link = request.form['pull_request_link']
email = request.form['email']
description = request.form['description']
# Handle ZIP file upload
if 'model_files' not in request.files:
return redirect(url_for('failed_submission'))
file = request.files['model_files']
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
file_path = os.path.join(app.config['UPLOAD_FOLDER'], filename)
file.save(file_path)
# Read the file content and encode it in base64
with open(file_path, 'rb') as f:
file_content = base64.b64encode(f.read()).decode('ascii')
# Set up Postmark email client
postmark = PostmarkClient(server_token=os.getenv('POSTMARK_SERVER_API'))
# Send the email with the attachment
postmark.emails.send(
From='[email protected]',
To='[email protected]',
Subject=f'Stick to Your Role! Model Submission: {model_name}',
HtmlBody=f"""
<p><strong>Model Name:</strong> {model_name}</p>
<p><strong>Pull Request Link:</strong> {pull_request_link}</p>
<p><strong>Email:</strong> {email}</p>
<p><strong>Description:</strong> {description}</p>
""",
Attachments=[{
'Name': filename,
'Content': file_content,
'ContentType': 'application/zip'
}]
)
else:
return redirect(url_for('failed_submission'))
return redirect(url_for('model_submitted'))
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860, debug=True)