arif670 commited on
Commit
cec9f06
·
verified ·
1 Parent(s): 8817595

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -12
app.py CHANGED
@@ -7,23 +7,31 @@ from pdf2image import convert_from_path
7
 
8
  logging.basicConfig(level=logging.INFO)
9
 
10
- # Name of the default CSV file (must be in your repository)
11
- DEFAULT_CSV = "default.csv"
12
 
13
  def load_csv(file):
14
  """
15
- Load the CSV file.
16
- If a valid file path is provided and it does not equal the working directory,
17
- use it. Otherwise, fall back to the default CSV.
18
  """
19
- if file and isinstance(file, str) and os.path.isfile(file) and file != os.getcwd():
20
- logging.info(f"Loading uploaded file: {file}")
 
 
 
 
 
 
 
 
 
21
  return pd.read_csv(file)
22
- elif os.path.isfile(DEFAULT_CSV):
23
- logging.info("No valid file uploaded; using default CSV.")
24
- return pd.read_csv(DEFAULT_CSV)
25
  else:
26
- raise FileNotFoundError("No CSV file provided and default CSV not found.")
 
 
 
 
27
 
28
  def build_tree(df):
29
  employees = {}
@@ -164,6 +172,7 @@ def generate_chart(file, title):
164
  HTML(string=html_content).write_pdf(pdf_path)
165
  logging.info("PDF generated successfully.")
166
  except Exception as e:
 
167
  return None, f"Error generating PDF: {e}"
168
 
169
  try:
@@ -174,13 +183,15 @@ def generate_chart(file, title):
174
  else:
175
  image_path = ""
176
  except Exception as e:
 
177
  image_path = ""
178
 
179
  return image_path, pdf_path
180
 
181
  with gr.Blocks() as demo:
182
  gr.Markdown("## Organization Chart Generator")
183
- gr.Markdown("Upload a CSV file (optional). If no file is uploaded, the default CSV (default.csv) will be used.")
 
184
  file_input = gr.File(label="Upload CSV File (optional)", type="filepath")
185
  title_input = gr.Textbox(label="Enter PDF Title", placeholder="Company Org Chart")
186
  submit_button = gr.Button("Generate Chart")
 
7
 
8
  logging.basicConfig(level=logging.INFO)
9
 
10
+ DEFAULT_CSV = "default.csv" # Must be included in your repository
 
11
 
12
  def load_csv(file):
13
  """
14
+ If a valid file path is provided and it is not equal to the working directory,
15
+ load that CSV. Otherwise, load the default CSV.
 
16
  """
17
+ # If file is empty, None, or equals the current working directory, use default.
18
+ cwd = os.getcwd()
19
+ if not file or file == "" or file.strip() == "" or file == cwd:
20
+ logging.info("No valid file provided; using default CSV.")
21
+ if os.path.isfile(DEFAULT_CSV):
22
+ return pd.read_csv(DEFAULT_CSV)
23
+ else:
24
+ raise FileNotFoundError("Default CSV not found.")
25
+ # Otherwise, if the file exists and is not a directory, use it.
26
+ if os.path.isfile(file):
27
+ logging.info(f"Loading uploaded CSV: {file}")
28
  return pd.read_csv(file)
 
 
 
29
  else:
30
+ logging.warning(f"Provided file path '{file}' is not valid. Using default CSV.")
31
+ if os.path.isfile(DEFAULT_CSV):
32
+ return pd.read_csv(DEFAULT_CSV)
33
+ else:
34
+ raise FileNotFoundError("No valid CSV file and default CSV not found.")
35
 
36
  def build_tree(df):
37
  employees = {}
 
172
  HTML(string=html_content).write_pdf(pdf_path)
173
  logging.info("PDF generated successfully.")
174
  except Exception as e:
175
+ logging.error(f"Error generating PDF: {e}")
176
  return None, f"Error generating PDF: {e}"
177
 
178
  try:
 
183
  else:
184
  image_path = ""
185
  except Exception as e:
186
+ logging.error(f"Error converting PDF to image: {e}")
187
  image_path = ""
188
 
189
  return image_path, pdf_path
190
 
191
  with gr.Blocks() as demo:
192
  gr.Markdown("## Organization Chart Generator")
193
+ gr.Markdown("Upload a CSV file (optional). If no file is uploaded or the file is invalid, the default CSV will be used.")
194
+
195
  file_input = gr.File(label="Upload CSV File (optional)", type="filepath")
196
  title_input = gr.Textbox(label="Enter PDF Title", placeholder="Company Org Chart")
197
  submit_button = gr.Button("Generate Chart")