Spaces:
Sleeping
Sleeping
| import pickle | |
| import pandas as pd | |
| def load_service_data(file_path: str) -> pd.DataFrame: | |
| """ | |
| Loads a CSV file into a Pandas DataFrame and sets the index to the 'service' column. | |
| Parameters: | |
| file_path (str): Path to the CSV file. | |
| Returns: | |
| pd.DataFrame: DataFrame with 'service' as the index. | |
| """ | |
| try: | |
| df = pd.read_csv(file_path) | |
| df = df.set_index("service") # Set 'service' as index | |
| return df | |
| except FileNotFoundError: | |
| print(f"Error: The file '{file_path}' was not found.") | |
| return pd.DataFrame() # Return an empty DataFrame on error | |
| except Exception as e: | |
| print(f"Error loading CSV file '{file_path}': {e}") | |
| return pd.DataFrame() | |
| def load_pickle(file_path: str): | |
| """Loads and returns data from a Pickle (.pkl) file.""" | |
| try: | |
| with open(file_path, "rb") as file: # Open in 'rb' (read binary) mode | |
| return pickle.load(file) | |
| except FileNotFoundError: | |
| print(f"Error: The file '{file_path}' was not found.") | |
| return None | |
| except Exception as e: | |
| print(f"Error reading Pickle file '{file_path}': {e}") | |
| return None | |
| def load_file(file_path: str) -> str: | |
| """Reads the text from a file safely.""" | |
| try: | |
| with open(file_path, "r", encoding="utf-8") as file: | |
| return file.read() | |
| except FileNotFoundError: | |
| print(f"Error: The file '{file_path}' was not found.") | |
| return "" | |
| except Exception as e: | |
| print(f"Error reading file '{file_path}': {e}") | |
| return "" |