Spaces:
No application file
No application file
Download src/code/data_loading.py from Dashpreetsinghofficial/Flytxt_Assignment: direct link, hf CLI and curl.
- Browser
- Download file 1.25 kB
-
https://huggingface.co/spaces/Dashpreetsinghofficial/Flytxt_Assignment/resolve/main/src/code/data_loading.py
- Command line
-
hf download hf://spaces/Dashpreetsinghofficial/Flytxt_Assignment/src/code/data_loading.py
-
curl -L -o data_loading.py https://huggingface.co/spaces/Dashpreetsinghofficial/Flytxt_Assignment/resolve/main/src/code/data_loading.py
1.25 kB
| import pandas as pd | |
| import sys | |
| import os | |
| # Add project root to sys.path | |
| PROJECT_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), '../..')) | |
| sys.path.append(PROJECT_ROOT) | |
| from definitions.definition import DATA | |
| class DataLoading: | |
| def __init__(self, file_path=DATA): | |
| self.file_path = file_path | |
| def fetch(self): | |
| if not os.path.exists(self.file_path): | |
| print(f"File not found: {self.file_path}") | |
| return None | |
| try: | |
| data = pd.read_csv(self.file_path) | |
| required_columns = {'book_name', 'summaries', 'categories'} | |
| missing_columns = required_columns - set(data.columns) | |
| if missing_columns: | |
| # print(f"Missing expected columns: {missing_columns}") | |
| return None | |
| # Drop rows where 'book_name', 'summaries', or 'categories' are missing | |
| data.dropna(subset=['book_name', 'summaries', 'categories'], inplace=True) | |
| # Reset index after dropping rows | |
| data.reset_index(drop=True, inplace=True) | |
| return data | |
| except Exception as e: | |
| print(f"Error while loading data: {e}") | |
| return None | |
| run = DataLoading() | |
| run.fetch() | |