-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
62 lines (55 loc) · 2.34 KB
/
app.py
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
from flask import Flask, request, jsonify
import os
from src.create_file import download_and_save_file
from src.create_metadata import generate_metadata_file
from src.utils.commands.execute import run_execute_commands
from src.utils.commands.initial import run_initial_commands
from src.utils.commands.push import run_push_commands
from src.utils.getID import get_id
from src.utils.commands.status import run_status_commands
from src.utils.commands.output import run_output_commands
from src.pinata_upload import pin_file_to_ipfs
app = Flask(__name__)
port = 3000
@app.route('/compute', methods=['GET'])
async def compute():
fileUrl = request.args.get('fileUrl')
# inputGpu = str(request.args.get('enable_gpu'))
# inputTpu = str(request.args.get('enable_tpu'))
# Convert "t" or "true" to "true", and "f" or "false" to "false"
# enableGpu = "true" if inputGpu.lower() in ['t', 'true'] else "false"
# enableTpu = "true" if inputTpu.lower() in ['t', 'true'] else "false"
result_file = await download_and_save_file(fileUrl)
print(result_file)
targetFolder = os.path.normpath(result_file['targetFolder'])
fileExtension = result_file['fileExtension']
filename = result_file['filename']
kernel_type = "script" if fileExtension == "py" else "notebook" if fileExtension == "ipynb" else "unknown"
print("RESULT",targetFolder, fileExtension, filename)
await run_execute_commands(targetFolder)
generatedfolder = generate_metadata_file(targetFolder, filename, fileExtension, kernel_type, "true", "false")
await run_push_commands(generatedfolder)
return {"generatedfolder": generatedfolder}
@app.route('/status', methods=['GET'])
async def get_status():
folderID = request.args.get('folderID')
ID = await get_id(folderID)
result = await run_status_commands(ID)
return {"result": result}
@app.route('/output', methods=['GET'])
async def get_output():
folderID = request.args.get('folderID')
ID = await get_id(folderID)
print("ID",ID)
await run_output_commands(ID, folderID)
# Read the file
title = ID.split('/')[1]
filePath = os.path.join(folderID, title + '.log')
ipfs_hash = pin_file_to_ipfs(filePath)
print(ipfs_hash)
return {"ipfshash": ipfs_hash}
# Start running the terminal commands
run_initial_commands()
# Start the Flask API
if __name__ == '__main__':
app.run(port=port)