Wajahat698 commited on
Commit
9274bd2
·
verified ·
1 Parent(s): ffedfe9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -13
app.py CHANGED
@@ -12,6 +12,7 @@ import numpy as np
12
  from sklearn.linear_model import LinearRegression
13
  from sklearn.metrics import r2_score
14
  import base64
 
15
 
16
  # Initialize logging
17
  logging.basicConfig(level=logging.INFO)
@@ -280,15 +281,42 @@ def create_error_message(message):
280
  </div>
281
  """
282
 
283
- def call_r_script_simplified(input_file, csv_output_path):
284
  """
285
- Call R script for Shapley regression analysis on Consideration.
286
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
287
  command = [
288
  "Rscript",
289
  "process_data.R",
290
  input_file,
291
- csv_output_path
 
 
 
 
 
 
 
 
 
 
 
292
  ]
293
 
294
  try:
@@ -382,13 +410,13 @@ def analyze_prospects_data(file_path):
382
  temp_dir = tempfile.mkdtemp()
383
  csv_output_path = os.path.join(temp_dir, "consideration_results.csv")
384
 
385
- # Call R script
386
- r_success = call_r_script_simplified(file_path, csv_output_path)
387
 
388
  if not r_success:
389
  # Clean up and return error
390
  try:
391
- os.rmdir(temp_dir)
392
  except:
393
  pass
394
  return create_error_message("R script failed to execute. Shapley analysis not available."), None, None, None
@@ -396,7 +424,7 @@ def analyze_prospects_data(file_path):
396
  # Check if R script produced output file
397
  if not os.path.exists(csv_output_path):
398
  try:
399
- os.rmdir(temp_dir)
400
  except:
401
  pass
402
  return create_error_message("R script did not produce expected output file."), None, None, None
@@ -407,8 +435,7 @@ def analyze_prospects_data(file_path):
407
  except Exception as e:
408
  logger.error(f"Error reading R script output: {e}")
409
  try:
410
- os.remove(csv_output_path)
411
- os.rmdir(temp_dir)
412
  except:
413
  pass
414
  return create_error_message(f"Error reading R script output: {e}"), None, None, None
@@ -417,8 +444,7 @@ def analyze_prospects_data(file_path):
417
  if "Predictor" not in results_df.columns or "Importance" not in results_df.columns:
418
  logger.error("R script output missing required columns")
419
  try:
420
- os.remove(csv_output_path)
421
- os.rmdir(temp_dir)
422
  except:
423
  pass
424
  return create_error_message("R script output is invalid - missing required columns."), None, None, None
@@ -440,8 +466,7 @@ def analyze_prospects_data(file_path):
440
 
441
  # Clean up
442
  try:
443
- os.remove(csv_output_path)
444
- os.rmdir(temp_dir)
445
  except Exception as e:
446
  logger.error(f"Error cleaning up temp files: {e}")
447
 
 
12
  from sklearn.linear_model import LinearRegression
13
  from sklearn.metrics import r2_score
14
  import base64
15
+ import shutil
16
 
17
  # Initialize logging
18
  logging.basicConfig(level=logging.INFO)
 
281
  </div>
282
  """
283
 
284
+ def call_r_script_for_consideration(input_file, csv_output_path):
285
  """
286
+ Call R script for Shapley regression analysis specifically for Consideration.
287
  """
288
+ # Create temporary files for all outputs (even though we only need consideration)
289
+ temp_dir = os.path.dirname(csv_output_path)
290
+ text_output_path = os.path.join(temp_dir, "output.txt")
291
+ csv_output_path_trust = os.path.join(temp_dir, "trust.csv")
292
+ csv_output_path_nps = os.path.join(temp_dir, "nps.csv")
293
+ csv_output_path_loyalty = os.path.join(temp_dir, "loyalty.csv")
294
+ csv_output_path_satisfaction = os.path.join(temp_dir, "satisfaction.csv")
295
+ csv_output_path_trustbuilder = os.path.join(temp_dir, "trustbuilder.csv")
296
+
297
+ # Set the boolean flags - we only want consideration analysis
298
+ nps_present = False
299
+ loyalty_present = False
300
+ consideration_present = True # This is what we want
301
+ satisfaction_present = False
302
+ trustbuilder_present = False
303
+
304
  command = [
305
  "Rscript",
306
  "process_data.R",
307
  input_file,
308
+ text_output_path,
309
+ csv_output_path_trust,
310
+ csv_output_path_nps,
311
+ csv_output_path_loyalty,
312
+ csv_output_path, # This is our consideration output
313
+ csv_output_path_satisfaction,
314
+ csv_output_path_trustbuilder,
315
+ str(nps_present).upper(), # Convert to "TRUE"/"FALSE"
316
+ str(loyalty_present).upper(),
317
+ str(consideration_present).upper(),
318
+ str(satisfaction_present).upper(),
319
+ str(trustbuilder_present).upper(),
320
  ]
321
 
322
  try:
 
410
  temp_dir = tempfile.mkdtemp()
411
  csv_output_path = os.path.join(temp_dir, "consideration_results.csv")
412
 
413
+ # Call R script with proper parameters
414
+ r_success = call_r_script_for_consideration(file_path, csv_output_path)
415
 
416
  if not r_success:
417
  # Clean up and return error
418
  try:
419
+ shutil.rmtree(temp_dir)
420
  except:
421
  pass
422
  return create_error_message("R script failed to execute. Shapley analysis not available."), None, None, None
 
424
  # Check if R script produced output file
425
  if not os.path.exists(csv_output_path):
426
  try:
427
+ shutil.rmtree(temp_dir)
428
  except:
429
  pass
430
  return create_error_message("R script did not produce expected output file."), None, None, None
 
435
  except Exception as e:
436
  logger.error(f"Error reading R script output: {e}")
437
  try:
438
+ shutil.rmtree(temp_dir)
 
439
  except:
440
  pass
441
  return create_error_message(f"Error reading R script output: {e}"), None, None, None
 
444
  if "Predictor" not in results_df.columns or "Importance" not in results_df.columns:
445
  logger.error("R script output missing required columns")
446
  try:
447
+ shutil.rmtree(temp_dir)
 
448
  except:
449
  pass
450
  return create_error_message("R script output is invalid - missing required columns."), None, None, None
 
466
 
467
  # Clean up
468
  try:
469
+ shutil.rmtree(temp_dir)
 
470
  except Exception as e:
471
  logger.error(f"Error cleaning up temp files: {e}")
472