File size: 3,436 Bytes
ee5d33c 05501d1 ee5d33c 05501d1 ee5d33c 73c45d0 ee5d33c 05501d1 ee5d33c |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
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."
|