arif670 commited on
Commit
f01b8f5
·
verified ·
1 Parent(s): e014ca7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -7,32 +7,30 @@ from pdf2image import convert_from_path
7
 
8
  logging.basicConfig(level=logging.INFO)
9
 
10
- # Name of the default CSV file. It will be created if not found.
11
  DEFAULT_CSV = "default.csv"
12
- cwd = os.getcwd()
13
- logging.info(f"Working directory: {cwd}")
14
-
15
- if not os.path.isfile(DEFAULT_CSV):
16
- logging.info(f"{DEFAULT_CSV} not found. Creating a default CSV file.")
17
- with open(DEFAULT_CSV, "w") as f:
18
- f.write("Name,Role,Reporting To\nAlice,CEO,\nBob,Manager,Alice\nCharlie,Engineer,Bob")
19
 
20
  def load_csv(file):
21
  """
22
- If a valid file path is provided and it does not equal the working directory,
23
- load that CSV. Otherwise, fall back to the default CSV.
24
  """
25
  cwd = os.getcwd()
26
  logging.info(f"load_csv received: {file}")
27
- # If no file provided or file equals working directory, use default.
28
- if not file or file.strip() == "" or os.path.abspath(file) == cwd:
 
29
  logging.info("No valid file provided; using default CSV.")
30
- return pd.read_csv(DEFAULT_CSV)
31
- # If file exists and is not a directory, use it.
 
 
 
 
32
  if os.path.isfile(file):
33
  logging.info(f"Loading uploaded CSV: {file}")
34
  return pd.read_csv(file)
35
- # Otherwise, warn and use default.
36
  logging.warning(f"Provided file path '{file}' is not valid. Using default CSV.")
37
  return pd.read_csv(DEFAULT_CSV)
38
 
 
7
 
8
  logging.basicConfig(level=logging.INFO)
9
 
10
+ # Name of the default CSV file. Ensure this file exists in your repository.
11
  DEFAULT_CSV = "default.csv"
 
 
 
 
 
 
 
12
 
13
  def load_csv(file):
14
  """
15
+ Load a CSV file from the given file path.
16
+ If the file is missing, empty, or is a directory, load the default CSV.
17
  """
18
  cwd = os.getcwd()
19
  logging.info(f"load_csv received: {file}")
20
+
21
+ # If file is empty, None, or a directory, use the default CSV.
22
+ if not file or file.strip() == "" or os.path.isdir(file) or os.path.abspath(file) == cwd:
23
  logging.info("No valid file provided; using default CSV.")
24
+ if os.path.isfile(DEFAULT_CSV):
25
+ return pd.read_csv(DEFAULT_CSV)
26
+ else:
27
+ raise FileNotFoundError("Default CSV not found.")
28
+
29
+ # If the file exists and is not a directory, load it.
30
  if os.path.isfile(file):
31
  logging.info(f"Loading uploaded CSV: {file}")
32
  return pd.read_csv(file)
33
+
34
  logging.warning(f"Provided file path '{file}' is not valid. Using default CSV.")
35
  return pd.read_csv(DEFAULT_CSV)
36