Defalt-404's picture
add gunicorn bind
73c45d0
raw
history blame
No virus
3.44 kB
from flask import Flask, render_template, request, abort, session
import csv
import io
import requests
import os
import tempfile
def get_secrets(file):
file_path = f'/run/secrets/{file}'
print('in get_secrets function')
try:
with open(file_path, 'r') as file:
secret_example = file.read()
print('The content of SECRET_EXAMPLE is:', secret_example)
return secret_example
except FileNotFoundError:
print('File not found:', file_path)
except PermissionError:
print('Permission denied. Make sure you have the necessary permissions to read the file.')
def create_app():
app = Flask(__name__)
app.config['SESSION_COOKIE_SECURE'] = True # Requires HTTPS
app.config['SESSION_COOKIE_SAMESITE'] = 'None'
app.secret_key = get_secrets('secret_key')
return app
app = create_app()
# Function to apply transformation to specific columns
def apply_transformation(row):
transformed_row = []
for i, cell in enumerate(row):
if 1 <= i <= 8: # Columns 1 to 8 (0-based index) transformation
try:
transformed_value = "{:.3f}".format(float(cell))
except ValueError:
transformed_value = cell # Keep non-numeric values unchanged
transformed_row.append(transformed_value)
else:
transformed_row.append(cell) # Keep other columns unchanged
return transformed_row
@app.errorhandler(404)
def page_not_found(error):
return render_template('404.html'), 404
# Route to display transformed CSV as a table
@app.route('/')
def display_transformed_csv():
app.secret_key = get_secrets('secret_key')
access_token = get_secrets('GH_token')
app.secret_key = get_secrets('secret_key')
print(access_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
# Create a temporary file to store the CSV content
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(response.content)
csv_path = temp_file.name
session[active_tab] = csv_path
# Retrieve the CSV content from the session
csv_path = session[active_tab]
#print(csv_content)
try:
with open(csv_path, 'r') as csv_file:
csv_reader = csv.reader(csv_file)
header_row = next(csv_reader)
#print(header_row)
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."