Roland Ding commited on
Commit
617260a
·
1 Parent(s): de53278

1.0.1.1 ready for public relase

Browse files
Files changed (4) hide show
  1. app.py +26 -0
  2. application.py +76 -0
  3. cloud_db.py +71 -0
  4. utility.py +158 -0
app.py ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ with gr.Blocks(
4
+ css="footer {visibility: hidden}",
5
+ theme='JohnSmith9982/small_and_pretty',
6
+ # label="Profile"
7
+ ) as ui:
8
+ gr.HTML("<h1>User Profile</h1>")
9
+ name = gr.Textbox(lines=1, label="Name", placeholder="Enter your name here")
10
+
11
+ address = gr.Textbox(lines=2, label="Address", placeholder="Enter your address here")
12
+ with gr.Row():
13
+ city = gr.Textbox(lines=1, label="City", placeholder="Enter your city here")
14
+ state = gr.Textbox(lines=1, label="State", placeholder="Enter your state here")
15
+ zip_code = gr.Textbox(lines=1, label="Zip Code", placeholder="Enter your zip code here")
16
+
17
+ with gr.Row():
18
+ email = gr.Textbox(lines=1, label="Email", placeholder="Enter your email here")
19
+ phone = gr.Textbox(lines=1, label="Phone", placeholder="Enter your phone number here")
20
+
21
+ with gr.Row():
22
+ info_reset_button = gr.Button("Reset")
23
+ info_save_button = gr.Button("Save")
24
+
25
+ if __name__ == "__main__":
26
+ ui.launch()
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,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
55
+
56
+ def get_item(table_name:str,key:dict):
57
+ try:
58
+ res = db_client.get_item(
59
+ TableName = table_name,
60
+ Key = py_dict_to_db_map(key)
61
+ )
62
+ except Exception as e:
63
+ return {"Error":e}
64
+ return res
65
+
66
+ '''
67
+ dynamodb structure management
68
+ '''
69
+ def get_structure(table_name:str):
70
+ result = db_client.describe_table(TableName = table_name)
71
+ return result["Table"]["AttributeDefinitions"]
utility.py ADDED
@@ -0,0 +1,158 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import os
3
+ import PyPDF2
4
+
5
+ from application import *
6
+
7
+ '''
8
+ following functions are for file manipulation
9
+ '''
10
+
11
+ # read pdf file and return text
12
+ def read_pdf(file_path):
13
+ # open the pdf file
14
+ try:
15
+ filename = file_path
16
+ pdfFileObj = open(file_path, 'rb')
17
+ except TypeError:
18
+ filename = file_path.name
19
+ pdfFileObj = open(file_path.name, 'rb')
20
+
21
+ # create a pdf reader object
22
+ pdfReader = PyPDF2.PdfReader(pdfFileObj)
23
+
24
+ # get the number of pages in the pdf file
25
+ num_pages = len(pdfReader.pages)
26
+
27
+ # create an empty string
28
+ text = ''
29
+
30
+ # iterate through all the pages
31
+ for page_num in range(num_pages):
32
+ page_obj = pdfReader.pages[page_num]
33
+ text += page_obj.extract_text ()
34
+
35
+ # close the pdf file object
36
+ pdfFileObj.close()
37
+
38
+ text = remove_symbols(text)
39
+
40
+ with open(f"{filename.split('.')[0]}.txt", "w") as f:
41
+ f.write(text)
42
+
43
+ # return the string of text
44
+ return text, pdfReader.metadata
45
+
46
+ '''
47
+ following functions are for format standard response
48
+ '''
49
+
50
+ # format standard response for status code and data
51
+ def format_response(code,data):
52
+ return {
53
+ "statusCode":code,
54
+ "headers":{
55
+ "Access-Control-Allow-Origin": "*",
56
+ "Content-Type": "application/json"
57
+ },
58
+ "body":json.dumps(data),
59
+ "isBase64Encoded": False
60
+ }
61
+
62
+ '''
63
+ following functions are for string manipulation
64
+ '''
65
+
66
+ # format text output by removing excessive characters
67
+ def format_text(text,remove_char_ls = ["\\n--\\n","\\n\\n","\n"]):
68
+ for c in remove_char_ls:
69
+ text = text.replace(c,"")
70
+
71
+ return text
72
+
73
+ # function to remove symbols that are not in unicode
74
+ def remove_symbols(text):
75
+ # remove symbols that are not in unicode
76
+ text = text.encode("ascii", "ignore").decode()
77
+ # remove the break word new line return
78
+ text = text.replace('-\n', '')
79
+ return text
80
+
81
+ def str_to_tuple(s):
82
+ return tuple(s.replace("(","").replace(")","").split(","))
83
+
84
+ '''
85
+ following functions are for dynamodb data manipulation
86
+ '''
87
+ # convert dynamodb map to python dictionary
88
+ def db_map_to_py_dict(db_map):
89
+ py_dict = {}
90
+ for k,i in db_map.items():
91
+ for l,v in i.items():
92
+ if l == "M":
93
+ py_dict[k] = db_map_to_py_dict(v)
94
+ elif l == "S":
95
+ py_dict[k] = v
96
+ elif l == "N":
97
+ py_dict[k] = int(v) if float(v)%1 ==0 else float(v)
98
+ elif l == "L":
99
+ py_dict[k] = db_list_to_py_list(v)
100
+ else:
101
+ py_dict[k] = v
102
+
103
+ return py_dict
104
+
105
+ # convert python dictionary to dynamodb map
106
+ def py_dict_to_db_map(py_dict):
107
+ db_map = {}
108
+ for key,value in py_dict.items():
109
+ key = str(key)
110
+ if type(value) is str:
111
+ db_map[key] = {"S":value}
112
+ elif type(value) is int or type(value) is float:
113
+ db_map[key] = {"N":value}
114
+ elif type(value) is dict:
115
+ db_map[key] = {"M":py_dict_to_db_map(value)}
116
+ elif type(value) is list:
117
+ db_map[key] = {"L":py_list_to_db_list(value)}
118
+
119
+ return db_map
120
+
121
+ # convert dynamodb list to python list
122
+ def db_list_to_py_list(db_list):
123
+ py_list = []
124
+ for d in db_list:
125
+ for t,v in d.items():
126
+ if t == "M":
127
+ py_list.append(db_map_to_py_dict(v))
128
+ elif t == "L":
129
+ py_list.append(db_list_to_py_list(v))
130
+ else:
131
+ py_list.append(v)
132
+
133
+ return py_list
134
+
135
+ # convert python list to dynamodb list
136
+ def py_list_to_db_list(py_list):
137
+ db_list = []
138
+ for value in py_list:
139
+ if type(value) is str:
140
+ item = {"S":value}
141
+ elif type(value) is int or float:
142
+ item = {"N":value}
143
+ elif type(value) is dict:
144
+ item = {"M":py_dict_to_db_map(value)}
145
+ elif type(value) is list:
146
+ item = {"L":py_list_to_db_list(value)}
147
+
148
+ db_list.append(item)
149
+
150
+ return db_list
151
+
152
+ '''
153
+ following functions are used for business logic. (to be moved to business logic layer)
154
+ '''
155
+
156
+ # function to calculate the estimated cost of the translation
157
+ def est_cost(n_tokens,rate):
158
+ return round(rate*n_tokens/1000,4)