mknolan commited on
Commit
bff9586
·
verified ·
1 Parent(s): c39dc7d

Upload app.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +52 -16
app.py CHANGED
@@ -20,10 +20,20 @@ import tempfile
20
  import logging
21
  import traceback
22
 
23
- # Set up logging
24
- LOG_DIR = "logs"
25
- os.makedirs(LOG_DIR, exist_ok=True)
26
- log_file = os.path.join(LOG_DIR, f"app_debug_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
 
 
 
 
 
 
 
 
 
 
27
 
28
  # Configure logging
29
  logging.basicConfig(
@@ -83,18 +93,6 @@ def log_tensor_info(tensor, name="tensor"):
83
  except Exception as e:
84
  logger.error(f"Error logging tensor info for {name}: {str(e)}")
85
 
86
- # Constants
87
- IMAGENET_MEAN = (0.485, 0.456, 0.406)
88
- IMAGENET_STD = (0.229, 0.224, 0.225)
89
-
90
- # Configuration
91
- MODEL_NAME = "OpenGVLab/InternVL2_5-8B" # Smaller model for faster loading
92
- IMAGE_SIZE = 448
93
- OUTPUT_DIR = "saved_outputs" # Changed to a visible repo directory
94
-
95
- # Create output directory if it doesn't exist
96
- os.makedirs(OUTPUT_DIR, exist_ok=True)
97
-
98
  # Set up environment variables
99
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
100
 
@@ -1410,6 +1408,23 @@ def process_image_with_text(image, prompt):
1410
  logger.error(traceback.format_exc())
1411
  return f"Error processing image: {str(e)}"
1412
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1413
  # Main function
1414
  def main():
1415
  # Load the model
@@ -1538,6 +1553,27 @@ def main():
1538
  outputs=save_status_folder
1539
  )
1540
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1541
  # New tab for saved output files
1542
  with gr.Tab("Saved Outputs"):
1543
  refresh_btn = gr.Button("Refresh File List")
 
20
  import logging
21
  import traceback
22
 
23
+ # Constants
24
+ IMAGENET_MEAN = (0.485, 0.456, 0.406)
25
+ IMAGENET_STD = (0.229, 0.224, 0.225)
26
+
27
+ # Configuration
28
+ MODEL_NAME = "OpenGVLab/InternVL2_5-8B" # Smaller model for faster loading
29
+ IMAGE_SIZE = 448
30
+ OUTPUT_DIR = "saved_outputs" # Changed to a visible repo directory
31
+
32
+ # Create output directory if it doesn't exist
33
+ os.makedirs(OUTPUT_DIR, exist_ok=True)
34
+
35
+ # Set up logging to write to saved_outputs directory
36
+ log_file = os.path.join(OUTPUT_DIR, f"debug_log_{datetime.datetime.now().strftime('%Y%m%d_%H%M%S')}.log")
37
 
38
  # Configure logging
39
  logging.basicConfig(
 
93
  except Exception as e:
94
  logger.error(f"Error logging tensor info for {name}: {str(e)}")
95
 
 
 
 
 
 
 
 
 
 
 
 
 
96
  # Set up environment variables
97
  os.environ["PYTORCH_CUDA_ALLOC_CONF"] = "max_split_size_mb:128"
98
 
 
1408
  logger.error(traceback.format_exc())
1409
  return f"Error processing image: {str(e)}"
1410
 
1411
+ # Function to get log file content
1412
+ def get_latest_log_content():
1413
+ """Get the content of the latest log file for display in the UI."""
1414
+ try:
1415
+ log_files = sorted(glob.glob(os.path.join(OUTPUT_DIR, "debug_log_*.log")))
1416
+ if not log_files:
1417
+ return "No log files found."
1418
+
1419
+ latest_log = log_files[-1]
1420
+ with open(latest_log, 'r') as f:
1421
+ # Get the last 100 lines (most recent logs)
1422
+ lines = f.readlines()
1423
+ last_lines = lines[-100:] if len(lines) > 100 else lines
1424
+ return "".join(last_lines)
1425
+ except Exception as e:
1426
+ return f"Error reading log file: {str(e)}"
1427
+
1428
  # Main function
1429
  def main():
1430
  # Load the model
 
1553
  outputs=save_status_folder
1554
  )
1555
 
1556
+ # Add a new tab for logs
1557
+ with gr.Tab("Debug Logs"):
1558
+ gr.Markdown("## Debug Logs")
1559
+ gr.Markdown("View the latest application logs to diagnose issues:")
1560
+
1561
+ log_output = gr.Textbox(
1562
+ value=get_latest_log_content(),
1563
+ lines=20,
1564
+ label="Latest Log Entries",
1565
+ elem_id="log_output"
1566
+ )
1567
+
1568
+ refresh_btn = gr.Button("Refresh Logs")
1569
+ refresh_btn.click(
1570
+ fn=get_latest_log_content,
1571
+ inputs=[],
1572
+ outputs=[log_output]
1573
+ )
1574
+
1575
+ gr.Markdown("**Note**: Log files are also saved to the `saved_outputs` directory and can be accessed via the Files tab.")
1576
+
1577
  # New tab for saved output files
1578
  with gr.Tab("Saved Outputs"):
1579
  refresh_btn = gr.Button("Refresh File List")