grg's picture
Adding submit model instructions and route
215d189
raw
history blame
No virus
4.38 kB
import os
import pandas as pd
import utils
import base64
import shutil
import zipfile
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
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
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.df_to_table_html(main_df, additional_class="main-table")
full_table_html = utils.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):
return render_template('model_detail.html', model_name=model_name)
@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='grgur.kovac@inria.fr',
To='grgur.kovac@inria.fr',
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)