Roland Ding commited on
Commit
e8f5f18
·
1 Parent(s): ddc46db

8.7.20.54 added terminal print out function as a decorator to wrap up function and provide a terminal view for the code it executed.

Browse files
Files changed (4) hide show
  1. cloud_db.py +5 -0
  2. cloud_storage.py +7 -1
  3. supplier.py +10 -3
  4. utility.py +64 -0
cloud_db.py CHANGED
@@ -15,6 +15,7 @@ dynamodb data operations
15
  '''
16
 
17
  # get the list of articles from articles table in dynamodb
 
18
  def get_table(table_name:str):
19
  result = db_client.scan(TableName = table_name)#,AttributesToGet = data_structure[table_name]["fields"])
20
  return [db_map_to_py_dict(r) for r in result["Items"]]
@@ -31,6 +32,7 @@ def post_item(table_name:str,item:dict):
31
  return res
32
 
33
  # update an article in table articles in dynamodb, return error if failed
 
34
  def put_item(table_name:str,item:dict):
35
  try:
36
  res = db_client.put_item(
@@ -42,6 +44,7 @@ def put_item(table_name:str,item:dict):
42
  return res
43
 
44
  # delete an article in table articles in dynamodb, return error if not found.
 
45
  def delete_item(table_name:str,key:dict):
46
  try:
47
  res = db_client.delete_item(
@@ -55,6 +58,7 @@ def delete_item(table_name:str,key:dict):
55
 
56
  '''
57
  '''
 
58
  def get_item(table_name:str,key:dict):
59
  try:
60
  res = db_client.get_item(
@@ -68,6 +72,7 @@ def get_item(table_name:str,key:dict):
68
  '''
69
  dynamodb structure management
70
  '''
 
71
  def get_structure(table_name:str):
72
  result = db_client.describe_table(TableName = table_name)
73
  return result["Table"]["AttributeDefinitions"]
 
15
  '''
16
 
17
  # get the list of articles from articles table in dynamodb
18
+ @terminal_print
19
  def get_table(table_name:str):
20
  result = db_client.scan(TableName = table_name)#,AttributesToGet = data_structure[table_name]["fields"])
21
  return [db_map_to_py_dict(r) for r in result["Items"]]
 
32
  return res
33
 
34
  # update an article in table articles in dynamodb, return error if failed
35
+ @terminal_print
36
  def put_item(table_name:str,item:dict):
37
  try:
