Roland Ding commited on
Commit ·
9c6b97d
1
Parent(s): 27080a2
0.0.1.2 included common app files and utilities.
Browse files- __pycache__/application.cpython-310.pyc +0 -0
- __pycache__/cloud_db.cpython-310.pyc +0 -0
- __pycache__/invoices.cpython-310.pyc +0 -0
- __pycache__/user.cpython-310.pyc +0 -0
- __pycache__/utility.cpython-310.pyc +0 -0
- application.py +76 -0
- cloud_db.py +69 -0
- invoices.py +8 -0
- user.py +10 -0
- utility.py +163 -0
__pycache__/application.cpython-310.pyc
ADDED
|
Binary file (1.23 kB). View file
|
|
|
__pycache__/cloud_db.cpython-310.pyc
ADDED
|
Binary file (1.42 kB). View file
|
|
|
__pycache__/invoices.cpython-310.pyc
ADDED
|
Binary file (526 Bytes). View file
|
|
|
__pycache__/user.cpython-310.pyc
ADDED
|
Binary file (603 Bytes). View file
|
|
|
__pycache__/utility.cpython-310.pyc
ADDED
|
Binary file (3.26 kB). View file
|
|
|
application.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
|
| 3 |
+
'''
|
| 4 |
+
shared environment variables
|
| 5 |
+
'''
|
| 6 |
+
default_domain = "cervical-cage"
|
| 7 |
+
|
| 8 |
+
aws_access_key_id = os.environ.get('AMRA_AWS_ACCESS_KEY_ID')
|
| 9 |
+
aws_secret_access_key = os.environ.get('AMRA_AWS_SECRET_ACCESS_KEY')
|
| 10 |
+
openai_api_key = os.environ.get('AMRA_OPENAI_API_KEY')
|
| 11 |
+
|
| 12 |
+
'''
|
| 13 |
+
ui device environment variables
|
| 14 |
+
'''
|
| 15 |
+
eu_device_classification = ["phone", "laptop", "tablet", "desktop"]
|
| 16 |
+
performance_outcome_list = [ "VAS Score", "Incidence of Fusion", "Incidence of Pseudoarthrosis", "Incidence of Reoperation"]
|
| 17 |
+
safety_outcome_list = ["Incidence of Revision","Incidence of Nonunion"]
|
| 18 |
+
|
| 19 |
+
device_options={
|
| 20 |
+
"secondary extraction":False,
|
| 21 |
+
"secondary extraction count":0
|
| 22 |
+
}
|
| 23 |
+
|
| 24 |
+
'''
|
| 25 |
+
ui equivalent environment variables
|
| 26 |
+
'''
|
| 27 |
+
ec_options={
|
| 28 |
+
"Equivalent Comparator":False,
|
| 29 |
+
"Equivalent Comparator require SD":False,
|
| 30 |
+
"Equivalent Comparator count":0
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
'''
|
| 34 |
+
dynamodb tables structure
|
| 35 |
+
'''
|
| 36 |
+
data_structure = {
|
| 37 |
+
"terms":{
|
| 38 |
+
"key":{
|
| 39 |
+
|
| 40 |
+
},
|
| 41 |
+
"fields":[
|
| 42 |
+
"assessment_step",
|
| 43 |
+
"term",
|
| 44 |
+
"clinical study",
|
| 45 |
+
"summary",
|
| 46 |
+
"command"
|
| 47 |
+
]},
|
| 48 |
+
"prompts":[
|
| 49 |
+
"assessment_step",
|
| 50 |
+
"template_name",
|
| 51 |
+
"instruction"
|
| 52 |
+
],
|
| 53 |
+
"articles":[
|
| 54 |
+
"domain",
|
| 55 |
+
"name",
|
| 56 |
+
"s3_path",
|
| 57 |
+
"vector",
|
| 58 |
+
"meta",
|
| 59 |
+
"content"
|
| 60 |
+
],
|
| 61 |
+
"outputs":[
|
| 62 |
+
"domain",
|
| 63 |
+
"article",
|
| 64 |
+
"output"
|
| 65 |
+
]
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
+
'''
|
| 69 |
+
application default data
|
| 70 |
+
'''
|
| 71 |
+
app_data = {
|
| 72 |
+
"articles":[],
|
| 73 |
+
"terms":[],
|
| 74 |
+
"prompts":[],
|
| 75 |
+
"outputs":[]
|
| 76 |
+
}
|
cloud_db.py
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import boto3
|
| 2 |
+
|
| 3 |
+
from utility import *
|
| 4 |
+
|
| 5 |
+
# initialize dynamodb instance
|
| 6 |
+
db_client = boto3.client(
|
| 7 |
+
"dynamodb",
|
| 8 |
+
aws_access_key_id = aws_access_key_id,
|
| 9 |
+
aws_secret_access_key = aws_secret_access_key
|
| 10 |
+
)
|
| 11 |
+
|
| 12 |
+
'''
|
| 13 |
+
dynamodb data operations
|
| 14 |
+
'''
|
| 15 |
+
|
| 16 |
+
# get the list of articles from articles table in dynamodb
|
| 17 |
+
def get_table(table_name:str):
|
| 18 |
+
result = db_client.scan(TableName = table_name,AttributesToGet = data_structure[table_name])
|
| 19 |
+
return db_list_to_py_list(result["Items"])
|
| 20 |
+
|
| 21 |
+
# add a new article to table articles in dynamodb, return error if failed
|
| 22 |
+
def post_item(table_name:str,item:dict):
|
| 23 |
+
try:
|
| 24 |
+
res = db_client.put_item(
|
| 25 |
+
TableName = table_name,
|
| 26 |
+
Item = py_dict_to_db_map(item)
|
| 27 |
+
)
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return {"Error":e}
|
| 30 |
+
return res
|
| 31 |
+
|
| 32 |
+
# update an article in table articles in dynamodb, return error if failed
|
| 33 |
+
def put_item(table_name:str,item:dict):
|
| 34 |
+
try:
|
| 35 |
+
res = db_client.put_item(
|
| 36 |
+
TableName = table_name,
|
| 37 |
+
Item = py_dict_to_db_map(item)
|
| 38 |
+
)
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return {"Error":e}
|
| 41 |
+
return res
|
| 42 |
+
|
| 43 |
+
# delete an article in table articles in dynamodb, return error if not found.
|
| 44 |
+
def delete_item(table_name:str,key:dict):
|
| 45 |
+
try:
|
| 46 |
+
res = db_client.delete_item(
|
| 47 |
+
TableName = table_name,
|
| 48 |
+
Key = py_dict_to_db_map(key)
|
| 49 |
+
)
|
| 50 |
+
except Exception as e:
|
| 51 |
+
return {"Error":e}
|
| 52 |
+
return res
|
| 53 |
+
|
| 54 |
+
def get_item(table_name:str,key:dict):
|
| 55 |
+
try:
|
| 56 |
+
res = db_client.get_item(
|
| 57 |
+
TableName = table_name,
|
| 58 |
+
Key = py_dict_to_db_map(key)
|
| 59 |
+
)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return {"Error":e}
|
| 62 |
+
return res
|
| 63 |
+
|
| 64 |
+
'''
|
| 65 |
+
dynamodb structure management
|
| 66 |
+
'''
|
| 67 |
+
def get_structure(table_name:str):
|
| 68 |
+
result = db_client.describe_table(TableName = table_name)
|
| 69 |
+
return result["Table"]["AttributeDefinitions"]
|
invoices.py
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from cloud_db import *
|
| 2 |
+
|
| 3 |
+
def get_invoices(user_id:str):
|
| 4 |
+
return db_map_to_pd_dataframe(get_table("invoices",{"user_id":user_id}))
|
| 5 |
+
|
| 6 |
+
def get_invoice(user_id:str,invoice_id:str):
|
| 7 |
+
return get_item("invoices",{"user_id":user_id,"invoice_id":invoice_id})
|
| 8 |
+
|
user.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from cloud_db import *
|
| 2 |
+
|
| 3 |
+
def get_user(user_id:str):
|
| 4 |
+
return get_item("users",{"user_id":user_id})
|
| 5 |
+
|
| 6 |
+
def put_billing_info(user_id:str,billing_info:dict=None,payment_method:dict=None):
|
| 7 |
+
if not billing_info and not payment_method:
|
| 8 |
+
return {"Error":"No billing info or payment method provided"}
|
| 9 |
+
|
| 10 |
+
return put_item("users",{"user_id":user_id,"billing_info":billing_info,"payment_method":payment_method})
|
utility.py
ADDED
|
@@ -0,0 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
# import PyPDF2
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
from application import *
|
| 7 |
+
|
| 8 |
+
'''
|
| 9 |
+
following functions are for file manipulation
|
| 10 |
+
'''
|
| 11 |
+
|
| 12 |
+
# read pdf file and return text
|
| 13 |
+
def read_pdf(file_path):
|
| 14 |
+
# open the pdf file
|
| 15 |
+
try:
|
| 16 |
+
filename = file_path
|
| 17 |
+
pdfFileObj = open(file_path, 'rb')
|
| 18 |
+
except TypeError:
|
| 19 |
+
filename = file_path.name
|
| 20 |
+
pdfFileObj = open(file_path.name, 'rb')
|
| 21 |
+
|
| 22 |
+
# create a pdf reader object
|
| 23 |
+
pdfReader = PyPDF2.PdfReader(pdfFileObj)
|
| 24 |
+
|
| 25 |
+
# get the number of pages in the pdf file
|
| 26 |
+
num_pages = len(pdfReader.pages)
|
| 27 |
+
|
| 28 |
+
# create an empty string
|
| 29 |
+
text = ''
|
| 30 |
+
|
| 31 |
+
# iterate through all the pages
|
| 32 |
+
for page_num in range(num_pages):
|
| 33 |
+
page_obj = pdfReader.pages[page_num]
|
| 34 |
+
text += page_obj.extract_text ()
|
| 35 |
+
|
| 36 |
+
# close the pdf file object
|
| 37 |
+
pdfFileObj.close()
|
| 38 |
+
|
| 39 |
+
text = remove_symbols(text)
|
| 40 |
+
|
| 41 |
+
with open(f"{filename.split('.')[0]}.txt", "w") as f:
|
| 42 |
+
f.write(text)
|
| 43 |
+
|
| 44 |
+
# return the string of text
|
| 45 |
+
return text, pdfReader.metadata
|
| 46 |
+
|
| 47 |
+
'''
|
| 48 |
+
following functions are for format standard response
|
| 49 |
+
'''
|
| 50 |
+
|
| 51 |
+
# format standard response for status code and data
|
| 52 |
+
def format_response(code,data):
|
| 53 |
+
return {
|
| 54 |
+
"statusCode":code,
|
| 55 |
+
"headers":{
|
| 56 |
+
"Access-Control-Allow-Origin": "*",
|
| 57 |
+
"Content-Type": "application/json"
|
| 58 |
+
},
|
| 59 |
+
"body":json.dumps(data),
|
| 60 |
+
"isBase64Encoded": False
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
'''
|
| 64 |
+
following functions are for string manipulation
|
| 65 |
+
'''
|
| 66 |
+
|
| 67 |
+
# format text output by removing excessive characters
|
| 68 |
+
def format_text(text,remove_char_ls = ["\\n--\\n","\\n\\n","\n"]):
|
| 69 |
+
for c in remove_char_ls:
|
| 70 |
+
text = text.replace(c,"")
|
| 71 |
+
|
| 72 |
+
return text
|
| 73 |
+
|
| 74 |
+
# function to remove symbols that are not in unicode
|
| 75 |
+
def remove_symbols(text):
|
| 76 |
+
# remove symbols that are not in unicode
|
| 77 |
+
text = text.encode("ascii", "ignore").decode()
|
| 78 |
+
# remove the break word new line return
|
| 79 |
+
text = text.replace('-\n', '')
|
| 80 |
+
return text
|
| 81 |
+
|
| 82 |
+
def str_to_tuple(s):
|
| 83 |
+
return tuple(s.replace("(","").replace(")","").split(","))
|
| 84 |
+
|
| 85 |
+
'''
|
| 86 |
+
following functions are for dynamodb data manipulation
|
| 87 |
+
'''
|
| 88 |
+
# convert dynamodb map to python dictionary
|
| 89 |
+
def db_map_to_py_dict(db_map):
|
| 90 |
+
py_dict = {}
|
| 91 |
+
for k,i in db_map.items():
|
| 92 |
+
for l,v in i.items():
|
| 93 |
+
if l == "M":
|
| 94 |
+
py_dict[k] = db_map_to_py_dict(v)
|
| 95 |
+
elif l == "S":
|
| 96 |
+
py_dict[k] = v
|
| 97 |
+
elif l == "N":
|
| 98 |
+
py_dict[k] = int(v) if float(v)%1 ==0 else float(v)
|
| 99 |
+
elif l == "L":
|
| 100 |
+
py_dict[k] = db_list_to_py_list(v)
|
| 101 |
+
else:
|
| 102 |
+
py_dict[k] = v
|
| 103 |
+
|
| 104 |
+
return py_dict
|
| 105 |
+
|
| 106 |
+
def db_map_to_pd_dataframe(db_map):
|
| 107 |
+
py_dict = db_map_to_py_dict(db_map)
|
| 108 |
+
return pd.DataFrame(py_dict)
|
| 109 |
+
|
| 110 |
+
# convert python dictionary to dynamodb map
|
| 111 |
+
def py_dict_to_db_map(py_dict):
|
| 112 |
+
db_map = {}
|
| 113 |
+
for key,value in py_dict.items():
|
| 114 |
+
key = str(key)
|
| 115 |
+
if type(value) is str:
|
| 116 |
+
db_map[key] = {"S":value}
|
| 117 |
+
elif type(value) is int or type(value) is float:
|
| 118 |
+
db_map[key] = {"N":value}
|
| 119 |
+
elif type(value) is dict:
|
| 120 |
+
db_map[key] = {"M":py_dict_to_db_map(value)}
|
| 121 |
+
elif type(value) is list:
|
| 122 |
+
db_map[key] = {"L":py_list_to_db_list(value)}
|
| 123 |
+
|
| 124 |
+
return db_map
|
| 125 |
+
|
| 126 |
+
# convert dynamodb list to python list
|
| 127 |
+
def db_list_to_py_list(db_list):
|
| 128 |
+
py_list = []
|
| 129 |
+
for d in db_list:
|
| 130 |
+
for t,v in d.items():
|
| 131 |
+
if t == "M":
|
| 132 |
+
py_list.append(db_map_to_py_dict(v))
|
| 133 |
+
elif t == "L":
|
| 134 |
+
py_list.append(db_list_to_py_list(v))
|
| 135 |
+
else:
|
| 136 |
+
py_list.append(v)
|
| 137 |
+
|
| 138 |
+
return py_list
|
| 139 |
+
|
| 140 |
+
# convert python list to dynamodb list
|
| 141 |
+
def py_list_to_db_list(py_list):
|
| 142 |
+
db_list = []
|
| 143 |
+
for value in py_list:
|
| 144 |
+
if type(value) is str:
|
| 145 |
+
item = {"S":value}
|
| 146 |
+
elif type(value) is int or float:
|
| 147 |
+
item = {"N":value}
|
| 148 |
+
elif type(value) is dict:
|
| 149 |
+
item = {"M":py_dict_to_db_map(value)}
|
| 150 |
+
elif type(value) is list:
|
| 151 |
+
item = {"L":py_list_to_db_list(value)}
|
| 152 |
+
|
| 153 |
+
db_list.append(item)
|
| 154 |
+
|
| 155 |
+
return db_list
|
| 156 |
+
|
| 157 |
+
'''
|
| 158 |
+
following functions are used for business logic. (to be moved to business logic layer)
|
| 159 |
+
'''
|
| 160 |
+
|
| 161 |
+
# function to calculate the estimated cost of the translation
|
| 162 |
+
def est_cost(n_tokens,rate):
|
| 163 |
+
return round(rate*n_tokens/1000,4)
|