|
from flask import Flask, render_template, request, abort, session |
|
import csv |
|
import io |
|
import requests |
|
import os |
|
import tempfile |
|
|
|
|
|
def create_app(): |
|
app = Flask(__name__) |
|
app.config['SESSION_COOKIE_SECURE'] = True |
|
app.config['SESSION_COOKIE_SAMESITE'] = 'None' |
|
app.secret_key = os.environ.get('secret_key') |
|
return app |
|
|
|
app = create_app() |
|
|
|
|
|
|
|
def apply_transformation(row): |
|
transformed_row = [] |
|
for i, cell in enumerate(row): |
|
if 1 <= i <= 8: |
|
try: |
|
transformed_value = "{:.3f}".format(float(cell)) |
|
except ValueError: |
|
transformed_value = cell |
|
transformed_row.append(transformed_value) |
|
else: |
|
transformed_row.append(cell) |
|
return transformed_row |
|
|
|
|
|
@app.errorhandler(404) |
|
def page_not_found(error): |
|
return render_template('404.html'), 404 |
|
|
|
|
|
|
|
|
|
@app.route('/') |
|
def display_transformed_csv(): |
|
access_token = os.environ.get('GH_token') |
|
benchmark_file_texteval = 'Miner_Benchmark.csv' |
|
benchmark_file_agieval = 'miner_benchmark_AGIEval.csv' |
|
benchmark_file = '' |
|
active_tab = request.args.get('tab', default='TextEval') |
|
|
|
if active_tab == 'TextEval': |
|
benchmark_file = benchmark_file_texteval |
|
elif active_tab == 'AGIEval': |
|
benchmark_file = benchmark_file_agieval |
|
else: |
|
abort(404) |
|
|
|
|
|
print(session) |
|
resp_404_flag = 1 |
|
|
|
if resp_404_flag: |
|
GH_url = f"https://raw.githubusercontent.com/Kunj-2206/NIValidatorEndpoint/main/benchmark_files/{benchmark_file}?token=GHSAT0AAAAAACAPTSUR7YFNWFNDGFCFQRSCZIHVYMA" |
|
response = requests.get(GH_url, headers={'Authorization': f'Bearer {access_token}'}) |
|
if response.status_code is [200, 201, 202, 204]: |
|
resp_404_flag = 0 |
|
|
|
|
|
with tempfile.NamedTemporaryFile(delete=False) as temp_file: |
|
temp_file.write(response.content) |
|
csv_path = temp_file.name |
|
|
|
session[active_tab] = csv_path |
|
|
|
|
|
csv_path = session[active_tab] |
|
|
|
|
|
|
|
try: |
|
with open(csv_path, 'r') as csv_file: |
|
csv_reader = csv.reader(csv_file) |
|
header_row = next(csv_reader) |
|
|
|
data = [apply_transformation(row) for row in csv_reader] |
|
return render_template('table.html', data=data, header_row=header_row, active_tab=active_tab) |
|
except FileNotFoundError: |
|
return "CSV file not found." |
|
|
|
|