Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

stage_02_prepare.py 2.0 KB

You have to be logged in to leave a comment. Sign In
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
  1. import argparse
  2. import os
  3. import shutil
  4. from tqdm import tqdm
  5. import logging
  6. from src.utils.common import read_yaml, create_directories
  7. from src.utils.data_mgmt import process_posts
  8. import random
  9. STAGE = "stage 02 prepare data" ## <<< change stage name
  10. logging.basicConfig(
  11. filename=os.path.join("logs", 'running_logs.log'),
  12. level=logging.INFO,
  13. format="[%(asctime)s: %(levelname)s: %(module)s]: %(message)s",
  14. filemode="a"
  15. )
  16. def main(config_path, params_path):
  17. ## read config files
  18. config = read_yaml(config_path)
  19. params = read_yaml(params_path)
  20. local_data_dir = config["source_download_dir"]["data_dir"]
  21. data_filename = config["source_download_dir"]["data_file"]
  22. input_data = os.path.join(local_data_dir, data_filename)
  23. split = params["prepare"]["split"]
  24. seed = params["prepare"]["seed"]
  25. random.seed(seed)
  26. artifacts = config["artifacts"]
  27. prepared_data_dir_path = os.path.join(artifacts["ARTIFACTS_DIR"], artifacts["PREPARED_DATA"])
  28. create_directories([prepared_data_dir_path])
  29. train_data_path = os.path.join(prepared_data_dir_path, artifacts["TRAIN_DATA"])
  30. test_data_path = os.path.join(prepared_data_dir_path, artifacts["TEST_DATA"])
  31. encode = "utf8"
  32. with open(input_data, encoding=encode) as f_in:
  33. with open(train_data_path, "w", encoding=encode) as f_out_train:
  34. with open(test_data_path, "w", encoding=encode) as f_out_test:
  35. process_posts(f_in, f_out_train, f_out_test, "<python>", split)
  36. if __name__ == '__main__':
  37. args = argparse.ArgumentParser()
  38. args.add_argument("--config", "-c", default="configs/config.yaml")
  39. args.add_argument("--params", "-p", default="params.yaml")
  40. parsed_args = args.parse_args()
  41. try:
  42. logging.info("\n********************")
  43. logging.info(f">>>>> stage {STAGE} started <<<<<")
  44. main(config_path=parsed_args.config, params_path=parsed_args.params)
  45. logging.info(f">>>>> stage {STAGE} completed!<<<<<\n")
  46. except Exception as e:
  47. logging.exception(e)
  48. raise e
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...