Spaces:
Paused
Paused
Commit ·
f030d3a
0
Parent(s):
Clean slate: fresh commit with current code
Browse files- .github/workflows/.gitkeep +0 -0
- .gitignore +194 -0
- Dockerfile +0 -0
- LICENSE +21 -0
- README.md +12 -0
- app.py +0 -0
- config/config.yaml +34 -0
- main.py +50 -0
- params.yaml +10 -0
- requirements.txt +21 -0
- research/01_data_ingestion.ipynb +217 -0
- research/02_data_validation.ipynb +202 -0
- research/03_data_transformation.ipynb +224 -0
- research/04_model_trainer.ipynb +642 -0
- research/05_Model_evaluation.ipynb +210 -0
- research/trails.ipynb +266 -0
- setup.py +27 -0
- src/textSummarizer/__init__.py +0 -0
- src/textSummarizer/components/__init__.py +0 -0
- src/textSummarizer/components/data_ingestion.py +34 -0
- src/textSummarizer/components/data_transformation.py +31 -0
- src/textSummarizer/components/data_validation.py +30 -0
- src/textSummarizer/components/model_trainer.py +57 -0
- src/textSummarizer/config/__init__.py +0 -0
- src/textSummarizer/config/configuration.py +85 -0
- src/textSummarizer/constants/__init__.py +4 -0
- src/textSummarizer/entity/__init__.py +40 -0
- src/textSummarizer/logging/__init__.py +21 -0
- src/textSummarizer/pipeline/__init__.py +0 -0
- src/textSummarizer/pipeline/stage_01_data_ingestion.py +16 -0
- src/textSummarizer/pipeline/stage_02_data_validation.py +15 -0
- src/textSummarizer/pipeline/stage_03_data_transformation.py +15 -0
- src/textSummarizer/pipeline/stage_04_model_trainer.py +15 -0
- src/textSummarizer/utils/__init__.py +0 -0
- src/textSummarizer/utils/common.py +64 -0
- template.py +46 -0
.github/workflows/.gitkeep
ADDED
|
File without changes
|
.gitignore
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Byte-compiled / optimized / DLL files
|
| 2 |
+
__pycache__/
|
| 3 |
+
*.py[cod]
|
| 4 |
+
*$py.class
|
| 5 |
+
|
| 6 |
+
# C extensions
|
| 7 |
+
*.so
|
| 8 |
+
|
| 9 |
+
# Distribution / packaging
|
| 10 |
+
.Python
|
| 11 |
+
build/
|
| 12 |
+
develop-eggs/
|
| 13 |
+
dist/
|
| 14 |
+
downloads/
|
| 15 |
+
eggs/
|
| 16 |
+
.eggs/
|
| 17 |
+
lib/
|
| 18 |
+
lib64/
|
| 19 |
+
parts/
|
| 20 |
+
sdist/
|
| 21 |
+
var/
|
| 22 |
+
wheels/
|
| 23 |
+
share/python-wheels/
|
| 24 |
+
*.egg-info/
|
| 25 |
+
.installed.cfg
|
| 26 |
+
*.egg
|
| 27 |
+
MANIFEST
|
| 28 |
+
|
| 29 |
+
# PyInstaller
|
| 30 |
+
# Usually these files are written by a python script from a template
|
| 31 |
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
| 32 |
+
*.manifest
|
| 33 |
+
*.spec
|
| 34 |
+
|
| 35 |
+
# Installer logs
|
| 36 |
+
pip-log.txt
|
| 37 |
+
pip-delete-this-directory.txt
|
| 38 |
+
|
| 39 |
+
# Unit test / coverage reports
|
| 40 |
+
htmlcov/
|
| 41 |
+
.tox/
|
| 42 |
+
.nox/
|
| 43 |
+
.coverage
|
| 44 |
+
.coverage.*
|
| 45 |
+
.cache
|
| 46 |
+
nosetests.xml
|
| 47 |
+
coverage.xml
|
| 48 |
+
*.cover
|
| 49 |
+
*.py,cover
|
| 50 |
+
.hypothesis/
|
| 51 |
+
.pytest_cache/
|
| 52 |
+
cover/
|
| 53 |
+
|
| 54 |
+
# Translations
|
| 55 |
+
*.mo
|
| 56 |
+
*.pot
|
| 57 |
+
|
| 58 |
+
# Django stuff:
|
| 59 |
+
*.log
|
| 60 |
+
local_settings.py
|
| 61 |
+
db.sqlite3
|
| 62 |
+
db.sqlite3-journal
|
| 63 |
+
|
| 64 |
+
# Flask stuff:
|
| 65 |
+
instance/
|
| 66 |
+
.webassets-cache
|
| 67 |
+
|
| 68 |
+
# Scrapy stuff:
|
| 69 |
+
.scrapy
|
| 70 |
+
|
| 71 |
+
# Sphinx documentation
|
| 72 |
+
docs/_build/
|
| 73 |
+
|
| 74 |
+
# PyBuilder
|
| 75 |
+
.pybuilder/
|
| 76 |
+
target/
|
| 77 |
+
|
| 78 |
+
# Jupyter Notebook
|
| 79 |
+
.ipynb_checkpoints
|
| 80 |
+
|
| 81 |
+
# IPython
|
| 82 |
+
profile_default/
|
| 83 |
+
ipython_config.py
|
| 84 |
+
|
| 85 |
+
# pyenv
|
| 86 |
+
# For a library or package, you might want to ignore these files since the code is
|
| 87 |
+
# intended to run in multiple environments; otherwise, check them in:
|
| 88 |
+
# .python-version
|
| 89 |
+
|
| 90 |
+
# pipenv
|
| 91 |
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
| 92 |
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
| 93 |
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
| 94 |
+
# install all needed dependencies.
|
| 95 |
+
#Pipfile.lock
|
| 96 |
+
|
| 97 |
+
# UV
|
| 98 |
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
| 99 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 100 |
+
# commonly ignored for libraries.
|
| 101 |
+
#uv.lock
|
| 102 |
+
|
| 103 |
+
# poetry
|
| 104 |
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
| 105 |
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
| 106 |
+
# commonly ignored for libraries.
|
| 107 |
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
| 108 |
+
#poetry.lock
|
| 109 |
+
|
| 110 |
+
# pdm
|
| 111 |
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
| 112 |
+
#pdm.lock
|
| 113 |
+
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
|
| 114 |
+
# in version control.
|
| 115 |
+
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
|
| 116 |
+
.pdm.toml
|
| 117 |
+
.pdm-python
|
| 118 |
+
.pdm-build/
|
| 119 |
+
|
| 120 |
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
| 121 |
+
__pypackages__/
|
| 122 |
+
|
| 123 |
+
# Celery stuff
|
| 124 |
+
celerybeat-schedule
|
| 125 |
+
celerybeat.pid
|
| 126 |
+
|
| 127 |
+
# SageMath parsed files
|
| 128 |
+
*.sage.py
|
| 129 |
+
|
| 130 |
+
# Environments
|
| 131 |
+
.env
|
| 132 |
+
.venv
|
| 133 |
+
env/
|
| 134 |
+
venv/
|
| 135 |
+
ENV/
|
| 136 |
+
env.bak/
|
| 137 |
+
venv.bak/
|
| 138 |
+
|
| 139 |
+
# Spyder project settings
|
| 140 |
+
.spyderproject
|
| 141 |
+
.spyproject
|
| 142 |
+
|
| 143 |
+
# Rope project settings
|
| 144 |
+
.ropeproject
|
| 145 |
+
|
| 146 |
+
# mkdocs documentation
|
| 147 |
+
/site
|
| 148 |
+
|
| 149 |
+
# mypy
|
| 150 |
+
.mypy_cache/
|
| 151 |
+
.dmypy.json
|
| 152 |
+
dmypy.json
|
| 153 |
+
|
| 154 |
+
# Pyre type checker
|
| 155 |
+
.pyre/
|
| 156 |
+
|
| 157 |
+
# pytype static type analyzer
|
| 158 |
+
.pytype/
|
| 159 |
+
|
| 160 |
+
# Cython debug symbols
|
| 161 |
+
cython_debug/
|
| 162 |
+
|
| 163 |
+
# PyCharm
|
| 164 |
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
| 165 |
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
| 166 |
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
| 167 |
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
| 168 |
+
#.idea/
|
| 169 |
+
|
| 170 |
+
# Abstra
|
| 171 |
+
# Abstra is an AI-powered process automation framework.
|
| 172 |
+
# Ignore directories containing user credentials, local state, and settings.
|
| 173 |
+
# Learn more at https://abstra.io/docs
|
| 174 |
+
.abstra/
|
| 175 |
+
|
| 176 |
+
# Visual Studio Code
|
| 177 |
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
| 178 |
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
| 179 |
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
| 180 |
+
# you could uncomment the following to ignore the enitre vscode folder
|
| 181 |
+
# .vscode/
|
| 182 |
+
|
| 183 |
+
# Ruff stuff:
|
| 184 |
+
.ruff_cache/
|
| 185 |
+
|
| 186 |
+
# PyPI configuration file
|
| 187 |
+
.pypirc
|
| 188 |
+
|
| 189 |
+
# Cursor
|
| 190 |
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
| 191 |
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
| 192 |
+
# refer to https://docs.cursor.com/context/ignore-files
|
| 193 |
+
.cursorignore
|
| 194 |
+
.cursorindexingignore
|
Dockerfile
ADDED
|
File without changes
|
LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2025 Jeevant
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# AI_Summarizer
|
| 2 |
+
|
| 3 |
+
## Workflows
|
| 4 |
+
|
| 5 |
+
1. Update Config.yaml
|
| 6 |
+
2. Update params.yaml
|
| 7 |
+
3. Update entity
|
| 8 |
+
4. Update the configuration manager in the src config
|
| 9 |
+
5. Update the components
|
| 10 |
+
6. Update the pipeline
|
| 11 |
+
7. Update the main.py
|
| 12 |
+
8. Update the app.py
|
app.py
ADDED
|
File without changes
|
config/config.yaml
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
artifacts_root: artifacts_root
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
data_ingestion:
|
| 5 |
+
root_dir: artifacts/data_ingestion
|
| 6 |
+
source_URL: https://github.com/entbappy/Branching-tutorial/raw/master/summarizer-data.zip
|
| 7 |
+
local_data_file: artifacts/data_ingestion/data.zip
|
| 8 |
+
unzip_dir : artifacts/data_ingestion
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
data_validation:
|
| 12 |
+
root_dir: artifacts/data_validation
|
| 13 |
+
STATUS_FILE: artifacts/data_validation/status.txt
|
| 14 |
+
ALL_REQUIRED_FILES: ["train" , "test" , "validation"]
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
data_transformation:
|
| 18 |
+
root_dir: artifacts/data_transformation
|
| 19 |
+
data_path: artifacts/data_ingestion/samsum_dataset
|
| 20 |
+
tokenizer_name: google/pegasus-cnn_dailymail
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
model_trainer:
|
| 24 |
+
root_dir: artifacts/model_trainer
|
| 25 |
+
data_path: artifacts/data_transformation/samsum_dataset
|
| 26 |
+
model_ckpt: google/pegasus-cnn_dailymail
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
model_evaluation:
|
| 30 |
+
root_dir: artifacts/model_evaluation
|
| 31 |
+
data_path: artifacts/data_transformation/samsum_dataset
|
| 32 |
+
model_path: artifacts/model_trainer/pegasus-samsum_model
|
| 33 |
+
tokenizer_path: artifacts/model_trainer/tokenizer_name
|
| 34 |
+
metrics_file_name: artifacts/model_evaluation/metrics.csv
|
main.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from textSummarizer.pipeline.stage_01_data_ingestion import DataIngestionTrainingPipeline
|
| 2 |
+
from textSummarizer.pipeline.stage_02_data_validation import DataValidationTrainingPipeline
|
| 3 |
+
from textSummarizer.pipeline.stage_03_data_transformation import DataTransformationTrainingPipeline
|
| 4 |
+
from textSummarizer.pipeline.stage_04_model_trainer import ModelTrainerTrainingPipeline
|
| 5 |
+
from textSummarizer.logging import logger
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
STAGE_NAME = "Data Ingestion Stage"
|
| 9 |
+
try:
|
| 10 |
+
logger.info(f">>>>>> stage {STAGE_NAME} started <<<<<<")
|
| 11 |
+
data_ingestion = DataIngestionTrainingPipeline()
|
| 12 |
+
data_ingestion.main()
|
| 13 |
+
logger.info(f">>>>>> stage {STAGE_NAME} completed <<<<<<\n\nx============x")
|
| 14 |
+
except Exception as e:
|
| 15 |
+
logger.exception(e)
|
| 16 |
+
raise e
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
STAGE_NAME = "Data Validation Stage"
|
| 20 |
+
try:
|
| 21 |
+
logger.info(f">>>>>> stage {STAGE_NAME} started <<<<<<")
|
| 22 |
+
data_validation = DataValidationTrainingPipeline()
|
| 23 |
+
data_validation.main()
|
| 24 |
+
logger.info(f">>>>>> stage {STAGE_NAME} completed <<<<<<\n\nx============x")
|
| 25 |
+
except Exception as e:
|
| 26 |
+
logger.exception(e)
|
| 27 |
+
raise e
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
STAGE_NAME = "Data Transformation Stage"
|
| 31 |
+
try:
|
| 32 |
+
logger.info(f">>>>>> stage {STAGE_NAME} started <<<<<<")
|
| 33 |
+
data_transformation = DataTransformationTrainingPipeline()
|
| 34 |
+
data_transformation.main()
|
| 35 |
+
logger.info(f">>>>>> stage {STAGE_NAME} completed <<<<<<\n\nx============x")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
logger.exception(e)
|
| 38 |
+
raise e
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
STAGE_NAME = "Model Trainer Stage"
|
| 42 |
+
try:
|
| 43 |
+
logger.info(f"********************************")
|
| 44 |
+
logger.info(f">>>>>> stage {STAGE_NAME} started <<<<<<")
|
| 45 |
+
model_trainer = ModelTrainerTrainingPipeline()
|
| 46 |
+
model_trainer.main()
|
| 47 |
+
logger.info(f">>>>>> stage {STAGE_NAME} completed <<<<<<\n\nx============x")
|
| 48 |
+
except Exception as e:
|
| 49 |
+
logger.exception(e)
|
| 50 |
+
raise e
|
params.yaml
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
TrainingArguments:
|
| 2 |
+
num_train_epochs: 1
|
| 3 |
+
warmup_steps: 500
|
| 4 |
+
per_device_train_batch_size: 1
|
| 5 |
+
weight_decay: 0.01
|
| 6 |
+
logging_steps: 10
|
| 7 |
+
evaluation_strategy: steps
|
| 8 |
+
eval_steps: 500
|
| 9 |
+
save_steps: 1e6
|
| 10 |
+
gradient_accumulation_steps: 16
|
requirements.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
transformers
|
| 2 |
+
transformers[sentencepiece]
|
| 3 |
+
dataset
|
| 4 |
+
sacrebleu
|
| 5 |
+
py7zr
|
| 6 |
+
pandas
|
| 7 |
+
nltk
|
| 8 |
+
tqdm
|
| 9 |
+
PyYAML
|
| 10 |
+
matplotlib
|
| 11 |
+
torch
|
| 12 |
+
notebook
|
| 13 |
+
boto3
|
| 14 |
+
mypy-boto3-s3
|
| 15 |
+
python-box==6.0.2
|
| 16 |
+
ensure==1.0.2
|
| 17 |
+
fastapi==0.78.0
|
| 18 |
+
uvicorn==0.18.3
|
| 19 |
+
Jinja2==3.1.2
|
| 20 |
+
rouge-score
|
| 21 |
+
-e .
|
research/01_data_ingestion.ipynb
ADDED
|
@@ -0,0 +1,217 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "66881b4b",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import os\n",
|
| 11 |
+
"import sys"
|
| 12 |
+
]
|
| 13 |
+
},
|
| 14 |
+
{
|
| 15 |
+
"cell_type": "code",
|
| 16 |
+
"execution_count": 2,
|
| 17 |
+
"id": "ef1f89d3",
|
| 18 |
+
"metadata": {},
|
| 19 |
+
"outputs": [],
|
| 20 |
+
"source": [
|
| 21 |
+
"%pwd\n",
|
| 22 |
+
"os.chdir(\"../\")"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
{
|
| 26 |
+
"cell_type": "code",
|
| 27 |
+
"execution_count": 3,
|
| 28 |
+
"id": "58bce27a",
|
| 29 |
+
"metadata": {},
|
| 30 |
+
"outputs": [
|
| 31 |
+
{
|
| 32 |
+
"data": {
|
| 33 |
+
"text/plain": [
|
| 34 |
+
"'c:\\\\order\\\\Desktop\\\\AI_Summarizer'"
|
| 35 |
+
]
|
| 36 |
+
},
|
| 37 |
+
"execution_count": 3,
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"output_type": "execute_result"
|
| 40 |
+
}
|
| 41 |
+
],
|
| 42 |
+
"source": [
|
| 43 |
+
"%pwd"
|
| 44 |
+
]
|
| 45 |
+
},
|
| 46 |
+
{
|
| 47 |
+
"cell_type": "code",
|
| 48 |
+
"execution_count": 4,
|
| 49 |
+
"id": "375cb3da",
|
| 50 |
+
"metadata": {},
|
| 51 |
+
"outputs": [],
|
| 52 |
+
"source": [
|
| 53 |
+
"from dataclasses import dataclass\n",
|
| 54 |
+
"from pathlib import Path\n",
|
| 55 |
+
"\n",
|
| 56 |
+
"@dataclass(frozen=True)\n",
|
| 57 |
+
"class DataIngestionConfig:\n",
|
| 58 |
+
" root_dir : Path\n",
|
| 59 |
+
" source_URL : str\n",
|
| 60 |
+
" local_data_file : Path\n",
|
| 61 |
+
" unzip_dir : Path"
|
| 62 |
+
]
|
| 63 |
+
},
|
| 64 |
+
{
|
| 65 |
+
"cell_type": "code",
|
| 66 |
+
"execution_count": 5,
|
| 67 |
+
"id": "486d78f0",
|
| 68 |
+
"metadata": {},
|
| 69 |
+
"outputs": [],
|
| 70 |
+
"source": [
|
| 71 |
+
"from textSummarizer.constants import *\n",
|
| 72 |
+
"from textSummarizer.utils.common import read_yaml, create_directories"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": 6,
|
| 78 |
+
"id": "e4d43738",
|
| 79 |
+
"metadata": {},
|
| 80 |
+
"outputs": [],
|
| 81 |
+
"source": [
|
| 82 |
+
"class ConfigurationManager:\n",
|
| 83 |
+
" def __init__(\n",
|
| 84 |
+
" self,\n",
|
| 85 |
+
" config_filepath: str = CONFIG_FILE_PATH,\n",
|
| 86 |
+
" params_filepath: str = PARAMS_FILE_PATH):\n",
|
| 87 |
+
" \n",
|
| 88 |
+
" self.config = read_yaml(config_filepath)\n",
|
| 89 |
+
" self.params = read_yaml(params_filepath)\n",
|
| 90 |
+
" \n",
|
| 91 |
+
" create_directories([self.config.artifacts_root])\n",
|
| 92 |
+
" \n",
|
| 93 |
+
" \n",
|
| 94 |
+
" def get_data_ingestion_config(self) -> DataIngestionConfig:\n",
|
| 95 |
+
" config =self.config.data_ingestion\n",
|
| 96 |
+
" \n",
|
| 97 |
+
" create_directories([config.root_dir])\n",
|
| 98 |
+
" \n",
|
| 99 |
+
" data_ingestion_config = DataIngestionConfig(\n",
|
| 100 |
+
" root_dir = config.root_dir,\n",
|
| 101 |
+
" source_URL = config.source_URL,\n",
|
| 102 |
+
" local_data_file = config.local_data_file,\n",
|
| 103 |
+
" unzip_dir = config.unzip_dir,\n",
|
| 104 |
+
" )\n",
|
| 105 |
+
" \n",
|
| 106 |
+
" return data_ingestion_config"
|
| 107 |
+
]
|
| 108 |
+
},
|
| 109 |
+
{
|
| 110 |
+
"cell_type": "code",
|
| 111 |
+
"execution_count": 7,
|
| 112 |
+
"id": "9606fbff",
|
| 113 |
+
"metadata": {},
|
| 114 |
+
"outputs": [],
|
| 115 |
+
"source": [
|
| 116 |
+
"import os\n",
|
| 117 |
+
"import urllib.request as request\n",
|
| 118 |
+
"import zipfile\n",
|
| 119 |
+
"from textSummarizer.logging import logger\n",
|
| 120 |
+
"from textSummarizer.utils.common import get_size"
|
| 121 |
+
]
|
| 122 |
+
},
|
| 123 |
+
{
|
| 124 |
+
"cell_type": "code",
|
| 125 |
+
"execution_count": 8,
|
| 126 |
+
"id": "67b8be5c",
|
| 127 |
+
"metadata": {},
|
| 128 |
+
"outputs": [],
|
| 129 |
+
"source": [
|
| 130 |
+
"class DataIngestion:\n",
|
| 131 |
+
" def __init__(self, config: DataIngestionConfig):\n",
|
| 132 |
+
" self.config = config\n",
|
| 133 |
+
" \n",
|
| 134 |
+
" \n",
|
| 135 |
+
" def download_data(self):\n",
|
| 136 |
+
" if not os.path.exists(self.config.local_data_file):\n",
|
| 137 |
+
" filename, headers = request.urlretrieve(\n",
|
| 138 |
+
" url = self.config.source_URL,\n",
|
| 139 |
+
" filename = self.config.local_data_file\n",
|
| 140 |
+
" )\n",
|
| 141 |
+
" logger.info(f\"{filename} download! with following info : \\n{headers}\")\n",
|
| 142 |
+
" else:\n",
|
| 143 |
+
" logger.info(f\"File already exists of size : {get_size(Path(self.config.local_data_file))}\")\n",
|
| 144 |
+
" \n",
|
| 145 |
+
" def extract_zip_file(self):\n",
|
| 146 |
+
" \"\"\"\n",
|
| 147 |
+
" zip_file_path : str\n",
|
| 148 |
+
" EXtracts the zip file to the given directory.\n",
|
| 149 |
+
" Functions returns None.\n",
|
| 150 |
+
" \"\"\"\n",
|
| 151 |
+
" unzip_path = self.config.unzip_dir\n",
|
| 152 |
+
" os.makedirs(unzip_path, exist_ok=True)\n",
|
| 153 |
+
" with zipfile.ZipFile(self.config.local_data_file, 'r') as zip_ref:\n",
|
| 154 |
+
" zip_ref.extractall(unzip_path)\n",
|
| 155 |
+
" "
|
| 156 |
+
]
|
| 157 |
+
},
|
| 158 |
+
{
|
| 159 |
+
"cell_type": "code",
|
| 160 |
+
"execution_count": 9,
|
| 161 |
+
"id": "369293bc",
|
| 162 |
+
"metadata": {},
|
| 163 |
+
"outputs": [
|
| 164 |
+
{
|
| 165 |
+
"name": "stdout",
|
| 166 |
+
"output_type": "stream",
|
| 167 |
+
"text": [
|
| 168 |
+
"[2025-06-28 23:11:14,985: INFO: common : yaml. file: config\\config.yaml loaded successfully. ]\n",
|
| 169 |
+
"[2025-06-28 23:11:14,987: INFO: common : yaml. file: params.yaml loaded successfully. ]\n",
|
| 170 |
+
"[2025-06-28 23:11:14,988: INFO: common : created directory at path : artifacts_root ]\n",
|
| 171 |
+
"[2025-06-28 23:11:14,989: INFO: common : created directory at path : artifacts/data_ingestion ]\n",
|
| 172 |
+
"[2025-06-28 23:11:14,990: INFO: 2056919966 : File already exists of size : ~7718 KB ]\n"
|
| 173 |
+
]
|
| 174 |
+
}
|
| 175 |
+
],
|
| 176 |
+
"source": [
|
| 177 |
+
"try:\n",
|
| 178 |
+
" config = ConfigurationManager()\n",
|
| 179 |
+
" data_ingestion_config = config.get_data_ingestion_config()\n",
|
| 180 |
+
" data_ingestion = DataIngestion(data_ingestion_config)\n",
|
| 181 |
+
" data_ingestion.download_data()\n",
|
| 182 |
+
" data_ingestion.extract_zip_file()\n",
|
| 183 |
+
"except Exception as e:\n",
|
| 184 |
+
" raise e"
|
| 185 |
+
]
|
| 186 |
+
},
|
| 187 |
+
{
|
| 188 |
+
"cell_type": "code",
|
| 189 |
+
"execution_count": null,
|
| 190 |
+
"id": "0f2ce6e5",
|
| 191 |
+
"metadata": {},
|
| 192 |
+
"outputs": [],
|
| 193 |
+
"source": []
|
| 194 |
+
}
|
| 195 |
+
],
|
| 196 |
+
"metadata": {
|
| 197 |
+
"kernelspec": {
|
| 198 |
+
"display_name": "textSummarizer",
|
| 199 |
+
"language": "python",
|
| 200 |
+
"name": "python3"
|
| 201 |
+
},
|
| 202 |
+
"language_info": {
|
| 203 |
+
"codemirror_mode": {
|
| 204 |
+
"name": "ipython",
|
| 205 |
+
"version": 3
|
| 206 |
+
},
|
| 207 |
+
"file_extension": ".py",
|
| 208 |
+
"mimetype": "text/x-python",
|
| 209 |
+
"name": "python",
|
| 210 |
+
"nbconvert_exporter": "python",
|
| 211 |
+
"pygments_lexer": "ipython3",
|
| 212 |
+
"version": "3.11.13"
|
| 213 |
+
}
|
| 214 |
+
},
|
| 215 |
+
"nbformat": 4,
|
| 216 |
+
"nbformat_minor": 5
|
| 217 |
+
}
|
research/02_data_validation.ipynb
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "3ef222e5",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import os"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": 3,
|
| 16 |
+
"id": "186dcb71",
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"%pwd\n",
|
| 21 |
+
"os.chdir('../')"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": 4,
|
| 27 |
+
"id": "3f4eb483",
|
| 28 |
+
"metadata": {},
|
| 29 |
+
"outputs": [
|
| 30 |
+
{
|
| 31 |
+
"data": {
|
| 32 |
+
"text/plain": [
|
| 33 |
+
"'c:\\\\order\\\\Desktop\\\\AI_Summarizer'"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
"execution_count": 4,
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"output_type": "execute_result"
|
| 39 |
+
}
|
| 40 |
+
],
|
| 41 |
+
"source": [
|
| 42 |
+
"%pwd"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": 10,
|
| 48 |
+
"id": "b36d364e",
|
| 49 |
+
"metadata": {},
|
| 50 |
+
"outputs": [],
|
| 51 |
+
"source": [
|
| 52 |
+
"from dataclasses import dataclass\n",
|
| 53 |
+
"from pathlib import Path\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"@dataclass(frozen=True)\n",
|
| 56 |
+
"class DataValidationConfig:\n",
|
| 57 |
+
" root_dir : Path\n",
|
| 58 |
+
" STATUS_FILE : str\n",
|
| 59 |
+
" ALL_REQUIRED_FILES : list"
|
| 60 |
+
]
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"cell_type": "code",
|
| 64 |
+
"execution_count": 11,
|
| 65 |
+
"id": "05be5b22",
|
| 66 |
+
"metadata": {},
|
| 67 |
+
"outputs": [],
|
| 68 |
+
"source": [
|
| 69 |
+
"from textSummarizer.constants import *\n",
|
| 70 |
+
"from textSummarizer.utils.common import read_yaml, create_directories"
|
| 71 |
+
]
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
"cell_type": "code",
|
| 75 |
+
"execution_count": 12,
|
| 76 |
+
"id": "5d9f3fbe",
|
| 77 |
+
"metadata": {},
|
| 78 |
+
"outputs": [],
|
| 79 |
+
"source": [
|
| 80 |
+
"class ConfigurationManager:\n",
|
| 81 |
+
" def __init__(\n",
|
| 82 |
+
" self,\n",
|
| 83 |
+
" config_filepath: str = CONFIG_FILE_PATH,\n",
|
| 84 |
+
" params_filepath: str = PARAMS_FILE_PATH):\n",
|
| 85 |
+
" \n",
|
| 86 |
+
" self.config = read_yaml(config_filepath)\n",
|
| 87 |
+
" self.params = read_yaml(params_filepath)\n",
|
| 88 |
+
" \n",
|
| 89 |
+
" create_directories([self.config.artifacts_root])\n",
|
| 90 |
+
" \n",
|
| 91 |
+
" \n",
|
| 92 |
+
" def get_data_validation_config(self) -> DataValidationConfig:\n",
|
| 93 |
+
" config = self.config.data_validation\n",
|
| 94 |
+
" \n",
|
| 95 |
+
" create_directories([config.root_dir])\n",
|
| 96 |
+
" \n",
|
| 97 |
+
" data_validation_config = DataValidationConfig(\n",
|
| 98 |
+
" root_dir=config.root_dir,\n",
|
| 99 |
+
" STATUS_FILE=config.STATUS_FILE,\n",
|
| 100 |
+
" ALL_REQUIRED_FILES=config.ALL_REQUIRED_FILES,\n",
|
| 101 |
+
" )\n",
|
| 102 |
+
" \n",
|
| 103 |
+
" return data_validation_config\n",
|
| 104 |
+
" "
|
| 105 |
+
]
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"cell_type": "code",
|
| 109 |
+
"execution_count": 9,
|
| 110 |
+
"id": "7cca78e6",
|
| 111 |
+
"metadata": {},
|
| 112 |
+
"outputs": [],
|
| 113 |
+
"source": [
|
| 114 |
+
"import os\n",
|
| 115 |
+
"from textSummarizer.logging import logger"
|
| 116 |
+
]
|
| 117 |
+
},
|
| 118 |
+
{
|
| 119 |
+
"cell_type": "code",
|
| 120 |
+
"execution_count": 13,
|
| 121 |
+
"id": "6cd0042a",
|
| 122 |
+
"metadata": {},
|
| 123 |
+
"outputs": [],
|
| 124 |
+
"source": [
|
| 125 |
+
"class DataValidation:\n",
|
| 126 |
+
" def __init__(self, config: DataValidationConfig):\n",
|
| 127 |
+
" self.config = config\n",
|
| 128 |
+
" \n",
|
| 129 |
+
" \n",
|
| 130 |
+
" \n",
|
| 131 |
+
" def validate_all_files_exists(self) -> bool:\n",
|
| 132 |
+
" try:\n",
|
| 133 |
+
" validation_status = None\n",
|
| 134 |
+
" \n",
|
| 135 |
+
" all_files = os.listdir(os.path.join(\"artifacts\" , \"data_ingestion\" , \"samsum_dataset\"))\n",
|
| 136 |
+
" \n",
|
| 137 |
+
" for file in all_files:\n",
|
| 138 |
+
" if file not in self.config.ALL_REQUIRED_FILES:\n",
|
| 139 |
+
" validation_status = False\n",
|
| 140 |
+
" with open(self.config.STATUS_FILE, 'w') as f:\n",
|
| 141 |
+
" f.write(f\"Validation status: {validation_status}\")\n",
|
| 142 |
+
" else:\n",
|
| 143 |
+
" validation_status = True\n",
|
| 144 |
+
" with open(self.config.STATUS_FILE, 'w') as f:\n",
|
| 145 |
+
" f.write(f\"Validation status: {validation_status}\")\n",
|
| 146 |
+
" \n",
|
| 147 |
+
" return validation_status\n",
|
| 148 |
+
" \n",
|
| 149 |
+
" except Exception as e:\n",
|
| 150 |
+
" raise e"
|
| 151 |
+
]
|
| 152 |
+
},
|
| 153 |
+
{
|
| 154 |
+
"cell_type": "code",
|
| 155 |
+
"execution_count": 14,
|
| 156 |
+
"id": "38fa96fd",
|
| 157 |
+
"metadata": {},
|
| 158 |
+
"outputs": [
|
| 159 |
+
{
|
| 160 |
+
"name": "stdout",
|
| 161 |
+
"output_type": "stream",
|
| 162 |
+
"text": [
|
| 163 |
+
"[2025-06-26 05:05:39,842: INFO: common : yaml. file: config\\config.yaml loaded successfully. ]\n",
|
| 164 |
+
"[2025-06-26 05:05:39,847: INFO: common : yaml. file: params.yaml loaded successfully. ]\n",
|
| 165 |
+
"[2025-06-26 05:05:39,848: INFO: common : created directory at path : artifacts_root ]\n",
|
| 166 |
+
"[2025-06-26 05:05:39,849: INFO: common : created directory at path : artifacts/data_validation ]\n"
|
| 167 |
+
]
|
| 168 |
+
}
|
| 169 |
+
],
|
| 170 |
+
"source": [
|
| 171 |
+
"try:\n",
|
| 172 |
+
" config = ConfigurationManager()\n",
|
| 173 |
+
" data_validation_config = config.get_data_validation_config()\n",
|
| 174 |
+
" data_validation = DataValidation(data_validation_config)\n",
|
| 175 |
+
" data_validation.validate_all_files_exists()\n",
|
| 176 |
+
"except Exception as e:\n",
|
| 177 |
+
" raise e"
|
| 178 |
+
]
|
| 179 |
+
}
|
| 180 |
+
],
|
| 181 |
+
"metadata": {
|
| 182 |
+
"kernelspec": {
|
| 183 |
+
"display_name": "textSummarizer",
|
| 184 |
+
"language": "python",
|
| 185 |
+
"name": "python3"
|
| 186 |
+
},
|
| 187 |
+
"language_info": {
|
| 188 |
+
"codemirror_mode": {
|
| 189 |
+
"name": "ipython",
|
| 190 |
+
"version": 3
|
| 191 |
+
},
|
| 192 |
+
"file_extension": ".py",
|
| 193 |
+
"mimetype": "text/x-python",
|
| 194 |
+
"name": "python",
|
| 195 |
+
"nbconvert_exporter": "python",
|
| 196 |
+
"pygments_lexer": "ipython3",
|
| 197 |
+
"version": "3.11.13"
|
| 198 |
+
}
|
| 199 |
+
},
|
| 200 |
+
"nbformat": 4,
|
| 201 |
+
"nbformat_minor": 5
|
| 202 |
+
}
|
research/03_data_transformation.ipynb
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "aef5ef85",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import os"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": 2,
|
| 16 |
+
"id": "13cc3394",
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"%pwd\n",
|
| 21 |
+
"os.chdir(\"../\")"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": 3,
|
| 27 |
+
"id": "14c37e99",
|
| 28 |
+
"metadata": {},
|
| 29 |
+
"outputs": [
|
| 30 |
+
{
|
| 31 |
+
"data": {
|
| 32 |
+
"text/plain": [
|
| 33 |
+
"'c:\\\\order\\\\Desktop\\\\AI_Summarizer'"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
"execution_count": 3,
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"output_type": "execute_result"
|
| 39 |
+
}
|
| 40 |
+
],
|
| 41 |
+
"source": [
|
| 42 |
+
"%pwd"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": 4,
|
| 48 |
+
"id": "f421926f",
|
| 49 |
+
"metadata": {},
|
| 50 |
+
"outputs": [],
|
| 51 |
+
"source": [
|
| 52 |
+
"from dataclasses import dataclass\n",
|
| 53 |
+
"from pathlib import Path\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"@dataclass(frozen=True)\n",
|
| 56 |
+
"class DataTransformationConfig:\n",
|
| 57 |
+
" root_dir: Path\n",
|
| 58 |
+
" data_path: Path\n",
|
| 59 |
+
" tokenizer_name: Path"
|
| 60 |
+
]
|
| 61 |
+
},
|
| 62 |
+
{
|
| 63 |
+
"cell_type": "code",
|
| 64 |
+
"execution_count": 5,
|
| 65 |
+
"id": "0731278f",
|
| 66 |
+
"metadata": {},
|
| 67 |
+
"outputs": [],
|
| 68 |
+
"source": [
|
| 69 |
+
"from textSummarizer.constants import *\n",
|
| 70 |
+
"from textSummarizer.utils.common import read_yaml, create_directories"
|
| 71 |
+
]
|
| 72 |
+
},
|
| 73 |
+
{
|
| 74 |
+
"cell_type": "code",
|
| 75 |
+
"execution_count": 6,
|
| 76 |
+
"id": "f2a0eff4",
|
| 77 |
+
"metadata": {},
|
| 78 |
+
"outputs": [],
|
| 79 |
+
"source": [
|
| 80 |
+
"class ConfigurationManager:\n",
|
| 81 |
+
" def __init__(\n",
|
| 82 |
+
" self,\n",
|
| 83 |
+
" config_filepath: str = CONFIG_FILE_PATH,\n",
|
| 84 |
+
" params_filepath: str = PARAMS_FILE_PATH):\n",
|
| 85 |
+
" \n",
|
| 86 |
+
" self.config = read_yaml(config_filepath)\n",
|
| 87 |
+
" self.params = read_yaml(params_filepath)\n",
|
| 88 |
+
" \n",
|
| 89 |
+
" create_directories([self.config.artifacts_root])\n",
|
| 90 |
+
" \n",
|
| 91 |
+
" def get_data_transformation_config(self) -> DataTransformationConfig:\n",
|
| 92 |
+
" config= self.config.data_transformation\n",
|
| 93 |
+
" \n",
|
| 94 |
+
" create_directories([config.root_dir])\n",
|
| 95 |
+
" \n",
|
| 96 |
+
" data_transformation_config = DataTransformationConfig(\n",
|
| 97 |
+
" root_dir=config.root_dir,\n",
|
| 98 |
+
" data_path=config.data_path,\n",
|
| 99 |
+
" tokenizer_name=config.tokenizer_name,\n",
|
| 100 |
+
" )\n",
|
| 101 |
+
" \n",
|
| 102 |
+
" return data_transformation_config"
|
| 103 |
+
]
|
| 104 |
+
},
|
| 105 |
+
{
|
| 106 |
+
"cell_type": "code",
|
| 107 |
+
"execution_count": 7,
|
| 108 |
+
"id": "adad3d61",
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"outputs": [
|
| 111 |
+
{
|
| 112 |
+
"name": "stderr",
|
| 113 |
+
"output_type": "stream",
|
| 114 |
+
"text": [
|
| 115 |
+
"c:\\Users\\jeeva\\.conda\\envs\\textSummarizer\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 116 |
+
" from .autonotebook import tqdm as notebook_tqdm\n",
|
| 117 |
+
"None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.\n"
|
| 118 |
+
]
|
| 119 |
+
}
|
| 120 |
+
],
|
| 121 |
+
"source": [
|
| 122 |
+
"import os\n",
|
| 123 |
+
"from textSummarizer.logging import logger\n",
|
| 124 |
+
"from transformers import AutoTokenizer\n",
|
| 125 |
+
"from datasets import load_dataset, load_from_disk"
|
| 126 |
+
]
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
"cell_type": "code",
|
| 130 |
+
"execution_count": 8,
|
| 131 |
+
"id": "7369d543",
|
| 132 |
+
"metadata": {},
|
| 133 |
+
"outputs": [],
|
| 134 |
+
"source": [
|
| 135 |
+
"class DataTransformation:\n",
|
| 136 |
+
" def __init__(self, config: DataTransformationConfig):\n",
|
| 137 |
+
" self.config = config\n",
|
| 138 |
+
" self.tokenizer = AutoTokenizer.from_pretrained(self.config.tokenizer_name)\n",
|
| 139 |
+
" \n",
|
| 140 |
+
" \n",
|
| 141 |
+
" def convert_examples_to_features(self, example_batch):\n",
|
| 142 |
+
" input_encodings = self.tokenizer(example_batch['dialogue'] , max_length=1024, truncation=True)\n",
|
| 143 |
+
" \n",
|
| 144 |
+
" with self.tokenizer.as_target_tokenizer():\n",
|
| 145 |
+
" target_encodings = self.tokenizer(example_batch['summary'], max_length=128, truncation=True)\n",
|
| 146 |
+
" \n",
|
| 147 |
+
" return {\n",
|
| 148 |
+
" 'input_ids': input_encodings['input_ids'],\n",
|
| 149 |
+
" 'attention_mask': input_encodings['attention_mask'],\n",
|
| 150 |
+
" 'labels': target_encodings['input_ids'],\n",
|
| 151 |
+
" }\n",
|
| 152 |
+
" \n",
|
| 153 |
+
" \n",
|
| 154 |
+
" def convert(self):\n",
|
| 155 |
+
" dataset_samsum = load_from_disk(self.config.data_path)\n",
|
| 156 |
+
" dataset_samsum_pt = dataset_samsum.map(self.convert_examples_to_features, batched=True)\n",
|
| 157 |
+
" dataset_samsum_pt.save_to_disk(os.path.join(self.config.root_dir, \"samsum_dataset\"))\n",
|
| 158 |
+
" "
|
| 159 |
+
]
|
| 160 |
+
},
|
| 161 |
+
{
|
| 162 |
+
"cell_type": "code",
|
| 163 |
+
"execution_count": 9,
|
| 164 |
+
"id": "78cb118a",
|
| 165 |
+
"metadata": {},
|
| 166 |
+
"outputs": [
|
| 167 |
+
{
|
| 168 |
+
"name": "stdout",
|
| 169 |
+
"output_type": "stream",
|
| 170 |
+
"text": [
|
| 171 |
+
"[2025-06-26 22:06:59,994: INFO: common : yaml. file: config\\config.yaml loaded successfully. ]\n",
|
| 172 |
+
"[2025-06-26 22:06:59,995: INFO: common : yaml. file: params.yaml loaded successfully. ]\n",
|
| 173 |
+
"[2025-06-26 22:06:59,997: INFO: common : created directory at path : artifacts_root ]\n",
|
| 174 |
+
"[2025-06-26 22:06:59,997: INFO: common : created directory at path : artifacts/data_transformation ]\n"
|
| 175 |
+
]
|
| 176 |
+
},
|
| 177 |
+
{
|
| 178 |
+
"name": "stderr",
|
| 179 |
+
"output_type": "stream",
|
| 180 |
+
"text": [
|
| 181 |
+
"Map: 0%| | 0/14732 [00:00<?, ? examples/s]c:\\Users\\jeeva\\.conda\\envs\\textSummarizer\\Lib\\site-packages\\transformers\\tokenization_utils_base.py:3951: UserWarning: `as_target_tokenizer` is deprecated and will be removed in v5 of Transformers. You can tokenize your labels by using the argument `text_target` of the regular `__call__` method (either in the same call as your input texts if you use the same keyword arguments, or in a separate call.\n",
|
| 182 |
+
" warnings.warn(\n",
|
| 183 |
+
"Map: 100%|██████████| 14732/14732 [00:01<00:00, 8932.08 examples/s]\n",
|
| 184 |
+
"Map: 100%|██████████| 819/819 [00:00<00:00, 9059.66 examples/s]\n",
|
| 185 |
+
"Map: 100%|██████████| 818/818 [00:00<00:00, 9570.16 examples/s]\n",
|
| 186 |
+
"Saving the dataset (1/1 shards): 100%|██████████| 14732/14732 [00:00<00:00, 669372.95 examples/s]\n",
|
| 187 |
+
"Saving the dataset (1/1 shards): 100%|██████████| 819/819 [00:00<00:00, 180853.69 examples/s]\n",
|
| 188 |
+
"Saving the dataset (1/1 shards): 100%|██████████| 818/818 [00:00<00:00, 136245.76 examples/s]\n"
|
| 189 |
+
]
|
| 190 |
+
}
|
| 191 |
+
],
|
| 192 |
+
"source": [
|
| 193 |
+
"try:\n",
|
| 194 |
+
" config = ConfigurationManager()\n",
|
| 195 |
+
" data_transformation_config = config.get_data_transformation_config()\n",
|
| 196 |
+
" data_transformation = DataTransformation(config=data_transformation_config)\n",
|
| 197 |
+
" data_transformation.convert()\n",
|
| 198 |
+
"except Exception as e:\n",
|
| 199 |
+
" raise e"
|
| 200 |
+
]
|
| 201 |
+
}
|
| 202 |
+
],
|
| 203 |
+
"metadata": {
|
| 204 |
+
"kernelspec": {
|
| 205 |
+
"display_name": "textSummarizer",
|
| 206 |
+
"language": "python",
|
| 207 |
+
"name": "python3"
|
| 208 |
+
},
|
| 209 |
+
"language_info": {
|
| 210 |
+
"codemirror_mode": {
|
| 211 |
+
"name": "ipython",
|
| 212 |
+
"version": 3
|
| 213 |
+
},
|
| 214 |
+
"file_extension": ".py",
|
| 215 |
+
"mimetype": "text/x-python",
|
| 216 |
+
"name": "python",
|
| 217 |
+
"nbconvert_exporter": "python",
|
| 218 |
+
"pygments_lexer": "ipython3",
|
| 219 |
+
"version": "3.11.13"
|
| 220 |
+
}
|
| 221 |
+
},
|
| 222 |
+
"nbformat": 4,
|
| 223 |
+
"nbformat_minor": 5
|
| 224 |
+
}
|
research/04_model_trainer.ipynb
ADDED
|
@@ -0,0 +1,642 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "83ad55ca",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import os"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": 2,
|
| 16 |
+
"id": "e692ac4e",
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"%pwd\n",
|
| 21 |
+
"os.chdir(\"../\")"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": 3,
|
| 27 |
+
"id": "7c1a28ab",
|
| 28 |
+
"metadata": {},
|
| 29 |
+
"outputs": [
|
| 30 |
+
{
|
| 31 |
+
"data": {
|
| 32 |
+
"text/plain": [
|
| 33 |
+
"'c:\\\\order\\\\Desktop\\\\AI_Summarizer'"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
"execution_count": 3,
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"output_type": "execute_result"
|
| 39 |
+
}
|
| 40 |
+
],
|
| 41 |
+
"source": [
|
| 42 |
+
"%pwd"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": 4,
|
| 48 |
+
"id": "2303b7df",
|
| 49 |
+
"metadata": {},
|
| 50 |
+
"outputs": [],
|
| 51 |
+
"source": [
|
| 52 |
+
"from dataclasses import dataclass\n",
|
| 53 |
+
"from pathlib import Path"
|
| 54 |
+
]
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"cell_type": "code",
|
| 58 |
+
"execution_count": 5,
|
| 59 |
+
"id": "4319d974",
|
| 60 |
+
"metadata": {},
|
| 61 |
+
"outputs": [],
|
| 62 |
+
"source": [
|
| 63 |
+
"@dataclass(frozen=True)\n",
|
| 64 |
+
"class ModelTrainerConfig:\n",
|
| 65 |
+
" root_dir: Path\n",
|
| 66 |
+
" data_path: Path\n",
|
| 67 |
+
" model_ckpt: Path\n",
|
| 68 |
+
" num_train_epochs: int\n",
|
| 69 |
+
" warmup_steps: int\n",
|
| 70 |
+
" per_device_train_batch_size: int\n",
|
| 71 |
+
" weight_decay: float\n",
|
| 72 |
+
" logging_steps: int\n",
|
| 73 |
+
" evaluation_strategy: str\n",
|
| 74 |
+
" eval_steps: int\n",
|
| 75 |
+
" save_steps: float\n",
|
| 76 |
+
" gradient_accumulation_steps: int "
|
| 77 |
+
]
|
| 78 |
+
},
|
| 79 |
+
{
|
| 80 |
+
"cell_type": "code",
|
| 81 |
+
"execution_count": 6,
|
| 82 |
+
"id": "dfd0b798",
|
| 83 |
+
"metadata": {},
|
| 84 |
+
"outputs": [],
|
| 85 |
+
"source": [
|
| 86 |
+
"from textSummarizer.constants import *\n",
|
| 87 |
+
"from textSummarizer.utils.common import read_yaml, create_directories"
|
| 88 |
+
]
|
| 89 |
+
},
|
| 90 |
+
{
|
| 91 |
+
"cell_type": "code",
|
| 92 |
+
"execution_count": 7,
|
| 93 |
+
"id": "2b9b5473",
|
| 94 |
+
"metadata": {},
|
| 95 |
+
"outputs": [],
|
| 96 |
+
"source": [
|
| 97 |
+
"class ConfigurationManager:\n",
|
| 98 |
+
" def __init__(\n",
|
| 99 |
+
" self,\n",
|
| 100 |
+
" config_filepath = CONFIG_FILE_PATH,\n",
|
| 101 |
+
" params_filepath = PARAMS_FILE_PATH):\n",
|
| 102 |
+
" \n",
|
| 103 |
+
" self.config = read_yaml(config_filepath)\n",
|
| 104 |
+
" self.params = read_yaml(params_filepath)\n",
|
| 105 |
+
" \n",
|
| 106 |
+
" create_directories([self.config.artifacts_root])\n",
|
| 107 |
+
" \n",
|
| 108 |
+
" def get_model_trainer_config(self) -> ModelTrainerConfig:\n",
|
| 109 |
+
" config = self.config.model_trainer\n",
|
| 110 |
+
" params = self.params.TrainingArguments\n",
|
| 111 |
+
" \n",
|
| 112 |
+
" create_directories([config.root_dir])\n",
|
| 113 |
+
" \n",
|
| 114 |
+
" model_trainer_config = ModelTrainerConfig(\n",
|
| 115 |
+
" root_dir=config.root_dir,\n",
|
| 116 |
+
" data_path=config.data_path,\n",
|
| 117 |
+
" model_ckpt=config.model_ckpt,\n",
|
| 118 |
+
" num_train_epochs=params.num_train_epochs,\n",
|
| 119 |
+
" warmup_steps=params.warmup_steps,\n",
|
| 120 |
+
" per_device_train_batch_size=params.per_device_train_batch_size,\n",
|
| 121 |
+
" weight_decay=params.weight_decay,\n",
|
| 122 |
+
" logging_steps=params.logging_steps,\n",
|
| 123 |
+
" evaluation_strategy=params.evaluation_strategy,\n",
|
| 124 |
+
" eval_steps=params.evaluation_strategy,\n",
|
| 125 |
+
" save_steps=params.save_steps,\n",
|
| 126 |
+
" gradient_accumulation_steps=params.gradient_accumulation_steps\n",
|
| 127 |
+
" )\n",
|
| 128 |
+
" \n",
|
| 129 |
+
" return model_trainer_config"
|
| 130 |
+
]
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"cell_type": "code",
|
| 134 |
+
"execution_count": null,
|
| 135 |
+
"id": "4f5f9714",
|
| 136 |
+
"metadata": {},
|
| 137 |
+
"outputs": [
|
| 138 |
+
{
|
| 139 |
+
"name": "stderr",
|
| 140 |
+
"output_type": "stream",
|
| 141 |
+
"text": [
|
| 142 |
+
"c:\\Users\\jeeva\\.conda\\envs\\textSummarizer\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 143 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 144 |
+
]
|
| 145 |
+
},
|
| 146 |
+
{
|
| 147 |
+
"name": "stdout",
|
| 148 |
+
"output_type": "stream",
|
| 149 |
+
"text": [
|
| 150 |
+
"[2025-06-29 01:03:26,836: INFO: config : PyTorch version 2.5.1+cu121 available. ]\n",
|
| 151 |
+
"2.5.1+cu121\n",
|
| 152 |
+
"True\n"
|
| 153 |
+
]
|
| 154 |
+
}
|
| 155 |
+
],
|
| 156 |
+
"source": [
|
| 157 |
+
"from transformers import TrainingArguments, Trainer\n",
|
| 158 |
+
"from transformers import DataCollatorForSeq2Seq\n",
|
| 159 |
+
"from transformers import AutoModelForSeq2SeqLM, AutoTokenizer\n",
|
| 160 |
+
"from datasets import load_dataset, load_from_disk\n",
|
| 161 |
+
"import torch "
|
| 162 |
+
]
|
| 163 |
+
},
|
| 164 |
+
{
|
| 165 |
+
"cell_type": "code",
|
| 166 |
+
"execution_count": 9,
|
| 167 |
+
"id": "dda95b61",
|
| 168 |
+
"metadata": {},
|
| 169 |
+
"outputs": [],
|
| 170 |
+
"source": [
|
| 171 |
+
"class ModelTrainer:\n",
|
| 172 |
+
" def __init__(self, config: ModelTrainerConfig):\n",
|
| 173 |
+
" self.config = config\n",
|
| 174 |
+
" \n",
|
| 175 |
+
" \n",
|
| 176 |
+
" def train(self):\n",
|
| 177 |
+
" device = \"cuda\" if torch.cuda.is_available() else \"cpu\"\n",
|
| 178 |
+
" tokenizer = AutoTokenizer.from_pretrained(self.config.model_ckpt)\n",
|
| 179 |
+
" model_pegasus = AutoModelForSeq2SeqLM.from_pretrained(self.config.model_ckpt, use_safetensors=True).to(device)\n",
|
| 180 |
+
" seq2seq_data_collator = DataCollatorForSeq2Seq(tokenizer, model=model_pegasus)\n",
|
| 181 |
+
" \n",
|
| 182 |
+
" dataset_samsum_pt = load_from_disk(self.config.data_path)\n",
|
| 183 |
+
" \n",
|
| 184 |
+
" # trainer_args = TrainingArguments(\n",
|
| 185 |
+
" # output_dir=self.config.root_dir,\n",
|
| 186 |
+
" # num_train_epochs=self.config.num_train_epochs,\n",
|
| 187 |
+
" # warmup_steps=self.config.warmup_steps,\n",
|
| 188 |
+
" # per_device_train_batch_size=self.config.per_device_train_batch_size,\n",
|
| 189 |
+
" # per_device_eval_batch_size=self.config.per_device_train_batch_size,\n",
|
| 190 |
+
" # weight_decay=self.config.weight_decay,\n",
|
| 191 |
+
" # logging_steps=self.config.logging_steps,\n",
|
| 192 |
+
" # evaluation_strategy=self.config.evaluation_strategy,\n",
|
| 193 |
+
" # eval_steps=self.config.eval_steps,\n",
|
| 194 |
+
" # save_steps=1e6,\n",
|
| 195 |
+
" # gradient_accumulation_steps=self.config.gradient_accumulation_steps\n",
|
| 196 |
+
" # )\n",
|
| 197 |
+
" \n",
|
| 198 |
+
" \n",
|
| 199 |
+
" trainer_args = TrainingArguments(\n",
|
| 200 |
+
" output_dir=self.config.root_dir,\n",
|
| 201 |
+
" num_train_epochs=1,\n",
|
| 202 |
+
" warmup_steps=500,\n",
|
| 203 |
+
" per_device_train_batch_size=1,\n",
|
| 204 |
+
" per_device_eval_batch_size=1,\n",
|
| 205 |
+
" weight_decay=0.01,\n",
|
| 206 |
+
" logging_steps=10\n",
|
| 207 |
+
" )\n",
|
| 208 |
+
"\n",
|
| 209 |
+
" \n",
|
| 210 |
+
" \n",
|
| 211 |
+
" trainer = Trainer(model=model_pegasus, args=trainer_args,\n",
|
| 212 |
+
" tokenizer=tokenizer, data_collator=seq2seq_data_collator,\n",
|
| 213 |
+
" train_dataset=dataset_samsum_pt[\"test\"],\n",
|
| 214 |
+
" eval_dataset=dataset_samsum_pt[\"validation\"])\n",
|
| 215 |
+
" \n",
|
| 216 |
+
" trainer.train()\n",
|
| 217 |
+
" \n",
|
| 218 |
+
" model_pegasus.save_pretrained(os.path.join(self.config.root_dir, \"pegasus-samsum-model\"))\n",
|
| 219 |
+
" \n",
|
| 220 |
+
" tokenizer.save_pretrained(os.path.join(self.config.root_dir, \"tokenizer\"))"
|
| 221 |
+
]
|
| 222 |
+
},
|
| 223 |
+
{
|
| 224 |
+
"cell_type": "code",
|
| 225 |
+
"execution_count": 10,
|
| 226 |
+
"id": "0eba62ba",
|
| 227 |
+
"metadata": {},
|
| 228 |
+
"outputs": [
|
| 229 |
+
{
|
| 230 |
+
"name": "stdout",
|
| 231 |
+
"output_type": "stream",
|
| 232 |
+
"text": [
|
| 233 |
+
"[2025-06-29 01:03:38,152: INFO: common : yaml. file: config\\config.yaml loaded successfully. ]\n",
|
| 234 |
+
"[2025-06-29 01:03:38,154: INFO: common : yaml. file: params.yaml loaded successfully. ]\n",
|
| 235 |
+
"[2025-06-29 01:03:38,155: INFO: common : created directory at path : artifacts_root ]\n",
|
| 236 |
+
"[2025-06-29 01:03:38,156: INFO: common : created directory at path : artifacts/model_trainer ]\n"
|
| 237 |
+
]
|
| 238 |
+
},
|
| 239 |
+
{
|
| 240 |
+
"name": "stderr",
|
| 241 |
+
"output_type": "stream",
|
| 242 |
+
"text": [
|
| 243 |
+
"Some weights of PegasusForConditionalGeneration were not initialized from the model checkpoint at google/pegasus-cnn_dailymail and are newly initialized: ['model.decoder.embed_positions.weight', 'model.encoder.embed_positions.weight']\n",
|
| 244 |
+
"You should probably TRAIN this model on a down-stream task to be able to use it for predictions and inference.\n",
|
| 245 |
+
"C:\\Users\\jeeva\\AppData\\Local\\Temp\\ipykernel_19788\\3518460819.py:41: FutureWarning: `tokenizer` is deprecated and will be removed in version 5.0.0 for `Trainer.__init__`. Use `processing_class` instead.\n",
|
| 246 |
+
" trainer = Trainer(model=model_pegasus, args=trainer_args,\n"
|
| 247 |
+
]
|
| 248 |
+
},
|
| 249 |
+
{
|
| 250 |
+
"data": {
|
| 251 |
+
"text/html": [
|
| 252 |
+
"\n",
|
| 253 |
+
" <div>\n",
|
| 254 |
+
" \n",
|
| 255 |
+
" <progress value='819' max='819' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
|
| 256 |
+
" [819/819 44:07, Epoch 1/1]\n",
|
| 257 |
+
" </div>\n",
|
| 258 |
+
" <table border=\"1\" class=\"dataframe\">\n",
|
| 259 |
+
" <thead>\n",
|
| 260 |
+
" <tr style=\"text-align: left;\">\n",
|
| 261 |
+
" <th>Step</th>\n",
|
| 262 |
+
" <th>Training Loss</th>\n",
|
| 263 |
+
" </tr>\n",
|
| 264 |
+
" </thead>\n",
|
| 265 |
+
" <tbody>\n",
|
| 266 |
+
" <tr>\n",
|
| 267 |
+
" <td>10</td>\n",
|
| 268 |
+
" <td>2.932800</td>\n",
|
| 269 |
+
" </tr>\n",
|
| 270 |
+
" <tr>\n",
|
| 271 |
+
" <td>20</td>\n",
|
| 272 |
+
" <td>3.312200</td>\n",
|
| 273 |
+
" </tr>\n",
|
| 274 |
+
" <tr>\n",
|
| 275 |
+
" <td>30</td>\n",
|
| 276 |
+
" <td>2.888900</td>\n",
|
| 277 |
+
" </tr>\n",
|
| 278 |
+
" <tr>\n",
|
| 279 |
+
" <td>40</td>\n",
|
| 280 |
+
" <td>2.671100</td>\n",
|
| 281 |
+
" </tr>\n",
|
| 282 |
+
" <tr>\n",
|
| 283 |
+
" <td>50</td>\n",
|
| 284 |
+
" <td>3.116900</td>\n",
|
| 285 |
+
" </tr>\n",
|
| 286 |
+
" <tr>\n",
|
| 287 |
+
" <td>60</td>\n",
|
| 288 |
+
" <td>2.466100</td>\n",
|
| 289 |
+
" </tr>\n",
|
| 290 |
+
" <tr>\n",
|
| 291 |
+
" <td>70</td>\n",
|
| 292 |
+
" <td>3.369200</td>\n",
|
| 293 |
+
" </tr>\n",
|
| 294 |
+
" <tr>\n",
|
| 295 |
+
" <td>80</td>\n",
|
| 296 |
+
" <td>3.053100</td>\n",
|
| 297 |
+
" </tr>\n",
|
| 298 |
+
" <tr>\n",
|
| 299 |
+
" <td>90</td>\n",
|
| 300 |
+
" <td>3.115300</td>\n",
|
| 301 |
+
" </tr>\n",
|
| 302 |
+
" <tr>\n",
|
| 303 |
+
" <td>100</td>\n",
|
| 304 |
+
" <td>2.541900</td>\n",
|
| 305 |
+
" </tr>\n",
|
| 306 |
+
" <tr>\n",
|
| 307 |
+
" <td>110</td>\n",
|
| 308 |
+
" <td>2.536800</td>\n",
|
| 309 |
+
" </tr>\n",
|
| 310 |
+
" <tr>\n",
|
| 311 |
+
" <td>120</td>\n",
|
| 312 |
+
" <td>2.558000</td>\n",
|
| 313 |
+
" </tr>\n",
|
| 314 |
+
" <tr>\n",
|
| 315 |
+
" <td>130</td>\n",
|
| 316 |
+
" <td>3.056200</td>\n",
|
| 317 |
+
" </tr>\n",
|
| 318 |
+
" <tr>\n",
|
| 319 |
+
" <td>140</td>\n",
|
| 320 |
+
" <td>2.083200</td>\n",
|
| 321 |
+
" </tr>\n",
|
| 322 |
+
" <tr>\n",
|
| 323 |
+
" <td>150</td>\n",
|
| 324 |
+
" <td>2.065100</td>\n",
|
| 325 |
+
" </tr>\n",
|
| 326 |
+
" <tr>\n",
|
| 327 |
+
" <td>160</td>\n",
|
| 328 |
+
" <td>2.338200</td>\n",
|
| 329 |
+
" </tr>\n",
|
| 330 |
+
" <tr>\n",
|
| 331 |
+
" <td>170</td>\n",
|
| 332 |
+
" <td>2.271100</td>\n",
|
| 333 |
+
" </tr>\n",
|
| 334 |
+
" <tr>\n",
|
| 335 |
+
" <td>180</td>\n",
|
| 336 |
+
" <td>2.053200</td>\n",
|
| 337 |
+
" </tr>\n",
|
| 338 |
+
" <tr>\n",
|
| 339 |
+
" <td>190</td>\n",
|
| 340 |
+
" <td>2.143000</td>\n",
|
| 341 |
+
" </tr>\n",
|
| 342 |
+
" <tr>\n",
|
| 343 |
+
" <td>200</td>\n",
|
| 344 |
+
" <td>1.947000</td>\n",
|
| 345 |
+
" </tr>\n",
|
| 346 |
+
" <tr>\n",
|
| 347 |
+
" <td>210</td>\n",
|
| 348 |
+
" <td>1.823900</td>\n",
|
| 349 |
+
" </tr>\n",
|
| 350 |
+
" <tr>\n",
|
| 351 |
+
" <td>220</td>\n",
|
| 352 |
+
" <td>2.339900</td>\n",
|
| 353 |
+
" </tr>\n",
|
| 354 |
+
" <tr>\n",
|
| 355 |
+
" <td>230</td>\n",
|
| 356 |
+
" <td>1.958000</td>\n",
|
| 357 |
+
" </tr>\n",
|
| 358 |
+
" <tr>\n",
|
| 359 |
+
" <td>240</td>\n",
|
| 360 |
+
" <td>2.743100</td>\n",
|
| 361 |
+
" </tr>\n",
|
| 362 |
+
" <tr>\n",
|
| 363 |
+
" <td>250</td>\n",
|
| 364 |
+
" <td>2.370100</td>\n",
|
| 365 |
+
" </tr>\n",
|
| 366 |
+
" <tr>\n",
|
| 367 |
+
" <td>260</td>\n",
|
| 368 |
+
" <td>1.970100</td>\n",
|
| 369 |
+
" </tr>\n",
|
| 370 |
+
" <tr>\n",
|
| 371 |
+
" <td>270</td>\n",
|
| 372 |
+
" <td>2.276200</td>\n",
|
| 373 |
+
" </tr>\n",
|
| 374 |
+
" <tr>\n",
|
| 375 |
+
" <td>280</td>\n",
|
| 376 |
+
" <td>2.156200</td>\n",
|
| 377 |
+
" </tr>\n",
|
| 378 |
+
" <tr>\n",
|
| 379 |
+
" <td>290</td>\n",
|
| 380 |
+
" <td>1.602900</td>\n",
|
| 381 |
+
" </tr>\n",
|
| 382 |
+
" <tr>\n",
|
| 383 |
+
" <td>300</td>\n",
|
| 384 |
+
" <td>2.405400</td>\n",
|
| 385 |
+
" </tr>\n",
|
| 386 |
+
" <tr>\n",
|
| 387 |
+
" <td>310</td>\n",
|
| 388 |
+
" <td>2.053100</td>\n",
|
| 389 |
+
" </tr>\n",
|
| 390 |
+
" <tr>\n",
|
| 391 |
+
" <td>320</td>\n",
|
| 392 |
+
" <td>2.130500</td>\n",
|
| 393 |
+
" </tr>\n",
|
| 394 |
+
" <tr>\n",
|
| 395 |
+
" <td>330</td>\n",
|
| 396 |
+
" <td>1.777200</td>\n",
|
| 397 |
+
" </tr>\n",
|
| 398 |
+
" <tr>\n",
|
| 399 |
+
" <td>340</td>\n",
|
| 400 |
+
" <td>2.240200</td>\n",
|
| 401 |
+
" </tr>\n",
|
| 402 |
+
" <tr>\n",
|
| 403 |
+
" <td>350</td>\n",
|
| 404 |
+
" <td>1.661200</td>\n",
|
| 405 |
+
" </tr>\n",
|
| 406 |
+
" <tr>\n",
|
| 407 |
+
" <td>360</td>\n",
|
| 408 |
+
" <td>1.875600</td>\n",
|
| 409 |
+
" </tr>\n",
|
| 410 |
+
" <tr>\n",
|
| 411 |
+
" <td>370</td>\n",
|
| 412 |
+
" <td>2.144000</td>\n",
|
| 413 |
+
" </tr>\n",
|
| 414 |
+
" <tr>\n",
|
| 415 |
+
" <td>380</td>\n",
|
| 416 |
+
" <td>2.140400</td>\n",
|
| 417 |
+
" </tr>\n",
|
| 418 |
+
" <tr>\n",
|
| 419 |
+
" <td>390</td>\n",
|
| 420 |
+
" <td>1.908100</td>\n",
|
| 421 |
+
" </tr>\n",
|
| 422 |
+
" <tr>\n",
|
| 423 |
+
" <td>400</td>\n",
|
| 424 |
+
" <td>1.828400</td>\n",
|
| 425 |
+
" </tr>\n",
|
| 426 |
+
" <tr>\n",
|
| 427 |
+
" <td>410</td>\n",
|
| 428 |
+
" <td>1.890400</td>\n",
|
| 429 |
+
" </tr>\n",
|
| 430 |
+
" <tr>\n",
|
| 431 |
+
" <td>420</td>\n",
|
| 432 |
+
" <td>1.912100</td>\n",
|
| 433 |
+
" </tr>\n",
|
| 434 |
+
" <tr>\n",
|
| 435 |
+
" <td>430</td>\n",
|
| 436 |
+
" <td>1.972800</td>\n",
|
| 437 |
+
" </tr>\n",
|
| 438 |
+
" <tr>\n",
|
| 439 |
+
" <td>440</td>\n",
|
| 440 |
+
" <td>1.950800</td>\n",
|
| 441 |
+
" </tr>\n",
|
| 442 |
+
" <tr>\n",
|
| 443 |
+
" <td>450</td>\n",
|
| 444 |
+
" <td>1.688000</td>\n",
|
| 445 |
+
" </tr>\n",
|
| 446 |
+
" <tr>\n",
|
| 447 |
+
" <td>460</td>\n",
|
| 448 |
+
" <td>2.048800</td>\n",
|
| 449 |
+
" </tr>\n",
|
| 450 |
+
" <tr>\n",
|
| 451 |
+
" <td>470</td>\n",
|
| 452 |
+
" <td>1.665500</td>\n",
|
| 453 |
+
" </tr>\n",
|
| 454 |
+
" <tr>\n",
|
| 455 |
+
" <td>480</td>\n",
|
| 456 |
+
" <td>1.779100</td>\n",
|
| 457 |
+
" </tr>\n",
|
| 458 |
+
" <tr>\n",
|
| 459 |
+
" <td>490</td>\n",
|
| 460 |
+
" <td>1.541600</td>\n",
|
| 461 |
+
" </tr>\n",
|
| 462 |
+
" <tr>\n",
|
| 463 |
+
" <td>500</td>\n",
|
| 464 |
+
" <td>2.011600</td>\n",
|
| 465 |
+
" </tr>\n",
|
| 466 |
+
" <tr>\n",
|
| 467 |
+
" <td>510</td>\n",
|
| 468 |
+
" <td>2.258400</td>\n",
|
| 469 |
+
" </tr>\n",
|
| 470 |
+
" <tr>\n",
|
| 471 |
+
" <td>520</td>\n",
|
| 472 |
+
" <td>1.945900</td>\n",
|
| 473 |
+
" </tr>\n",
|
| 474 |
+
" <tr>\n",
|
| 475 |
+
" <td>530</td>\n",
|
| 476 |
+
" <td>1.740100</td>\n",
|
| 477 |
+
" </tr>\n",
|
| 478 |
+
" <tr>\n",
|
| 479 |
+
" <td>540</td>\n",
|
| 480 |
+
" <td>1.871500</td>\n",
|
| 481 |
+
" </tr>\n",
|
| 482 |
+
" <tr>\n",
|
| 483 |
+
" <td>550</td>\n",
|
| 484 |
+
" <td>1.490400</td>\n",
|
| 485 |
+
" </tr>\n",
|
| 486 |
+
" <tr>\n",
|
| 487 |
+
" <td>560</td>\n",
|
| 488 |
+
" <td>2.026400</td>\n",
|
| 489 |
+
" </tr>\n",
|
| 490 |
+
" <tr>\n",
|
| 491 |
+
" <td>570</td>\n",
|
| 492 |
+
" <td>1.529800</td>\n",
|
| 493 |
+
" </tr>\n",
|
| 494 |
+
" <tr>\n",
|
| 495 |
+
" <td>580</td>\n",
|
| 496 |
+
" <td>2.545900</td>\n",
|
| 497 |
+
" </tr>\n",
|
| 498 |
+
" <tr>\n",
|
| 499 |
+
" <td>590</td>\n",
|
| 500 |
+
" <td>1.941400</td>\n",
|
| 501 |
+
" </tr>\n",
|
| 502 |
+
" <tr>\n",
|
| 503 |
+
" <td>600</td>\n",
|
| 504 |
+
" <td>1.714600</td>\n",
|
| 505 |
+
" </tr>\n",
|
| 506 |
+
" <tr>\n",
|
| 507 |
+
" <td>610</td>\n",
|
| 508 |
+
" <td>1.986300</td>\n",
|
| 509 |
+
" </tr>\n",
|
| 510 |
+
" <tr>\n",
|
| 511 |
+
" <td>620</td>\n",
|
| 512 |
+
" <td>1.499800</td>\n",
|
| 513 |
+
" </tr>\n",
|
| 514 |
+
" <tr>\n",
|
| 515 |
+
" <td>630</td>\n",
|
| 516 |
+
" <td>1.948500</td>\n",
|
| 517 |
+
" </tr>\n",
|
| 518 |
+
" <tr>\n",
|
| 519 |
+
" <td>640</td>\n",
|
| 520 |
+
" <td>1.451600</td>\n",
|
| 521 |
+
" </tr>\n",
|
| 522 |
+
" <tr>\n",
|
| 523 |
+
" <td>650</td>\n",
|
| 524 |
+
" <td>1.522000</td>\n",
|
| 525 |
+
" </tr>\n",
|
| 526 |
+
" <tr>\n",
|
| 527 |
+
" <td>660</td>\n",
|
| 528 |
+
" <td>1.699000</td>\n",
|
| 529 |
+
" </tr>\n",
|
| 530 |
+
" <tr>\n",
|
| 531 |
+
" <td>670</td>\n",
|
| 532 |
+
" <td>1.506800</td>\n",
|
| 533 |
+
" </tr>\n",
|
| 534 |
+
" <tr>\n",
|
| 535 |
+
" <td>680</td>\n",
|
| 536 |
+
" <td>1.235000</td>\n",
|
| 537 |
+
" </tr>\n",
|
| 538 |
+
" <tr>\n",
|
| 539 |
+
" <td>690</td>\n",
|
| 540 |
+
" <td>1.657100</td>\n",
|
| 541 |
+
" </tr>\n",
|
| 542 |
+
" <tr>\n",
|
| 543 |
+
" <td>700</td>\n",
|
| 544 |
+
" <td>1.871200</td>\n",
|
| 545 |
+
" </tr>\n",
|
| 546 |
+
" <tr>\n",
|
| 547 |
+
" <td>710</td>\n",
|
| 548 |
+
" <td>2.028800</td>\n",
|
| 549 |
+
" </tr>\n",
|
| 550 |
+
" <tr>\n",
|
| 551 |
+
" <td>720</td>\n",
|
| 552 |
+
" <td>1.872100</td>\n",
|
| 553 |
+
" </tr>\n",
|
| 554 |
+
" <tr>\n",
|
| 555 |
+
" <td>730</td>\n",
|
| 556 |
+
" <td>1.893200</td>\n",
|
| 557 |
+
" </tr>\n",
|
| 558 |
+
" <tr>\n",
|
| 559 |
+
" <td>740</td>\n",
|
| 560 |
+
" <td>1.719900</td>\n",
|
| 561 |
+
" </tr>\n",
|
| 562 |
+
" <tr>\n",
|
| 563 |
+
" <td>750</td>\n",
|
| 564 |
+
" <td>1.739000</td>\n",
|
| 565 |
+
" </tr>\n",
|
| 566 |
+
" <tr>\n",
|
| 567 |
+
" <td>760</td>\n",
|
| 568 |
+
" <td>1.705900</td>\n",
|
| 569 |
+
" </tr>\n",
|
| 570 |
+
" <tr>\n",
|
| 571 |
+
" <td>770</td>\n",
|
| 572 |
+
" <td>2.103700</td>\n",
|
| 573 |
+
" </tr>\n",
|
| 574 |
+
" <tr>\n",
|
| 575 |
+
" <td>780</td>\n",
|
| 576 |
+
" <td>1.915000</td>\n",
|
| 577 |
+
" </tr>\n",
|
| 578 |
+
" <tr>\n",
|
| 579 |
+
" <td>790</td>\n",
|
| 580 |
+
" <td>2.152000</td>\n",
|
| 581 |
+
" </tr>\n",
|
| 582 |
+
" <tr>\n",
|
| 583 |
+
" <td>800</td>\n",
|
| 584 |
+
" <td>1.793000</td>\n",
|
| 585 |
+
" </tr>\n",
|
| 586 |
+
" <tr>\n",
|
| 587 |
+
" <td>810</td>\n",
|
| 588 |
+
" <td>1.500600</td>\n",
|
| 589 |
+
" </tr>\n",
|
| 590 |
+
" </tbody>\n",
|
| 591 |
+
"</table><p>"
|
| 592 |
+
],
|
| 593 |
+
"text/plain": [
|
| 594 |
+
"<IPython.core.display.HTML object>"
|
| 595 |
+
]
|
| 596 |
+
},
|
| 597 |
+
"metadata": {},
|
| 598 |
+
"output_type": "display_data"
|
| 599 |
+
},
|
| 600 |
+
{
|
| 601 |
+
"name": "stderr",
|
| 602 |
+
"output_type": "stream",
|
| 603 |
+
"text": [
|
| 604 |
+
"c:\\Users\\jeeva\\.conda\\envs\\textSummarizer\\Lib\\site-packages\\transformers\\modeling_utils.py:3685: UserWarning: Moving the following attributes in the config to the generation config: {'max_length': 128, 'min_length': 32, 'num_beams': 8, 'length_penalty': 0.8}. You are seeing this warning because you've set generation parameters in the model config, as opposed to in the generation config.\n",
|
| 605 |
+
" warnings.warn(\n"
|
| 606 |
+
]
|
| 607 |
+
}
|
| 608 |
+
],
|
| 609 |
+
"source": [
|
| 610 |
+
"try:\n",
|
| 611 |
+
" config = ConfigurationManager()\n",
|
| 612 |
+
" model_trainer_config = config.get_model_trainer_config()\n",
|
| 613 |
+
" model_trainer_config = ModelTrainer(config=model_trainer_config)\n",
|
| 614 |
+
" model_trainer_config.train()\n",
|
| 615 |
+
"\n",
|
| 616 |
+
"except Exception as e:\n",
|
| 617 |
+
" raise e"
|
| 618 |
+
]
|
| 619 |
+
}
|
| 620 |
+
],
|
| 621 |
+
"metadata": {
|
| 622 |
+
"kernelspec": {
|
| 623 |
+
"display_name": "textSummarizer",
|
| 624 |
+
"language": "python",
|
| 625 |
+
"name": "python3"
|
| 626 |
+
},
|
| 627 |
+
"language_info": {
|
| 628 |
+
"codemirror_mode": {
|
| 629 |
+
"name": "ipython",
|
| 630 |
+
"version": 3
|
| 631 |
+
},
|
| 632 |
+
"file_extension": ".py",
|
| 633 |
+
"mimetype": "text/x-python",
|
| 634 |
+
"name": "python",
|
| 635 |
+
"nbconvert_exporter": "python",
|
| 636 |
+
"pygments_lexer": "ipython3",
|
| 637 |
+
"version": "3.11.13"
|
| 638 |
+
}
|
| 639 |
+
},
|
| 640 |
+
"nbformat": 4,
|
| 641 |
+
"nbformat_minor": 5
|
| 642 |
+
}
|
research/05_Model_evaluation.ipynb
ADDED
|
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "d83f232a",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"import os"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": 2,
|
| 16 |
+
"id": "7f3a1697",
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [],
|
| 19 |
+
"source": [
|
| 20 |
+
"%pwd\n",
|
| 21 |
+
"os.chdir(\"../\")"
|
| 22 |
+
]
|
| 23 |
+
},
|
| 24 |
+
{
|
| 25 |
+
"cell_type": "code",
|
| 26 |
+
"execution_count": 3,
|
| 27 |
+
"id": "70433f0a",
|
| 28 |
+
"metadata": {},
|
| 29 |
+
"outputs": [
|
| 30 |
+
{
|
| 31 |
+
"data": {
|
| 32 |
+
"text/plain": [
|
| 33 |
+
"'c:\\\\order\\\\Desktop\\\\AI_Summarizer'"
|
| 34 |
+
]
|
| 35 |
+
},
|
| 36 |
+
"execution_count": 3,
|
| 37 |
+
"metadata": {},
|
| 38 |
+
"output_type": "execute_result"
|
| 39 |
+
}
|
| 40 |
+
],
|
| 41 |
+
"source": [
|
| 42 |
+
"%pwd"
|
| 43 |
+
]
|
| 44 |
+
},
|
| 45 |
+
{
|
| 46 |
+
"cell_type": "code",
|
| 47 |
+
"execution_count": 4,
|
| 48 |
+
"id": "4b141e83",
|
| 49 |
+
"metadata": {},
|
| 50 |
+
"outputs": [],
|
| 51 |
+
"source": [
|
| 52 |
+
"from dataclasses import dataclass\n",
|
| 53 |
+
"from pathlib import Path"
|
| 54 |
+
]
|
| 55 |
+
},
|
| 56 |
+
{
|
| 57 |
+
"cell_type": "code",
|
| 58 |
+
"execution_count": 7,
|
| 59 |
+
"id": "ffb2ded1",
|
| 60 |
+
"metadata": {},
|
| 61 |
+
"outputs": [],
|
| 62 |
+
"source": [
|
| 63 |
+
"@dataclass(frozen=True)\n",
|
| 64 |
+
"class ModelEvaluationConfig:\n",
|
| 65 |
+
" root_dir: Path\n",
|
| 66 |
+
" data_path: Path\n",
|
| 67 |
+
" model_path: Path\n",
|
| 68 |
+
" tokenizer_path: Path\n",
|
| 69 |
+
" metric_file_name: Path"
|
| 70 |
+
]
|
| 71 |
+
},
|
| 72 |
+
{
|
| 73 |
+
"cell_type": "code",
|
| 74 |
+
"execution_count": 8,
|
| 75 |
+
"id": "4cbe6e7f",
|
| 76 |
+
"metadata": {},
|
| 77 |
+
"outputs": [],
|
| 78 |
+
"source": [
|
| 79 |
+
"from textSummarizer.constants import *\n",
|
| 80 |
+
"from textSummarizer.utils.common import read_yaml, create_directories"
|
| 81 |
+
]
|
| 82 |
+
},
|
| 83 |
+
{
|
| 84 |
+
"cell_type": "code",
|
| 85 |
+
"execution_count": 9,
|
| 86 |
+
"id": "6ef66517",
|
| 87 |
+
"metadata": {},
|
| 88 |
+
"outputs": [],
|
| 89 |
+
"source": [
|
| 90 |
+
"class ConfigurationManager:\n",
|
| 91 |
+
" def __init__(\n",
|
| 92 |
+
" self,\n",
|
| 93 |
+
" config_filepath = CONFIG_FILE_PATH,\n",
|
| 94 |
+
" params_filepath = PARAMS_FILE_PATH):\n",
|
| 95 |
+
" \n",
|
| 96 |
+
" self.config = read_yaml(config_filepath)\n",
|
| 97 |
+
" self.params = read_yaml(params_filepath)\n",
|
| 98 |
+
" \n",
|
| 99 |
+
" create_directories([self.config.artifacts_root])\n",
|
| 100 |
+
" \n",
|
| 101 |
+
" \n",
|
| 102 |
+
" def get_model_evaluation_config(self) -> ModelEvaluationConfig:\n",
|
| 103 |
+
" config = self.config.model_evaluation\n",
|
| 104 |
+
" \n",
|
| 105 |
+
" create_directories([config.root_dir])\n",
|
| 106 |
+
" \n",
|
| 107 |
+
" model_evaluation_config = ModelEvaluationConfig(\n",
|
| 108 |
+
" root_dir = config.root_dir,\n",
|
| 109 |
+
" data_path= config.data_path,\n",
|
| 110 |
+
" model_path = config.model_path,\n",
|
| 111 |
+
" tokenizer_path= config.tokenizer_path,\n",
|
| 112 |
+
" metric_file_name = config.metric_file_name\n",
|
| 113 |
+
" )\n",
|
| 114 |
+
" \n",
|
| 115 |
+
" return model_evaluation_config"
|
| 116 |
+
]
|
| 117 |
+
},
|
| 118 |
+
{
|
| 119 |
+
"cell_type": "code",
|
| 120 |
+
"execution_count": 10,
|
| 121 |
+
"id": "98b6adaf",
|
| 122 |
+
"metadata": {},
|
| 123 |
+
"outputs": [
|
| 124 |
+
{
|
| 125 |
+
"name": "stderr",
|
| 126 |
+
"output_type": "stream",
|
| 127 |
+
"text": [
|
| 128 |
+
"c:\\Users\\jeeva\\.conda\\envs\\textSummarizer\\Lib\\site-packages\\tqdm\\auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
|
| 129 |
+
" from .autonotebook import tqdm as notebook_tqdm\n"
|
| 130 |
+
]
|
| 131 |
+
},
|
| 132 |
+
{
|
| 133 |
+
"name": "stdout",
|
| 134 |
+
"output_type": "stream",
|
| 135 |
+
"text": [
|
| 136 |
+
"[2025-07-02 02:05:06,043: INFO: config : PyTorch version 2.5.1+cu121 available. ]\n"
|
| 137 |
+
]
|
| 138 |
+
}
|
| 139 |
+
],
|
| 140 |
+
"source": [
|
| 141 |
+
"from transformers import AutoModelForSeq2SeqLM, AutoTokenizer\n",
|
| 142 |
+
"from datasets import load_dataset, load_from_disk\n",
|
| 143 |
+
"import torch\n",
|
| 144 |
+
"import pandas as pd\n",
|
| 145 |
+
"from tqdm import tqdm"
|
| 146 |
+
]
|
| 147 |
+
},
|
| 148 |
+
{
|
| 149 |
+
"cell_type": "code",
|
| 150 |
+
"execution_count": null,
|
| 151 |
+
"id": "ac50b978",
|
| 152 |
+
"metadata": {},
|
| 153 |
+
"outputs": [],
|
| 154 |
+
"source": [
|
| 155 |
+
"class ModelEvaluation:\n",
|
| 156 |
+
" def __init__(self, config: ModelEvaluationConfig):\n",
|
| 157 |
+
" self.config = config\n",
|
| 158 |
+
" \n",
|
| 159 |
+
" \n",
|
| 160 |
+
" def generate_batch_sized_chunks(self, List_of_elements, batch_size):\n",
|
| 161 |
+
" \"\"\"split the dataset into smaller batches that we can process simulaneously\n",
|
| 162 |
+
" yield successive batch-sized chunks from list_of_elements.\"\"\"\n",
|
| 163 |
+
" \n",
|
| 164 |
+
" for i in range(0, len(List_of_elements), batch_size):\n",
|
| 165 |
+
" yield List_of_elements[i : i + batch_size]\n",
|
| 166 |
+
" \n",
|
| 167 |
+
" def calculate_metric_on_test_ds(self, dataset, metric, model, tokenizer,\n",
|
| 168 |
+
" batch_size=16, device=\"cuda\" if torch.cuda.is_available() else \"cpu\",\n",
|
| 169 |
+
" column_text=\"article\", \n",
|
| 170 |
+
" column_summary=\"highlights\"):\n",
|
| 171 |
+
" article_batches = list(self.generate_batch_sized_chunks(dataset[column_text], batch_size))\n",
|
| 172 |
+
" target_batches = list(self.generate_batch_sized_chunks(dataset[column_summary], batch_size))\n",
|
| 173 |
+
" \n",
|
| 174 |
+
" for article_batch, target_batch in tqdm(\n",
|
| 175 |
+
" zip(article_batches, target_batches), total=len(article_batches)):\n",
|
| 176 |
+
" \n",
|
| 177 |
+
" inputs = tokenizer( article_batch, max_length=1024, truncation=True,\n",
|
| 178 |
+
" padding=\"max_length\", return_tensors=\"pt\")\n",
|
| 179 |
+
" \n",
|
| 180 |
+
" s\n",
|
| 181 |
+
" \n",
|
| 182 |
+
" \n",
|
| 183 |
+
" \n",
|
| 184 |
+
" \n",
|
| 185 |
+
" "
|
| 186 |
+
]
|
| 187 |
+
}
|
| 188 |
+
],
|
| 189 |
+
"metadata": {
|
| 190 |
+
"kernelspec": {
|
| 191 |
+
"display_name": "textSummarizer",
|
| 192 |
+
"language": "python",
|
| 193 |
+
"name": "python3"
|
| 194 |
+
},
|
| 195 |
+
"language_info": {
|
| 196 |
+
"codemirror_mode": {
|
| 197 |
+
"name": "ipython",
|
| 198 |
+
"version": 3
|
| 199 |
+
},
|
| 200 |
+
"file_extension": ".py",
|
| 201 |
+
"mimetype": "text/x-python",
|
| 202 |
+
"name": "python",
|
| 203 |
+
"nbconvert_exporter": "python",
|
| 204 |
+
"pygments_lexer": "ipython3",
|
| 205 |
+
"version": "3.11.13"
|
| 206 |
+
}
|
| 207 |
+
},
|
| 208 |
+
"nbformat": 4,
|
| 209 |
+
"nbformat_minor": 5
|
| 210 |
+
}
|
research/trails.ipynb
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"cells": [
|
| 3 |
+
{
|
| 4 |
+
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
+
"id": "09cfed6a",
|
| 7 |
+
"metadata": {},
|
| 8 |
+
"outputs": [],
|
| 9 |
+
"source": [
|
| 10 |
+
"d = {\"key\" : \" value 1\", \"key2\" : \"value 2\", \"key3\" : \"value 3\"}"
|
| 11 |
+
]
|
| 12 |
+
},
|
| 13 |
+
{
|
| 14 |
+
"cell_type": "code",
|
| 15 |
+
"execution_count": 2,
|
| 16 |
+
"id": "e6fd4e25",
|
| 17 |
+
"metadata": {},
|
| 18 |
+
"outputs": [
|
| 19 |
+
{
|
| 20 |
+
"data": {
|
| 21 |
+
"text/plain": [
|
| 22 |
+
"' value 1'"
|
| 23 |
+
]
|
| 24 |
+
},
|
| 25 |
+
"execution_count": 2,
|
| 26 |
+
"metadata": {},
|
| 27 |
+
"output_type": "execute_result"
|
| 28 |
+
}
|
| 29 |
+
],
|
| 30 |
+
"source": [
|
| 31 |
+
"d['key']"
|
| 32 |
+
]
|
| 33 |
+
},
|
| 34 |
+
{
|
| 35 |
+
"cell_type": "code",
|
| 36 |
+
"execution_count": 9,
|
| 37 |
+
"id": "1602f57d",
|
| 38 |
+
"metadata": {},
|
| 39 |
+
"outputs": [
|
| 40 |
+
{
|
| 41 |
+
"data": {
|
| 42 |
+
"text/plain": [
|
| 43 |
+
"dict_keys(['key', 'key2', 'key3'])"
|
| 44 |
+
]
|
| 45 |
+
},
|
| 46 |
+
"execution_count": 9,
|
| 47 |
+
"metadata": {},
|
| 48 |
+
"output_type": "execute_result"
|
| 49 |
+
}
|
| 50 |
+
],
|
| 51 |
+
"source": [
|
| 52 |
+
"d.keys()"
|
| 53 |
+
]
|
| 54 |
+
},
|
| 55 |
+
{
|
| 56 |
+
"cell_type": "code",
|
| 57 |
+
"execution_count": 5,
|
| 58 |
+
"id": "d16c1607",
|
| 59 |
+
"metadata": {},
|
| 60 |
+
"outputs": [],
|
| 61 |
+
"source": [
|
| 62 |
+
"from box import ConfigBox"
|
| 63 |
+
]
|
| 64 |
+
},
|
| 65 |
+
{
|
| 66 |
+
"cell_type": "code",
|
| 67 |
+
"execution_count": 6,
|
| 68 |
+
"id": "1e392ff6",
|
| 69 |
+
"metadata": {},
|
| 70 |
+
"outputs": [],
|
| 71 |
+
"source": [
|
| 72 |
+
"d2 = ConfigBox(d)"
|
| 73 |
+
]
|
| 74 |
+
},
|
| 75 |
+
{
|
| 76 |
+
"cell_type": "code",
|
| 77 |
+
"execution_count": 7,
|
| 78 |
+
"id": "e0d6f2c1",
|
| 79 |
+
"metadata": {},
|
| 80 |
+
"outputs": [
|
| 81 |
+
{
|
| 82 |
+
"data": {
|
| 83 |
+
"text/plain": [
|
| 84 |
+
"ConfigBox({'key': ' value 1', 'key2': 'value 2', 'key3': 'value 3'})"
|
| 85 |
+
]
|
| 86 |
+
},
|
| 87 |
+
"execution_count": 7,
|
| 88 |
+
"metadata": {},
|
| 89 |
+
"output_type": "execute_result"
|
| 90 |
+
}
|
| 91 |
+
],
|
| 92 |
+
"source": [
|
| 93 |
+
"d2"
|
| 94 |
+
]
|
| 95 |
+
},
|
| 96 |
+
{
|
| 97 |
+
"cell_type": "code",
|
| 98 |
+
"execution_count": 8,
|
| 99 |
+
"id": "08765318",
|
| 100 |
+
"metadata": {},
|
| 101 |
+
"outputs": [
|
| 102 |
+
{
|
| 103 |
+
"data": {
|
| 104 |
+
"text/plain": [
|
| 105 |
+
"' value 1'"
|
| 106 |
+
]
|
| 107 |
+
},
|
| 108 |
+
"execution_count": 8,
|
| 109 |
+
"metadata": {},
|
| 110 |
+
"output_type": "execute_result"
|
| 111 |
+
}
|
| 112 |
+
],
|
| 113 |
+
"source": [
|
| 114 |
+
"d2.key"
|
| 115 |
+
]
|
| 116 |
+
},
|
| 117 |
+
{
|
| 118 |
+
"cell_type": "code",
|
| 119 |
+
"execution_count": 10,
|
| 120 |
+
"id": "851457ac",
|
| 121 |
+
"metadata": {},
|
| 122 |
+
"outputs": [],
|
| 123 |
+
"source": [
|
| 124 |
+
"def get_product(x: int , y: int) -> int:\n",
|
| 125 |
+
" return x * y"
|
| 126 |
+
]
|
| 127 |
+
},
|
| 128 |
+
{
|
| 129 |
+
"cell_type": "code",
|
| 130 |
+
"execution_count": 11,
|
| 131 |
+
"id": "ff2ee71b",
|
| 132 |
+
"metadata": {},
|
| 133 |
+
"outputs": [
|
| 134 |
+
{
|
| 135 |
+
"data": {
|
| 136 |
+
"text/plain": [
|
| 137 |
+
"8"
|
| 138 |
+
]
|
| 139 |
+
},
|
| 140 |
+
"execution_count": 11,
|
| 141 |
+
"metadata": {},
|
| 142 |
+
"output_type": "execute_result"
|
| 143 |
+
}
|
| 144 |
+
],
|
| 145 |
+
"source": [
|
| 146 |
+
"get_product(x = 2,y = 4)"
|
| 147 |
+
]
|
| 148 |
+
},
|
| 149 |
+
{
|
| 150 |
+
"cell_type": "code",
|
| 151 |
+
"execution_count": 12,
|
| 152 |
+
"id": "d37d2718",
|
| 153 |
+
"metadata": {},
|
| 154 |
+
"outputs": [
|
| 155 |
+
{
|
| 156 |
+
"data": {
|
| 157 |
+
"text/plain": [
|
| 158 |
+
"'44'"
|
| 159 |
+
]
|
| 160 |
+
},
|
| 161 |
+
"execution_count": 12,
|
| 162 |
+
"metadata": {},
|
| 163 |
+
"output_type": "execute_result"
|
| 164 |
+
}
|
| 165 |
+
],
|
| 166 |
+
"source": [
|
| 167 |
+
"get_product(x = 2, y = \"4\")"
|
| 168 |
+
]
|
| 169 |
+
},
|
| 170 |
+
{
|
| 171 |
+
"cell_type": "code",
|
| 172 |
+
"execution_count": 13,
|
| 173 |
+
"id": "153cbd1d",
|
| 174 |
+
"metadata": {},
|
| 175 |
+
"outputs": [],
|
| 176 |
+
"source": [
|
| 177 |
+
"from ensure import ensure_annotations"
|
| 178 |
+
]
|
| 179 |
+
},
|
| 180 |
+
{
|
| 181 |
+
"cell_type": "code",
|
| 182 |
+
"execution_count": 14,
|
| 183 |
+
"id": "a65f4a8e",
|
| 184 |
+
"metadata": {},
|
| 185 |
+
"outputs": [],
|
| 186 |
+
"source": [
|
| 187 |
+
"@ensure_annotations\n",
|
| 188 |
+
"def get_product(x: int , y: int) -> int:\n",
|
| 189 |
+
" return x * y"
|
| 190 |
+
]
|
| 191 |
+
},
|
| 192 |
+
{
|
| 193 |
+
"cell_type": "code",
|
| 194 |
+
"execution_count": 15,
|
| 195 |
+
"id": "61cb9d3e",
|
| 196 |
+
"metadata": {},
|
| 197 |
+
"outputs": [
|
| 198 |
+
{
|
| 199 |
+
"data": {
|
| 200 |
+
"text/plain": [
|
| 201 |
+
"8"
|
| 202 |
+
]
|
| 203 |
+
},
|
| 204 |
+
"execution_count": 15,
|
| 205 |
+
"metadata": {},
|
| 206 |
+
"output_type": "execute_result"
|
| 207 |
+
}
|
| 208 |
+
],
|
| 209 |
+
"source": [
|
| 210 |
+
"get_product(x = 2,y = 4)"
|
| 211 |
+
]
|
| 212 |
+
},
|
| 213 |
+
{
|
| 214 |
+
"cell_type": "code",
|
| 215 |
+
"execution_count": 17,
|
| 216 |
+
"id": "46a5e079",
|
| 217 |
+
"metadata": {},
|
| 218 |
+
"outputs": [
|
| 219 |
+
{
|
| 220 |
+
"ename": "EnsureError",
|
| 221 |
+
"evalue": "Argument y of type <class 'str'> to <function get_product at 0x0000024DC30C3380> does not match annotation type <class 'int'>",
|
| 222 |
+
"output_type": "error",
|
| 223 |
+
"traceback": [
|
| 224 |
+
"\u001b[31m---------------------------------------------------------------------------\u001b[39m",
|
| 225 |
+
"\u001b[31mEnsureError\u001b[39m Traceback (most recent call last)",
|
| 226 |
+
"\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[17]\u001b[39m\u001b[32m, line 1\u001b[39m\n\u001b[32m----> \u001b[39m\u001b[32m1\u001b[39m \u001b[43mget_product\u001b[49m\u001b[43m(\u001b[49m\u001b[43mx\u001b[49m\u001b[43m \u001b[49m\u001b[43m=\u001b[49m\u001b[43m \u001b[49m\u001b[32;43m2\u001b[39;49m\u001b[43m,\u001b[49m\u001b[43m \u001b[49m\u001b[43my\u001b[49m\u001b[43m \u001b[49m\u001b[43m=\u001b[49m\u001b[43m \u001b[49m\u001b[33;43m\"\u001b[39;49m\u001b[33;43m4\u001b[39;49m\u001b[33;43m\"\u001b[39;49m\u001b[43m)\u001b[49m\n",
|
| 227 |
+
"\u001b[36mFile \u001b[39m\u001b[32mc:\\Users\\jeeva\\.conda\\envs\\textSummarizer\\Lib\\site-packages\\ensure\\main.py:870\u001b[39m, in \u001b[36mWrappedFunctionReturn.__call__\u001b[39m\u001b[34m(self, *args, **kwargs)\u001b[39m\n\u001b[32m 868\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(value, templ):\n\u001b[32m 869\u001b[39m msg = \u001b[33m\"\u001b[39m\u001b[33mArgument \u001b[39m\u001b[38;5;132;01m{arg}\u001b[39;00m\u001b[33m of type \u001b[39m\u001b[38;5;132;01m{valt}\u001b[39;00m\u001b[33m to \u001b[39m\u001b[38;5;132;01m{f}\u001b[39;00m\u001b[33m \u001b[39m\u001b[33m\"\u001b[39m \u001b[33m\"\u001b[39m\u001b[33mdoes not match annotation type \u001b[39m\u001b[38;5;132;01m{t}\u001b[39;00m\u001b[33m\"\u001b[39m\n\u001b[32m--> \u001b[39m\u001b[32m870\u001b[39m \u001b[38;5;28;01mraise\u001b[39;00m EnsureError(msg.format(arg=arg, f=\u001b[38;5;28mself\u001b[39m.f, t=templ, valt=\u001b[38;5;28mtype\u001b[39m(value)))\n\u001b[32m 872\u001b[39m return_val = \u001b[38;5;28mself\u001b[39m.f(*args, **kwargs)\n\u001b[32m 873\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m \u001b[38;5;129;01mnot\u001b[39;00m \u001b[38;5;28misinstance\u001b[39m(return_val, \u001b[38;5;28mself\u001b[39m.return_templ):\n",
|
| 228 |
+
"\u001b[31mEnsureError\u001b[39m: Argument y of type <class 'str'> to <function get_product at 0x0000024DC30C3380> does not match annotation type <class 'int'>"
|
| 229 |
+
]
|
| 230 |
+
}
|
| 231 |
+
],
|
| 232 |
+
"source": [
|
| 233 |
+
"get_product(x = 2, y = \"4\")"
|
| 234 |
+
]
|
| 235 |
+
},
|
| 236 |
+
{
|
| 237 |
+
"cell_type": "code",
|
| 238 |
+
"execution_count": null,
|
| 239 |
+
"id": "b9e1c167",
|
| 240 |
+
"metadata": {},
|
| 241 |
+
"outputs": [],
|
| 242 |
+
"source": []
|
| 243 |
+
}
|
| 244 |
+
],
|
| 245 |
+
"metadata": {
|
| 246 |
+
"kernelspec": {
|
| 247 |
+
"display_name": "textSummarizer",
|
| 248 |
+
"language": "python",
|
| 249 |
+
"name": "python3"
|
| 250 |
+
},
|
| 251 |
+
"language_info": {
|
| 252 |
+
"codemirror_mode": {
|
| 253 |
+
"name": "ipython",
|
| 254 |
+
"version": 3
|
| 255 |
+
},
|
| 256 |
+
"file_extension": ".py",
|
| 257 |
+
"mimetype": "text/x-python",
|
| 258 |
+
"name": "python",
|
| 259 |
+
"nbconvert_exporter": "python",
|
| 260 |
+
"pygments_lexer": "ipython3",
|
| 261 |
+
"version": "3.11.13"
|
| 262 |
+
}
|
| 263 |
+
},
|
| 264 |
+
"nbformat": 4,
|
| 265 |
+
"nbformat_minor": 5
|
| 266 |
+
}
|
setup.py
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import setuptools
|
| 2 |
+
|
| 3 |
+
with open("README.md" , "r" , encoding="utf-8") as f:
|
| 4 |
+
long_description = f.read()
|
| 5 |
+
|
| 6 |
+
__version__ = "0.0.0"
|
| 7 |
+
|
| 8 |
+
REPO_NAME = "AI_Summarizer"
|
| 9 |
+
AUTHOR_USER_NAME = "jeevant"
|
| 10 |
+
SRC_REPO = "textSummarizer"
|
| 11 |
+
AUTHOR_EMAIL = "jeevantmudgil10@gmail.com"
|
| 12 |
+
|
| 13 |
+
setuptools.setup(
|
| 14 |
+
name=SRC_REPO,
|
| 15 |
+
version=__version__,
|
| 16 |
+
author=AUTHOR_USER_NAME,
|
| 17 |
+
author_email=AUTHOR_EMAIL,
|
| 18 |
+
description="A Small python package for Text NLP App",
|
| 19 |
+
long_description=long_description,
|
| 20 |
+
long_description_content="text/markdown",
|
| 21 |
+
url=f"https://github.com/{AUTHOR_USER_NAME}/{REPO_NAME}",
|
| 22 |
+
project_urls={
|
| 23 |
+
"Bug Tracker": f"https://github.com/{AUTHOR_USER_NAME}/{REPO_NAME}/issues",
|
| 24 |
+
},
|
| 25 |
+
package_dir={"": "src"},
|
| 26 |
+
packages=setuptools.find_packages(where="src")
|
| 27 |
+
)
|
src/textSummarizer/__init__.py
ADDED
|
File without changes
|
src/textSummarizer/components/__init__.py
ADDED
|
File without changes
|
src/textSummarizer/components/data_ingestion.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import urllib.request as request
|
| 3 |
+
import zipfile
|
| 4 |
+
from textSummarizer.logging import logger
|
| 5 |
+
from textSummarizer.utils.common import get_size
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from textSummarizer.entity import DataIngestionConfig
|
| 8 |
+
|
| 9 |
+
class DataIngestion:
|
| 10 |
+
def __init__(self, config: DataIngestionConfig):
|
| 11 |
+
self.config = config
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def download_data(self):
|
| 15 |
+
if not os.path.exists(self.config.local_data_file):
|
| 16 |
+
filename, headers = request.urlretrieve(
|
| 17 |
+
url = self.config.source_URL,
|
| 18 |
+
filename = self.config.local_data_file
|
| 19 |
+
)
|
| 20 |
+
logger.info(f"{filename} download! with following info : \n{headers}")
|
| 21 |
+
else:
|
| 22 |
+
logger.info(f"File already exists of size : {get_size(Path(self.config.local_data_file))}")
|
| 23 |
+
|
| 24 |
+
def extract_zip_file(self):
|
| 25 |
+
"""
|
| 26 |
+
zip_file_path : str
|
| 27 |
+
EXtracts the zip file to the given directory.
|
| 28 |
+
Functions returns None.
|
| 29 |
+
"""
|
| 30 |
+
unzip_path = self.config.unzip_dir
|
| 31 |
+
os.makedirs(unzip_path, exist_ok=True)
|
| 32 |
+
with zipfile.ZipFile(self.config.local_data_file, 'r') as zip_ref:
|
| 33 |
+
zip_ref.extractall(unzip_path)
|
| 34 |
+
|
src/textSummarizer/components/data_transformation.py
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from textSummarizer.logging import logger
|
| 3 |
+
from transformers import AutoTokenizer
|
| 4 |
+
from datasets import load_dataset, load_from_disk
|
| 5 |
+
from textSummarizer.entity import DataTransformationConfig
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class DataTransformation:
|
| 9 |
+
def __init__(self, config: DataTransformationConfig):
|
| 10 |
+
self.config = config
|
| 11 |
+
self.tokenizer = AutoTokenizer.from_pretrained(self.config.tokenizer_name)
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def convert_examples_to_features(self, example_batch):
|
| 15 |
+
input_encodings = self.tokenizer(example_batch['dialogue'] , max_length=1024, truncation=True)
|
| 16 |
+
|
| 17 |
+
with self.tokenizer.as_target_tokenizer():
|
| 18 |
+
target_encodings = self.tokenizer(example_batch['summary'], max_length=128, truncation=True)
|
| 19 |
+
|
| 20 |
+
return {
|
| 21 |
+
'input_ids': input_encodings['input_ids'],
|
| 22 |
+
'attention_mask': input_encodings['attention_mask'],
|
| 23 |
+
'labels': target_encodings['input_ids'],
|
| 24 |
+
}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def convert(self):
|
| 28 |
+
dataset_samsum = load_from_disk(self.config.data_path)
|
| 29 |
+
dataset_samsum_pt = dataset_samsum.map(self.convert_examples_to_features, batched=True)
|
| 30 |
+
dataset_samsum_pt.save_to_disk(os.path.join(self.config.root_dir, "samsum_dataset"))
|
| 31 |
+
|
src/textSummarizer/components/data_validation.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from textSummarizer.logging import logger
|
| 3 |
+
from textSummarizer.entity import DataValidationConfig
|
| 4 |
+
|
| 5 |
+
class DataValidation:
|
| 6 |
+
def __init__(self, config: DataValidationConfig):
|
| 7 |
+
self.config = config
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def validate_all_files_exists(self) -> bool:
|
| 12 |
+
try:
|
| 13 |
+
validation_status = None
|
| 14 |
+
|
| 15 |
+
all_files = os.listdir(os.path.join("artifacts" , "data_ingestion" , "samsum_dataset"))
|
| 16 |
+
|
| 17 |
+
for file in all_files:
|
| 18 |
+
if file not in self.config.ALL_REQUIRED_FILES:
|
| 19 |
+
validation_status = False
|
| 20 |
+
with open(self.config.STATUS_FILE, 'w') as f:
|
| 21 |
+
f.write(f"Validation status: {validation_status}")
|
| 22 |
+
else:
|
| 23 |
+
validation_status = True
|
| 24 |
+
with open(self.config.STATUS_FILE, 'w') as f:
|
| 25 |
+
f.write(f"Validation status: {validation_status}")
|
| 26 |
+
|
| 27 |
+
return validation_status
|
| 28 |
+
|
| 29 |
+
except Exception as e:
|
| 30 |
+
raise e
|
src/textSummarizer/components/model_trainer.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import TrainingArguments, Trainer
|
| 2 |
+
from transformers import DataCollatorForSeq2Seq
|
| 3 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 4 |
+
from datasets import load_dataset, load_from_disk
|
| 5 |
+
from textSummarizer.entity import ModelTrainerConfig
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
class ModelTrainer:
|
| 9 |
+
def __init__(self, config: ModelTrainerConfig):
|
| 10 |
+
self.config = config
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def train(self):
|
| 14 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 15 |
+
tokenizer = AutoTokenizer.from_pretrained(self.config.model_ckpt)
|
| 16 |
+
model_pegasus = AutoModelForSeq2SeqLM.from_pretrained(self.config.model_ckpt, use_safetensors=True).to(device)
|
| 17 |
+
seq2seq_data_collator = DataCollatorForSeq2Seq(tokenizer, model=model_pegasus)
|
| 18 |
+
|
| 19 |
+
dataset_samsum_pt = load_from_disk(self.config.data_path)
|
| 20 |
+
|
| 21 |
+
# trainer_args = TrainingArguments(
|
| 22 |
+
# output_dir=self.config.root_dir,
|
| 23 |
+
# num_train_epochs=self.config.num_train_epochs,
|
| 24 |
+
# warmup_steps=self.config.warmup_steps,
|
| 25 |
+
# per_device_train_batch_size=self.config.per_device_train_batch_size,
|
| 26 |
+
# per_device_eval_batch_size=self.config.per_device_train_batch_size,
|
| 27 |
+
# weight_decay=self.config.weight_decay,
|
| 28 |
+
# logging_steps=self.config.logging_steps,
|
| 29 |
+
# evaluation_strategy=self.config.evaluation_strategy,
|
| 30 |
+
# eval_steps=self.config.eval_steps,
|
| 31 |
+
# save_steps=1e6,
|
| 32 |
+
# gradient_accumulation_steps=self.config.gradient_accumulation_steps
|
| 33 |
+
# )
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
trainer_args = TrainingArguments(
|
| 37 |
+
output_dir=self.config.root_dir,
|
| 38 |
+
num_train_epochs=1,
|
| 39 |
+
warmup_steps=500,
|
| 40 |
+
per_device_train_batch_size=1,
|
| 41 |
+
per_device_eval_batch_size=1,
|
| 42 |
+
weight_decay=0.01,
|
| 43 |
+
logging_steps=10
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
trainer = Trainer(model=model_pegasus, args=trainer_args,
|
| 49 |
+
tokenizer=tokenizer, data_collator=seq2seq_data_collator,
|
| 50 |
+
train_dataset=dataset_samsum_pt["test"],
|
| 51 |
+
eval_dataset=dataset_samsum_pt["validation"])
|
| 52 |
+
|
| 53 |
+
trainer.train()
|
| 54 |
+
|
| 55 |
+
model_pegasus.save_pretrained(os.path.join(self.config.root_dir, "pegasus-samsum-model"))
|
| 56 |
+
|
| 57 |
+
tokenizer.save_pretrained(os.path.join(self.config.root_dir, "tokenizer"))
|
src/textSummarizer/config/__init__.py
ADDED
|
File without changes
|
src/textSummarizer/config/configuration.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from textSummarizer.constants import *
|
| 2 |
+
from textSummarizer.utils.common import read_yaml, create_directories
|
| 3 |
+
from textSummarizer.entity import (DataIngestionConfig,
|
| 4 |
+
DataValidationConfig,
|
| 5 |
+
DataTransformationConfig,
|
| 6 |
+
ModelTrainerConfig)
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
class ConfigurationManager:
|
| 10 |
+
def __init__(
|
| 11 |
+
self,
|
| 12 |
+
config_filepath: str = CONFIG_FILE_PATH,
|
| 13 |
+
params_filepath: str = PARAMS_FILE_PATH):
|
| 14 |
+
|
| 15 |
+
self.config = read_yaml(config_filepath)
|
| 16 |
+
self.params = read_yaml(params_filepath)
|
| 17 |
+
|
| 18 |
+
create_directories([self.config.artifacts_root])
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def get_data_ingestion_config(self) -> DataIngestionConfig:
|
| 22 |
+
config =self.config.data_ingestion
|
| 23 |
+
|
| 24 |
+
create_directories([config.root_dir])
|
| 25 |
+
|
| 26 |
+
data_ingestion_config = DataIngestionConfig(
|
| 27 |
+
root_dir = config.root_dir,
|
| 28 |
+
source_URL = config.source_URL,
|
| 29 |
+
local_data_file = config.local_data_file,
|
| 30 |
+
unzip_dir = config.unzip_dir,
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
return data_ingestion_config
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def get_data_validation_config(self) -> DataValidationConfig:
|
| 37 |
+
config = self.config.data_validation
|
| 38 |
+
|
| 39 |
+
create_directories([config.root_dir])
|
| 40 |
+
|
| 41 |
+
data_validation_config = DataValidationConfig(
|
| 42 |
+
root_dir=config.root_dir,
|
| 43 |
+
STATUS_FILE=config.STATUS_FILE,
|
| 44 |
+
ALL_REQUIRED_FILES=config.ALL_REQUIRED_FILES,
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return data_validation_config
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def get_data_transformation_config(self) -> DataTransformationConfig:
|
| 51 |
+
config= self.config.data_transformation
|
| 52 |
+
|
| 53 |
+
create_directories([config.root_dir])
|
| 54 |
+
|
| 55 |
+
data_transformation_config = DataTransformationConfig(
|
| 56 |
+
root_dir=config.root_dir,
|
| 57 |
+
data_path=config.data_path,
|
| 58 |
+
tokenizer_name=config.tokenizer_name,
|
| 59 |
+
)
|
| 60 |
+
|
| 61 |
+
return data_transformation_config
|
| 62 |
+
|
| 63 |
+
|
| 64 |
+
def get_model_trainer_config(self) -> ModelTrainerConfig:
|
| 65 |
+
config = self.config.model_trainer
|
| 66 |
+
params = self.params.TrainingArguments
|
| 67 |
+
|
| 68 |
+
create_directories([config.root_dir])
|
| 69 |
+
|
| 70 |
+
model_trainer_config = ModelTrainerConfig(
|
| 71 |
+
root_dir=config.root_dir,
|
| 72 |
+
data_path=config.data_path,
|
| 73 |
+
model_ckpt=config.model_ckpt,
|
| 74 |
+
num_train_epochs=params.num_train_epochs,
|
| 75 |
+
warmup_steps=params.warmup_steps,
|
| 76 |
+
per_device_train_batch_size=params.per_device_train_batch_size,
|
| 77 |
+
weight_decay=params.weight_decay,
|
| 78 |
+
logging_steps=params.logging_steps,
|
| 79 |
+
evaluation_strategy=params.evaluation_strategy,
|
| 80 |
+
eval_steps=params.evaluation_strategy,
|
| 81 |
+
save_steps=params.save_steps,
|
| 82 |
+
gradient_accumulation_steps=params.gradient_accumulation_steps
|
| 83 |
+
)
|
| 84 |
+
|
| 85 |
+
return model_trainer_config
|
src/textSummarizer/constants/__init__.py
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
|
| 3 |
+
CONFIG_FILE_PATH = Path("config/config.yaml")
|
| 4 |
+
PARAMS_FILE_PATH = Path("params.yaml")
|
src/textSummarizer/entity/__init__.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
|
| 4 |
+
@dataclass(frozen=True)
|
| 5 |
+
class DataIngestionConfig:
|
| 6 |
+
root_dir : Path
|
| 7 |
+
source_URL : str
|
| 8 |
+
local_data_file : Path
|
| 9 |
+
unzip_dir : Path
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
@dataclass(frozen=True)
|
| 13 |
+
class DataValidationConfig:
|
| 14 |
+
root_dir : Path
|
| 15 |
+
STATUS_FILE : str
|
| 16 |
+
ALL_REQUIRED_FILES : list
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
@dataclass(frozen=True)
|
| 20 |
+
class DataTransformationConfig:
|
| 21 |
+
root_dir: Path
|
| 22 |
+
data_path: Path
|
| 23 |
+
tokenizer_name: Path
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@dataclass(frozen=True)
|
| 28 |
+
class ModelTrainerConfig:
|
| 29 |
+
root_dir: Path
|
| 30 |
+
data_path: Path
|
| 31 |
+
model_ckpt: Path
|
| 32 |
+
num_train_epochs: int
|
| 33 |
+
warmup_steps: int
|
| 34 |
+
per_device_train_batch_size: int
|
| 35 |
+
weight_decay: float
|
| 36 |
+
logging_steps: int
|
| 37 |
+
evaluation_strategy: str
|
| 38 |
+
eval_steps: int
|
| 39 |
+
save_steps: float
|
| 40 |
+
gradient_accumulation_steps: int
|
src/textSummarizer/logging/__init__.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
logging_str = "[%(asctime)s: %(levelname)s: %(module)s : %(message)s ]"
|
| 6 |
+
log_dir = "logs"
|
| 7 |
+
log_filepath = os.path.join(log_dir, "running_logs.log")
|
| 8 |
+
os.makedirs(log_dir, exist_ok=True)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
logging.basicConfig(
|
| 12 |
+
level= logging.INFO,
|
| 13 |
+
format= logging_str,
|
| 14 |
+
|
| 15 |
+
handlers=[
|
| 16 |
+
logging.FileHandler(log_filepath),
|
| 17 |
+
logging.StreamHandler(sys.stdout)
|
| 18 |
+
]
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
logger = logging.getLogger("textSummarizerLogger")
|
src/textSummarizer/pipeline/__init__.py
ADDED
|
File without changes
|
src/textSummarizer/pipeline/stage_01_data_ingestion.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from textSummarizer.config.configuration import ConfigurationManager
|
| 2 |
+
from textSummarizer.components.data_ingestion import DataIngestion
|
| 3 |
+
from textSummarizer.logging import logger
|
| 4 |
+
|
| 5 |
+
class DataIngestionTrainingPipeline:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
pass
|
| 8 |
+
|
| 9 |
+
def main(self):
|
| 10 |
+
config = ConfigurationManager()
|
| 11 |
+
data_ingestion_config = config.get_data_ingestion_config()
|
| 12 |
+
data_ingestion = DataIngestion(data_ingestion_config)
|
| 13 |
+
data_ingestion.download_data()
|
| 14 |
+
data_ingestion.extract_zip_file()
|
| 15 |
+
|
| 16 |
+
|
src/textSummarizer/pipeline/stage_02_data_validation.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from textSummarizer.config.configuration import ConfigurationManager
|
| 2 |
+
from textSummarizer.components.data_validation import DataValidation
|
| 3 |
+
from textSummarizer.logging import logger
|
| 4 |
+
|
| 5 |
+
class DataValidationTrainingPipeline:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
pass
|
| 8 |
+
|
| 9 |
+
def main(self):
|
| 10 |
+
config = ConfigurationManager()
|
| 11 |
+
data_validation_config = config.get_data_validation_config()
|
| 12 |
+
data_validation = DataValidation(data_validation_config)
|
| 13 |
+
data_validation.validate_all_files_exists()
|
| 14 |
+
|
| 15 |
+
|
src/textSummarizer/pipeline/stage_03_data_transformation.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from textSummarizer.config.configuration import ConfigurationManager
|
| 2 |
+
from textSummarizer.components.data_transformation import DataTransformation
|
| 3 |
+
from textSummarizer.logging import logger
|
| 4 |
+
|
| 5 |
+
class DataTransformationTrainingPipeline:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
pass
|
| 8 |
+
|
| 9 |
+
def main(self):
|
| 10 |
+
config = ConfigurationManager()
|
| 11 |
+
data_transformation_config = config.get_data_transformation_config()
|
| 12 |
+
data_transformation = DataTransformation(config=data_transformation_config)
|
| 13 |
+
data_transformation.convert()
|
| 14 |
+
|
| 15 |
+
|
src/textSummarizer/pipeline/stage_04_model_trainer.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from textSummarizer.config.configuration import ConfigurationManager
|
| 2 |
+
from textSummarizer.components.model_trainer import ModelTrainer
|
| 3 |
+
from textSummarizer.logging import logger
|
| 4 |
+
|
| 5 |
+
class ModelTrainerTrainingPipeline:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
pass
|
| 8 |
+
|
| 9 |
+
def main(self):
|
| 10 |
+
config = ConfigurationManager()
|
| 11 |
+
model_trainer_config = config.get_model_trainer_config()
|
| 12 |
+
model_trainer_config = ModelTrainer(config=model_trainer_config)
|
| 13 |
+
model_trainer_config.train()
|
| 14 |
+
|
| 15 |
+
|
src/textSummarizer/utils/__init__.py
ADDED
|
File without changes
|
src/textSummarizer/utils/common.py
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from box.exceptions import BoxValueError
|
| 3 |
+
import yaml
|
| 4 |
+
from textSummarizer.logging import logger
|
| 5 |
+
from ensure import ensure_annotations
|
| 6 |
+
from box import ConfigBox
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
from typing import Any
|
| 9 |
+
|
| 10 |
+
@ensure_annotations
|
| 11 |
+
def read_yaml(path_to_yaml: Path) -> ConfigBox:
|
| 12 |
+
"""_summary_
|
| 13 |
+
Read YAML file and convert it into a ConfigBox object.
|
| 14 |
+
|
| 15 |
+
Args:
|
| 16 |
+
path_to_yaml (Path): _description_
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
Raises:
|
| 20 |
+
BoxValueError: If the YAML file cannot be parsed.
|
| 21 |
+
ValueError: If the YAML file does not contain a dictionary.
|
| 22 |
+
e: empty file error
|
| 23 |
+
|
| 24 |
+
Returns:
|
| 25 |
+
ConfigBox : ConfigBox type
|
| 26 |
+
"""
|
| 27 |
+
|
| 28 |
+
try:
|
| 29 |
+
with open(path_to_yaml) as yaml_file:
|
| 30 |
+
content = yaml.safe_load(yaml_file)
|
| 31 |
+
logger.info(f"yaml. file: {path_to_yaml} loaded successfully.")
|
| 32 |
+
return ConfigBox(content)
|
| 33 |
+
except BoxValueError:
|
| 34 |
+
raise ValueError(f"YAML file is empty with path : {path_to_yaml}")
|
| 35 |
+
except Exception as e:
|
| 36 |
+
raise e
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
@ensure_annotations
|
| 40 |
+
def create_directories(path_to_directories: list, verbose=True):
|
| 41 |
+
"""
|
| 42 |
+
create list of directories
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
path_to_directory (list): list of directories.
|
| 46 |
+
ignore_log(bool, optional) : ignore if multiple directories are created. Defaults to False.
|
| 47 |
+
"""
|
| 48 |
+
for path in path_to_directories:
|
| 49 |
+
os.makedirs(path, exist_ok=True)
|
| 50 |
+
if verbose:
|
| 51 |
+
logger.info(f"created directory at path : {path}")
|
| 52 |
+
|
| 53 |
+
@ensure_annotations
|
| 54 |
+
def get_size(path: Path) ->str:
|
| 55 |
+
"""get size of file in kbs
|
| 56 |
+
|
| 57 |
+
Args:
|
| 58 |
+
path (Path): Path of the files
|
| 59 |
+
|
| 60 |
+
Returns:
|
| 61 |
+
str: =size in KB
|
| 62 |
+
"""
|
| 63 |
+
size_in_kb = round(os.path.getsize(path)/1024)
|
| 64 |
+
return f"~{size_in_kb} KB"
|
template.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from pathlib import Path
|
| 3 |
+
import logging
|
| 4 |
+
|
| 5 |
+
logging.basicConfig(level=logging.INFO, format='[%(asctime)s]:(message)s:')
|
| 6 |
+
|
| 7 |
+
project_name = "testSummarizer"
|
| 8 |
+
|
| 9 |
+
list_of_files = [
|
| 10 |
+
".github/workflows/.gitkeep",
|
| 11 |
+
f"src/{project_name}/__init__.py",
|
| 12 |
+
f"src/{project_name}/components/__init__.py",
|
| 13 |
+
f"src/{project_name}/utils/__init__.py",
|
| 14 |
+
f"src/{project_name}/utils/common.py",
|
| 15 |
+
f"src/{project_name}/logging/__init__.py",
|
| 16 |
+
f"src/{project_name}/config/__init__.py",
|
| 17 |
+
f"src/{project_name}/config/configuration.py",
|
| 18 |
+
f"src/{project_name}/pipeline/__init__.py",
|
| 19 |
+
f"src/{project_name}/entity/__init__.py",
|
| 20 |
+
f"src/{project_name}/constants/__init__.py",
|
| 21 |
+
"config/config.yaml",
|
| 22 |
+
"params.yaml",
|
| 23 |
+
"app.py",
|
| 24 |
+
"main.py",
|
| 25 |
+
"Dockerfile",
|
| 26 |
+
"requirements.txt",
|
| 27 |
+
"setup.py",
|
| 28 |
+
"research/trails.ipynb"
|
| 29 |
+
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
for filepath in list_of_files:
|
| 33 |
+
filepath = Path(filepath)
|
| 34 |
+
filedir, filename = os.path.split(filepath)
|
| 35 |
+
|
| 36 |
+
if filedir != "":
|
| 37 |
+
os.makedirs(filedir, exist_ok=True)
|
| 38 |
+
logging.info(f"Creating directry:{filepath} for the file {filepath}")
|
| 39 |
+
|
| 40 |
+
if (not os.path.exists(filepath)) or (os.path.getsize(filepath) == 0):
|
| 41 |
+
with open(filepath, 'w') as f:
|
| 42 |
+
pass
|
| 43 |
+
logging.info(f"Creating empty file: {filepath}")
|
| 44 |
+
|
| 45 |
+
else :
|
| 46 |
+
logging.info(f"{filename} is already exists.")
|