38
  res = db_client.put_item(
 
44
  return res
45
 
46
  # delete an article in table articles in dynamodb, return error if not found.
47
+ @terminal_print
48
  def delete_item(table_name:str,key:dict):
49
  try:
50
  res = db_client.delete_item(
 
58
 
59
  '''
60
  '''
61
+ @terminal_print
62
  def get_item(table_name:str,key:dict):
63
  try:
64
  res = db_client.get_item(
 
72
  '''
73
  dynamodb structure management
74
  '''
75
+ @terminal_print
76
  def get_structure(table_name:str):
77
  result = db_client.describe_table(TableName = table_name)
78
  return result["Table"]["AttributeDefinitions"]
cloud_storage.py CHANGED
@@ -2,7 +2,7 @@ import boto3
2
  import os
3
  import tempfile
4
 
5
- from utility import aws_access_key_id, aws_secret_access_key
6
 
7
  s3 = boto3.client(
8
  's3',
@@ -11,6 +11,7 @@ s3 = boto3.client(
11
  )
12
 
13
  # post or update a file to s3
 
14
  def upload_file(path, bucket, object_name=None):
15
  """Upload a file to an S3 bucket
16
 
@@ -33,6 +34,7 @@ def upload_file(path, bucket, object_name=None):
33
  return False
34
  return res
35
 
 
36
  def upload_fileobj(file_obj, bucket, object_name=None):
37
  '''
38
  Upload a file object to an S3 bucket
@@ -55,6 +57,7 @@ def upload_fileobj(file_obj, bucket, object_name=None):
55
  return res
56
 
57
  # get a file from s3
 
58
  def download_file(bucket, object_name, file_name=None):
59
  """Download a file from an S3 bucket
60
 
@@ -75,6 +78,7 @@ def download_file(bucket, object_name, file_name=None):
75
  return True
76
 
77
  # download a file object from s3
 
78
  def download_fileobj(bucket, object_name, temp_obj=None):
79
  '''
80
  Download a file object from an S3 bucket
@@ -97,6 +101,7 @@ def download_fileobj(bucket, object_name, temp_obj=None):
97
  return temp_obj
98
 
99
  # delete a file from s3
 
100
  def delete_file(bucket, object_name):
101
  """Delete a file from an S3 bucket
102
 
@@ -112,6 +117,7 @@ def delete_file(bucket, object_name):
112
  return response
113
 
114
  # list all files in a bucket
 
115
  def list_files(bucket):
116
  """List files in a bucket
117
 
 
2
  import os
3
  import tempfile
4
 
5
+ from utility import aws_access_key_id, aws_secret_access_key,terminal_print
6
 
7
  s3 = boto3.client(
8
  's3',
 
11
  )
12
 
13
  # post or update a file to s3
14
+ @terminal_print
15
  def upload_file(path, bucket, object_name=None):
16
  """Upload a file to an S3 bucket
17
 
 
34
  return False
35
  return res
36
 
37
+ @terminal_print
38
  def upload_fileobj(file_obj, bucket, object_name=None):
39
  '''
40
  Upload a file object to an S3 bucket
 
57
  return res
58
 
59
  # get a file from s3
60
+ @terminal_print
61
  def download_file(bucket, object_name, file_name=None):
62
  """Download a file from an S3 bucket
63
 
 
78
  return True
79
 
80
  # download a file object from s3
81
+ @terminal_print
82
  def download_fileobj(bucket, object_name, temp_obj=None):
83
  '''
84
  Download a file object from an S3 bucket
 
101
  return temp_obj
102
 
103
  # delete a file from s3
104
+ @terminal_print
105
  def delete_file(bucket, object_name):
106
  """Delete a file from an S3 bucket
107
 
 
117
  return response
118
 
119
  # list all files in a bucket
120
+ @terminal_print
121
  def list_files(bucket):
122
  """List files in a bucket
123
 
supplier.py CHANGED
@@ -2,11 +2,12 @@ import openai
2
  import tiktoken
3
 
4
  from application import *
 
5
 
6
  openai.api_key = openai_api_key
7
  token_encoder = tiktoken.get_encoding("cl100k_base")
8
 
9
-
10
  def execute_prompt(prompt):
11
  '''
12
  execute_prompt function takes two arguments: text and prompt
@@ -28,22 +29,28 @@ def execute_prompt(prompt):
28
  )
29
  return res.choices[0]["text"] if res.choices else "<error> failed to generate text</error>"
30
 
 
31
  def format(**kwargs):
32
  if "format" in kwargs:
33
  return kwargs["format"]
34
  return kwargs
35
 
36
 
 
37
  def execute_instruction(article, instruction,model="gpt-3.5-turbo-16k",format="markdown"):
38
  '''
39
  execute_instruction function takes three arguments: article, instruction and model
40
 
 
 
41
  article: the raw text from the article source
42
  instruction: the instruction for the rational execution it needs to complete
43
- model: the model used for the rational execution, default to gpt-3.5-turbo-16k
44
- format: the format of the table, default to markdown
45
 
46
  return: a string, the result of the rational execution
 
 
47
  '''
48
  msg_stream = [
49
  {
 
2
  import tiktoken
3
 
4
  from application import *
5
+ from utility import terminal_print
6
 
7
  openai.api_key = openai_api_key
8
  token_encoder = tiktoken.get_encoding("cl100k_base")
9
 
10
+ @terminal_print
11
  def execute_prompt(prompt):
12
  '''
13
  execute_prompt function takes two arguments: text and prompt
 
29
  )
30
  return res.choices[0]["text"] if res.choices else "<error> failed to generate text</error>"
31
 
32
+ @terminal_print
33
  def format(**kwargs):
34
  if "format" in kwargs:
35
  return kwargs["format"]
36
  return kwargs
37
 
38
 
39
+ @terminal_print
40
  def execute_instruction(article, instruction,model="gpt-3.5-turbo-16k",format="markdown"):
41
  '''
42
  execute_instruction function takes three arguments: article, instruction and model
43
 
44
+ Parameters
45
+ ----------
46
  article: the raw text from the article source
47
  instruction: the instruction for the rational execution it needs to complete
48
+ model: the model to use for the rational execution
49
+ format: the format of the table to be formatted
50
 
51
  return: a string, the result of the rational execution
52
+
53
+
54
  '''
55
  msg_stream = [
56
  {
utility.py CHANGED
@@ -6,6 +6,26 @@ from pdfminer.high_level import extract_text
6
  from pdfminer.pdfparser import PDFParser
7
  from pdfminer.pdfdocument import PDFDocument
8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  '''
11
  following functions are for file manipulation
@@ -15,6 +35,7 @@ keyword_search = lambda kw, text: kw.lower() in text.lower()
15
  list_or = lambda l: sum(l)>0
16
  list_and = lambda l: sum(l)==len(l)
17
 
 
18
  def read_pdf(file_path):
19
  '''
20
  this function read the pdf file and return the text
@@ -86,6 +107,7 @@ def format_response(code,data):
86
  following functions are for string manipulation
87
  '''
88
 
 
89
  def format_text(text,remove_char_ls = ["\\n--\\n","\\n\\n","\n"]):
90
  '''
91
  this function format the text output by removing excessive characters
@@ -105,6 +127,7 @@ def format_text(text,remove_char_ls = ["\\n--\\n","\\n\\n","\n"]):
105
 
106
  return text
107
 
 
108
  def remove_symbols(text):
109
  '''
110
  this function remove symbols that are not in unicode
@@ -123,6 +146,7 @@ def remove_symbols(text):
123
  text = text.replace('-\n', '')
124
  return text
125
 
 
126
  def remove_citation(text):
127
  '''
128
  this function remove citation pattern in the text
@@ -155,6 +179,7 @@ def str_to_tuple(s):
155
  '''
156
  return tuple(s.replace("(","").replace(")","").split(","))
157
 
 
158
  def replace_symbols(s):
159
  '''
160
  this function replace symbols in the string to comply with file names
@@ -186,6 +211,7 @@ def replace_symbols(s):
186
  following functions are for dynamodb data manipulation
187
  '''
188
 
 
189
  def db_map_to_py_dict(db_map):
190
  '''
191
  this function convert dynamodb map data structure to python dictionary
@@ -222,6 +248,7 @@ def db_map_to_py_dict(db_map):
222
 
223
  return py_dict
224
 
 
225
  def py_dict_to_db_map(py_dict):
226
  '''
227
  this function convert python dictionary to dynamodb map data structure
@@ -255,6 +282,7 @@ def py_dict_to_db_map(py_dict):
255
  db_map[key] = {"NULL":True}
256
  return db_map
257
 
 
258
  def db_list_to_py_list(db_list):
259
  '''
260
  this function convert dynamodb list data structure to python list
@@ -283,6 +311,7 @@ def db_list_to_py_list(db_list):
283
 
284
  return py_list
285
 
 
286
  def py_list_to_db_list(py_list):
287
  '''
288
  this function convert python list to dynamodb list data structure
@@ -321,10 +350,45 @@ def py_list_to_db_list(py_list):
321
 
322
  return db_list
323
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324
  '''
325
  following functions are used for business logic. (to be moved to business logic layer)
326
  '''
327
 
 
328
  def est_cost(n_tokens,rate):
329
  '''
330
  this function calculate the estimated cost of the translation
 
6
  from pdfminer.pdfparser import PDFParser
7
  from pdfminer.pdfdocument import PDFDocument
8
 
9
+ '''
10
+ universal system functions
11
+ '''
12
+
13
+ def terminal_print(func):
14
+ from datetime import datetime
15
+ # import os
16
+ def wrapper(*args, **kwargs):
17
+ start = datetime.now()
18
+ print(f"{start.strftime('%y-%m-%d %H:%M:%S')} - executing: {func.__name__}")
19
+
20
+ result = func(*args, **kwargs)
21
+
22
+ end = datetime.now()
23
+ print(f"{end.strftime('%y-%m-%d %H:%M:%S')} - completed: {func.__name__}, runtime: {end-start} seconds")
24
+
25
+ return result
26
+
27
+ return wrapper
28
+
29
 
30
  '''
31
  following functions are for file manipulation
 
35
  list_or = lambda l: sum(l)>0
36
  list_and = lambda l: sum(l)==len(l)
37
 
38
+ @terminal_print
39
  def read_pdf(file_path):
40
  '''
41
  this function read the pdf file and return the text
 
107
  following functions are for string manipulation
108
  '''
109
 
110
+ @terminal_print
111
  def format_text(text,remove_char_ls = ["\\n--\\n","\\n\\n","\n"]):
112
  '''
113
  this function format the text output by removing excessive characters
 
127
 
128
  return text
129
 
130
+ @terminal_print
131
  def remove_symbols(text):
132
  '''
133
  this function remove symbols that are not in unicode
 
146
  text = text.replace('-\n', '')
147
  return text
148
 
149
+ @terminal_print
150
  def remove_citation(text):
151
  '''
152
  this function remove citation pattern in the text
 
179
  '''
180
  return tuple(s.replace("(","").replace(")","").split(","))
181
 
182
+ @terminal_print
183
  def replace_symbols(s):
184
  '''
185
  this function replace symbols in the string to comply with file names
 
211
  following functions are for dynamodb data manipulation
212
  '''
213
 
214
+ @terminal_print
215
  def db_map_to_py_dict(db_map):
216
  '''
217
  this function convert dynamodb map data structure to python dictionary
 
248
 
249
  return py_dict
250
 
251
+ @terminal_print
252
  def py_dict_to_db_map(py_dict):
253
  '''
254
  this function convert python dictionary to dynamodb map data structure
 
282
  db_map[key] = {"NULL":True}
283
  return db_map
284
 
285
+ @terminal_print
286
  def db_list_to_py_list(db_list):
287
  '''
288
  this function convert dynamodb list data structure to python list
 
311
 
312
  return py_list
313
 
314
+ @terminal_print
315
  def py_list_to_db_list(py_list):
316
  '''
317
  this function convert python list to dynamodb list data structure
 
350
 
351
  return db_list
352
 
353
+ '''
354
+ following functions are for markdown table creation
355
+ '''
356
+
357
+ @terminal_print
358
+ def create_md_table(array):
359
+ '''
360
+ create markdown tables for an array.
361
+
362
+ Parameters
363
+ ----------
364
+ array: list
365
+ a table in the form of a list of lists
366
+
367
+ Returns
368
+ -------
369
+ md_table: str
370
+ '''
371
+ md_table = ""
372
+
373
+ for i,row in enumerate(array):
374
+ md_row = ""
375
+ for item in row:
376
+ md_item = f"| {item} "
377
+ md_row += md_item
378
+
379
+ md_row += " |\n"
380
+ md_table += md_row
381
+
382
+ if i == 0:
383
+ md_table += f"| {'|'.join(['---' for _ in range(len(row))])} |\n"
384
+
385
+ return md_table
386
+
387
  '''
388
  following functions are used for business logic. (to be moved to business logic layer)
389
  '''
390
 
391
+ @terminal_print
392
  def est_cost(n_tokens,rate):
393
  '''
394
  this function calculate the estimated cost of the translation