Spaces:
Sleeping
Sleeping
File size: 5,615 Bytes
352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f 352a4b6 6ae096f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
import os
from pathlib import Path
import csv # CSV ๋ชจ๋ ์ํฌํธ
import logging
# --- ์ค์ ---
# ๊ธฐ๋ณธ ๋ก๊น
์ค์ (์ ํ์ฌํญ, ํ์์ ๋ฐ๋ผ ์กฐ์ )
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# API ์ค์
HF_TOKEN = os.environ.get("HF_TOKEN", None)
MEDGEMMA_ENDPOINT_URL = os.environ.get("MEDGEMMA_ENDPOINT_URL", None)
# --- pathlib์ ์ฌ์ฉํ ๊ฒฝ๋ก ์ค์ ---
# ์ ํ๋ฆฌ์ผ์ด์
์ ๊ธฐ๋ณธ ๋๋ ํ ๋ฆฌ ๊ฒฐ์
BASE_DIR = Path(__file__).parent.resolve() # ์ ๋ ๊ฒฝ๋ก๋ฅผ ์ํด resolve() ์ฌ์ฉ
STATIC_DIR = BASE_DIR / 'static'
# --- CSV์์ ์ฌ์ฉ ๊ฐ๋ฅํ ๋ณด๊ณ ์/์ด๋ฏธ์ง ์ ๋ก๋ ---
AVAILABLE_REPORTS = []
MANIFEST_CSV_PATH = STATIC_DIR / 'reports_manifest.csv'
if MANIFEST_CSV_PATH.is_file():
try:
with open(MANIFEST_CSV_PATH, mode='r', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
# ์์๋๋ CSV ํค๋: 'image_type', 'case_display_name', 'image_path', 'report_path'
required_headers = {'case_display_name', 'image_path', 'report_path'}
if not required_headers.issubset(reader.fieldnames):
logger.error(
f"CSV ํ์ผ {MANIFEST_CSV_PATH}์ ๋ค์ ํ์ ํค๋๊ฐ ๋๋ฝ๋์์ต๋๋ค: {required_headers - set(reader.fieldnames)}"
)
else:
for row in reader:
case_name = row['case_display_name']
image_path_from_csv = row['image_path'] # ์: static/images/report1.jpg
report_path_from_csv = row['report_path'] # ์: static/reports/report1.txt ๋๋ ๋น ๋ฌธ์์ด
# image_path_from_csv ์ ํจ์ฑ ๊ฒ์ฌ (๋น์ด์์ผ๋ฉด ์ ๋จ)
if not image_path_from_csv:
logger.warning(f"์ผ์ด์ค '{case_name}'์ CSV์์ image_path๊ฐ ๋น์ด์์ต๋๋ค. ์ด ํญ๋ชฉ์ ๊ฑด๋๋๋๋ค.")
continue
# ์ด๋ฏธ์ง ํ์ผ ๊ฒ์ฆ์ ์ํ ์ ๋ ๊ฒฝ๋ก ๊ตฌ์ฑ (CSV์ ๊ฒฝ๋ก๋ BASE_DIR ๊ธฐ์ค ์๋ ๊ฒฝ๋ก)
abs_image_path_to_check = BASE_DIR / image_path_from_csv
if not abs_image_path_to_check.is_file():
logger.warning(f"์ผ์ด์ค '{case_name}'์ ์ด๋ฏธ์ง ํ์ผ์ '{abs_image_path_to_check}'์์ ์ฐพ์ ์ ์์ต๋๋ค. ์ด ํญ๋ชฉ์ ๊ฑด๋๋๋๋ค.")
continue
image_file_for_config = image_path_from_csv
report_file_for_config = "" # ๋ณด๊ณ ์๊ฐ ์๊ฑฐ๋ ์ค๋ฅ๊ฐ ์์ผ๋ฉด ๊ธฐ๋ณธ๊ฐ์ ๋น ๋ฌธ์์ด
if report_path_from_csv: # ๋ณด๊ณ ์ ๊ฒฝ๋ก๋ ์ ํ์ฌํญ
# ๋ณด๊ณ ์ ํ์ผ ๊ฒ์ฆ์ ์ํ ์ ๋ ๊ฒฝ๋ก ๊ตฌ์ฑ (CSV์ ๊ฒฝ๋ก๋ BASE_DIR ๊ธฐ์ค ์๋ ๊ฒฝ๋ก)
abs_report_path_to_check = BASE_DIR / report_path_from_csv
if not abs_report_path_to_check.is_file():
logger.warning(
f"์ผ์ด์ค '{case_name}'์ ์ง์ ๋ ๋ณด๊ณ ์ ํ์ผ '{abs_report_path_to_check}'์(๋ฅผ) ์ฐพ์ ์ ์์ต๋๋ค. "
f"์ด ํญ๋ชฉ์ ๋ํด ๋ณด๊ณ ์ ํ์ผ ์์ด ์งํํฉ๋๋ค."
)
# report_file_for_config๋ ""๋ก ์ ์ง
else:
# ํ์ผ (BASE_DIR / report_path_from_csv)์ด ์กด์ฌํจ.
# ์ด์ report_path_from_csv ๋ฌธ์์ด ์์ฒด๊ฐ "static/"์ผ๋ก ์์ํ๋์ง ํ์ธ
# CSV ๋ด์ฉ์ ๋ํ ๊ฐ์ ์ ๋ฐ๋ผ.
if report_path_from_csv.startswith('static/') or report_path_from_csv.startswith('static\\'):
# ๊ฒฝ๋ก๊ฐ ์ฌ๋ฐ๋ฅธ ํ์(static/์ผ๋ก ์์)์ด๊ณ ํ์ผ์ด ์กด์ฌํจ.
# ๊ฒฝ๋ก๋ฅผ ๊ทธ๋๋ก ์ ์ฅ (์: "static/reports/report1.txt").
report_file_for_config = report_path_from_csv
else:
logger.warning(
f"CSV์ ์ผ์ด์ค '{case_name}'์ ๋ํ ๋ณด๊ณ ์ ๊ฒฝ๋ก '{report_path_from_csv}'๊ฐ "
f"์๋ชป๋ ํ์์
๋๋ค ('static/'์ผ๋ก ์์ํ์ง ์์). ๋ณด๊ณ ์ ๊ฒฝ๋ก๊ฐ ์ง์ ๋์ง ์์ ๊ฒ์ผ๋ก ์ฒ๋ฆฌํฉ๋๋ค."
)
# report_file_for_config๋ ""๋ก ์ ์ง
AVAILABLE_REPORTS.append({
"name": case_name,
"image_file": image_file_for_config, # static/images/report1.jpg
"report_file": report_file_for_config, # static/reports/report1.txt ๋๋ ""
"image_type": row['image_type']
})
AVAILABLE_REPORTS.sort(key=lambda x: x['name'])
logger.info(f"CSV์์ {len(AVAILABLE_REPORTS)}๊ฐ์ ๋ณด๊ณ ์/์ด๋ฏธ์ง ์์ ๋ก๋ํ์ต๋๋ค.")
except Exception as e:
logger.error(f"CSV ํ์ผ {MANIFEST_CSV_PATH} ์ฝ๊ธฐ ๋๋ ์ฒ๋ฆฌ ์ค ์ค๋ฅ ๋ฐ์: {e}", exc_info=True)
else:
logger.warning(f"{MANIFEST_CSV_PATH}์์ ๋งค๋ํ์คํธ CSV ํ์ผ์ ์ฐพ์ ์ ์์ต๋๋ค. AVAILABLE_REPORTS๊ฐ ๋น์ด์๊ฒ ๋ฉ๋๋ค.")
# --- ์ ํ์ฌํญ: ํ์ํ ๊ฒฝ์ฐ ๊ธฐ๋ณธ ๋ณด๊ณ ์ ์ ์ ---
DEFAULT_REPORT_INFO = AVAILABLE_REPORTS[0] if AVAILABLE_REPORTS else None |