seawolf2357 commited on
Commit
6ae096f
ยท
verified ยท
1 Parent(s): f7b2e65

Update config.py

Browse files
Files changed (1) hide show
  1. config.py +36 -37
config.py CHANGED
@@ -1,23 +1,23 @@
1
  import os
2
  from pathlib import Path
3
- import csv # Import the csv module
4
  import logging
5
 
6
- # --- Configuration ---
7
- # Configure basic logging (optional, adjust as needed)
8
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
9
  logger = logging.getLogger(__name__)
10
 
11
- # API Configuration
12
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
13
  MEDGEMMA_ENDPOINT_URL = os.environ.get("MEDGEMMA_ENDPOINT_URL", None)
14
 
15
- # --- Paths using pathlib ---
16
- # Determine the base directory of the application
17
- BASE_DIR = Path(__file__).parent.resolve() # Use resolve() for absolute path
18
  STATIC_DIR = BASE_DIR / 'static'
19
 
20
- # --- Load available report/image pairs from CSV ---
21
  AVAILABLE_REPORTS = []
22
  MANIFEST_CSV_PATH = STATIC_DIR / 'reports_manifest.csv'
23
 
@@ -25,69 +25,68 @@ if MANIFEST_CSV_PATH.is_file():
25
  try:
26
  with open(MANIFEST_CSV_PATH, mode='r', encoding='utf-8') as csvfile:
27
  reader = csv.DictReader(csvfile)
28
- # Expected CSV headers: 'image_type', 'case_display_name', 'image_path', 'report_path'
29
  required_headers = {'case_display_name', 'image_path', 'report_path'}
30
  if not required_headers.issubset(reader.fieldnames):
31
  logger.error(
32
- f"CSV file {MANIFEST_CSV_PATH} is missing one or more required headers: {required_headers - set(reader.fieldnames)}"
33
  )
34
  else:
35
  for row in reader:
36
  case_name = row['case_display_name']
37
- image_path_from_csv = row['image_path'] # e.g., static/images/report1.jpg
38
- report_path_from_csv = row['report_path'] # e.g., static/reports/report1.txt or empty
39
 
40
- # Validate image_path_from_csv (must not be empty)
41
  if not image_path_from_csv:
42
- logger.warning(f"Empty image_path in CSV for case '{case_name}'. Skipping this entry.")
43
  continue
44
 
45
- # Construct absolute path for image file validation (paths from CSV are relative to BASE_DIR)
46
  abs_image_path_to_check = BASE_DIR / image_path_from_csv
47
  if not abs_image_path_to_check.is_file():
48
- logger.warning(f"Image file not found for case '{case_name}' at '{abs_image_path_to_check}'. Skipping this entry.")
49
  continue
50
 
51
-
52
  image_file_for_config = image_path_from_csv
53
 
54
- report_file_for_config = "" # Default to empty if no report or error
55
- if report_path_from_csv: # Report path is optional
56
- # Construct absolute path for report file validation (paths from CSV are relative to BASE_DIR)
57
  abs_report_path_to_check = BASE_DIR / report_path_from_csv
58
  if not abs_report_path_to_check.is_file():
59
  logger.warning(
60
- f"Report file specified for case '{case_name}' at '{abs_report_path_to_check}' not found. "
61
- f"Proceeding without report file for this entry."
62
  )
63
- # report_file_for_config remains ""
64
  else:
65
- # The file (BASE_DIR / report_path_from_csv) exists.
66
- # Now, ensure report_path_from_csv string itself starts with "static/"
67
- # as per the assumption about CSV content.
68
  if report_path_from_csv.startswith('static/') or report_path_from_csv.startswith('static\\'):
69
- # Path is well-formed (starts with static/) and file exists.
70
- # Store the path as is (e.g., "static/reports/report1.txt").
71
  report_file_for_config = report_path_from_csv
72
  else:
73
  logger.warning(
74
- f"Report path '{report_path_from_csv}' for case '{case_name}' in CSV "
75
- f"is malformed (does not start with 'static/'). Treating as if no report path was specified."
76
  )
77
- # report_file_for_config remains ""
78
  AVAILABLE_REPORTS.append({
79
  "name": case_name,
80
  "image_file": image_file_for_config, # static/images/report1.jpg
81
- "report_file": report_file_for_config, # static/reports/report1.txt or ""
82
  "image_type": row['image_type']
83
  })
84
  AVAILABLE_REPORTS.sort(key=lambda x: x['name'])
85
- logger.info(f"Loaded {len(AVAILABLE_REPORTS)} report/image pairs from CSV.")
86
 
87
  except Exception as e:
88
- logger.error(f"Error reading or processing CSV file {MANIFEST_CSV_PATH}: {e}", exc_info=True)
89
  else:
90
- logger.warning(f"Manifest CSV file not found at {MANIFEST_CSV_PATH}. AVAILABLE_REPORTS will be empty.")
91
 
92
- # --- Optional: Define a default report if needed ---
93
- DEFAULT_REPORT_INFO = AVAILABLE_REPORTS[0] if AVAILABLE_REPORTS else None
 
1
  import os
2
  from pathlib import Path
3
+ import csv # CSV ๋ชจ๋“ˆ ์ž„ํฌํŠธ
4
  import logging
5
 
6
+ # --- ์„ค์ • ---
7
+ # ๊ธฐ๋ณธ ๋กœ๊น… ์„ค์ • (์„ ํƒ์‚ฌํ•ญ, ํ•„์š”์— ๋”ฐ๋ผ ์กฐ์ •)
8
  logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
9
  logger = logging.getLogger(__name__)
10
 
11
+ # API ์„ค์ •
12
  HF_TOKEN = os.environ.get("HF_TOKEN", None)
13
  MEDGEMMA_ENDPOINT_URL = os.environ.get("MEDGEMMA_ENDPOINT_URL", None)
14
 
15
+ # --- pathlib์„ ์‚ฌ์šฉํ•œ ๊ฒฝ๋กœ ์„ค์ • ---
16
+ # ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ธฐ๋ณธ ๋””๋ ‰ํ† ๋ฆฌ ๊ฒฐ์ •
17
+ BASE_DIR = Path(__file__).parent.resolve() # ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ฅผ ์œ„ํ•ด resolve() ์‚ฌ์šฉ
18
  STATIC_DIR = BASE_DIR / 'static'
19
 
20
+ # --- CSV์—์„œ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ๋ณด๊ณ ์„œ/์ด๋ฏธ์ง€ ์Œ ๋กœ๋“œ ---
21
  AVAILABLE_REPORTS = []
22
  MANIFEST_CSV_PATH = STATIC_DIR / 'reports_manifest.csv'
23
 
 
25
  try:
26
  with open(MANIFEST_CSV_PATH, mode='r', encoding='utf-8') as csvfile:
27
  reader = csv.DictReader(csvfile)
28
+ # ์˜ˆ์ƒ๋˜๋Š” CSV ํ—ค๋”: 'image_type', 'case_display_name', 'image_path', 'report_path'
29
  required_headers = {'case_display_name', 'image_path', 'report_path'}
30
  if not required_headers.issubset(reader.fieldnames):
31
  logger.error(
32
+ f"CSV ํŒŒ์ผ {MANIFEST_CSV_PATH}์— ๋‹ค์Œ ํ•„์ˆ˜ ํ—ค๋”๊ฐ€ ๋ˆ„๋ฝ๋˜์—ˆ์Šต๋‹ˆ๋‹ค: {required_headers - set(reader.fieldnames)}"
33
  )
34
  else:
35
  for row in reader:
36
  case_name = row['case_display_name']
37
+ image_path_from_csv = row['image_path'] # ์˜ˆ: static/images/report1.jpg
38
+ report_path_from_csv = row['report_path'] # ์˜ˆ: static/reports/report1.txt ๋˜๋Š” ๋นˆ ๋ฌธ์ž์—ด
39
 
40
+ # image_path_from_csv ์œ ํšจ์„ฑ ๊ฒ€์‚ฌ (๋น„์–ด์žˆ์œผ๋ฉด ์•ˆ ๋จ)
41
  if not image_path_from_csv:
42
+ logger.warning(f"์ผ€์ด์Šค '{case_name}'์˜ CSV์—์„œ image_path๊ฐ€ ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค. ์ด ํ•ญ๋ชฉ์„ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
43
  continue
44
 
45
+ # ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฒ€์ฆ์„ ์œ„ํ•œ ์ ˆ๋Œ€ ๊ฒฝ๋กœ ๊ตฌ์„ฑ (CSV์˜ ๊ฒฝ๋กœ๋Š” BASE_DIR ๊ธฐ์ค€ ์ƒ๋Œ€ ๊ฒฝ๋กœ)
46
  abs_image_path_to_check = BASE_DIR / image_path_from_csv
47
  if not abs_image_path_to_check.is_file():
48
+ logger.warning(f"์ผ€์ด์Šค '{case_name}'์˜ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ '{abs_image_path_to_check}'์—์„œ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ์ด ํ•ญ๋ชฉ์„ ๊ฑด๋„ˆ๋œ๋‹ˆ๋‹ค.")
49
  continue
50
 
 
51
  image_file_for_config = image_path_from_csv
52
 
53
+ report_file_for_config = "" # ๋ณด๊ณ ์„œ๊ฐ€ ์—†๊ฑฐ๋‚˜ ์˜ค๋ฅ˜๊ฐ€ ์žˆ์œผ๋ฉด ๊ธฐ๋ณธ๊ฐ’์€ ๋นˆ ๋ฌธ์ž์—ด
54
+ if report_path_from_csv: # ๋ณด๊ณ ์„œ ๊ฒฝ๋กœ๋Š” ์„ ํƒ์‚ฌํ•ญ
55
+ # ๋ณด๊ณ ์„œ ํŒŒ์ผ ๊ฒ€์ฆ์„ ์œ„ํ•œ ์ ˆ๋Œ€ ๊ฒฝ๋กœ ๊ตฌ์„ฑ (CSV์˜ ๊ฒฝ๋กœ๋Š” BASE_DIR ๊ธฐ์ค€ ์ƒ๋Œ€ ๊ฒฝ๋กœ)
56
  abs_report_path_to_check = BASE_DIR / report_path_from_csv
57
  if not abs_report_path_to_check.is_file():
58
  logger.warning(
59
+ f"์ผ€์ด์Šค '{case_name}'์— ์ง€์ •๋œ ๋ณด๊ณ ์„œ ํŒŒ์ผ '{abs_report_path_to_check}'์„(๋ฅผ) ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. "
60
+ f"์ด ๏ฟฝ๏ฟฝ๏ฟฝ๋ชฉ์— ๋Œ€ํ•ด ๋ณด๊ณ ์„œ ํŒŒ์ผ ์—†์ด ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค."
61
  )
62
+ # report_file_for_config๋Š” ""๋กœ ์œ ์ง€
63
  else:
64
+ # ํŒŒ์ผ (BASE_DIR / report_path_from_csv)์ด ์กด์žฌํ•จ.
65
+ # ์ด์ œ report_path_from_csv ๋ฌธ์ž์—ด ์ž์ฒด๊ฐ€ "static/"์œผ๋กœ ์‹œ์ž‘ํ•˜๋Š”์ง€ ํ™•์ธ
66
+ # CSV ๋‚ด์šฉ์— ๋Œ€ํ•œ ๊ฐ€์ •์— ๋”ฐ๋ผ.
67
  if report_path_from_csv.startswith('static/') or report_path_from_csv.startswith('static\\'):
68
+ # ๊ฒฝ๋กœ๊ฐ€ ์˜ฌ๋ฐ”๋ฅธ ํ˜•์‹(static/์œผ๋กœ ์‹œ์ž‘)์ด๊ณ  ํŒŒ์ผ์ด ์กด์žฌํ•จ.
69
+ # ๊ฒฝ๋กœ๋ฅผ ๊ทธ๋Œ€๋กœ ์ €์žฅ (์˜ˆ: "static/reports/report1.txt").
70
  report_file_for_config = report_path_from_csv
71
  else:
72
  logger.warning(
73
+ f"CSV์˜ ์ผ€์ด์Šค '{case_name}'์— ๋Œ€ํ•œ ๋ณด๊ณ ์„œ ๊ฒฝ๋กœ '{report_path_from_csv}'๊ฐ€ "
74
+ f"์ž˜๋ชป๋œ ํ˜•์‹์ž…๋‹ˆ๋‹ค ('static/'์œผ๋กœ ์‹œ์ž‘ํ•˜์ง€ ์•Š์Œ). ๋ณด๊ณ ์„œ ๊ฒฝ๋กœ๊ฐ€ ์ง€์ •๋˜์ง€ ์•Š์€ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค."
75
  )
76
+ # report_file_for_config๋Š” ""๋กœ ์œ ์ง€
77
  AVAILABLE_REPORTS.append({
78
  "name": case_name,
79
  "image_file": image_file_for_config, # static/images/report1.jpg
80
+ "report_file": report_file_for_config, # static/reports/report1.txt ๋˜๋Š” ""
81
  "image_type": row['image_type']
82
  })
83
  AVAILABLE_REPORTS.sort(key=lambda x: x['name'])
84
+ logger.info(f"CSV์—์„œ {len(AVAILABLE_REPORTS)}๊ฐœ์˜ ๋ณด๊ณ ์„œ/์ด๋ฏธ์ง€ ์Œ์„ ๋กœ๋“œํ–ˆ์Šต๋‹ˆ๋‹ค.")
85
 
86
  except Exception as e:
87
+ logger.error(f"CSV ํŒŒ์ผ {MANIFEST_CSV_PATH} ์ฝ๊ธฐ ๋˜๋Š” ์ฒ˜๋ฆฌ ์ค‘ ์˜ค๋ฅ˜ ๋ฐœ์ƒ: {e}", exc_info=True)
88
  else:
89
+ logger.warning(f"{MANIFEST_CSV_PATH}์—์„œ ๋งค๋‹ˆํŽ˜์ŠคํŠธ CSV ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. AVAILABLE_REPORTS๊ฐ€ ๋น„์–ด์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.")
90
 
91
+ # --- ์„ ํƒ์‚ฌํ•ญ: ํ•„์š”ํ•œ ๊ฒฝ์šฐ ๊ธฐ๋ณธ ๋ณด๊ณ ์„œ ์ •์˜ ---
92
+ DEFAULT_REPORT_INFO = AVAILABLE_REPORTS[0] if AVAILABLE_REPORTS else None