Spaces:
Sleeping
Sleeping
File size: 12,487 Bytes
352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 352a4b6 f7b2e65 |
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
import logging
from flask import Blueprint, render_template, request, jsonify, send_from_directory
from pathlib import Path
import shutil # For zipping the cache directory
import json # For parsing streamed JSON data
import os
import config
import utils
from llm_client import make_chat_completion_request, is_initialized as llm_is_initialized
from cache_store import cache
from cache_store import cache_directory
import requests
logger = logging.getLogger(__name__)
main_bp = Blueprint('main', __name__)
# LLM client is initialized in app.py create_app()
# --- Serve the cache directory as a zip file ---
@main_bp.route('/download_cache')
def download_cache_zip():
"""μΊμ λλ ν 리λ₯Ό μμΆνμ¬ λ€μ΄λ‘λλ₯Ό μν΄ μ 곡ν©λλ€."""
zip_filename = "radexplain-cache.zip"
# Create the zip file in a temporary directory
# Using /tmp is common in containerized environments
temp_dir = "/tmp"
zip_base_path = os.path.join(temp_dir, "radexplain-cache") # shutil adds .zip
zip_filepath = zip_base_path + ".zip"
# Ensure the cache directory exists before trying to zip it
if not os.path.isdir(cache_directory):
logger.error(f"μΊμ λλ ν 리λ₯Ό μ°Ύμ μ μμ΅λλ€: {cache_directory}")
return jsonify({"error": f"μλ²μμ μΊμ λλ ν 리λ₯Ό μ°Ύμ μ μμ΅λλ€: {cache_directory}"}), 500
try:
logger.info(f"μΊμ λλ ν 리μ μμΆ νμΌ μμ± μ€: {cache_directory} -> {zip_filepath}")
shutil.make_archive(
zip_base_path, # This is the base name, shutil adds the .zip extension
"zip",
cache_directory, # This is the root directory to archive
)
logger.info("μμΆ νμΌμ΄ μ±κ³΅μ μΌλ‘ μμ±λμμ΅λλ€.")
# Send the file and then clean it up
return send_from_directory(temp_dir, zip_filename, as_attachment=True)
except Exception as e:
logger.error(f"μΊμ λλ ν 리 μμΆ νμΌ μμ± λλ μ μ‘ μ€ μ€λ₯ λ°μ: {e}", exc_info=True)
return jsonify({"error": f"μμΆ νμΌ μμ± λλ μ μ‘ μ€ μ€λ₯ λ°μ: {e}"}), 500
@main_bp.route('/')
def index():
"""λ©μΈ HTML νμ΄μ§λ₯Ό μ 곡ν©λλ€."""
# The backend now only provides the list of available reports.
# The frontend will be responsible for selecting a report,
# fetching its details (text, image path), and managing the current state.
if not config.AVAILABLE_REPORTS:
logger.warning("μ€μ μμ λ³΄κ³ μλ₯Ό μ°Ύμ μ μμ΅λλ€. AVAILABLE_REPORTSκ° λΉμ΄μμ΅λλ€.")
return render_template(
'index.html',
available_reports=config.AVAILABLE_REPORTS
)
@main_bp.route('/get_report_details/<report_name>')
def get_report_details(report_name):
"""μ£Όμ΄μ§ λ³΄κ³ μ μ΄λ¦μ λν ν
μ€νΈ λ΄μ©κ³Ό μ΄λ―Έμ§ κ²½λ‘λ₯Ό κ°μ Έμ΅λλ€."""
selected_report_info = next((item for item in config.AVAILABLE_REPORTS if item['name'] == report_name), None)
if not selected_report_info:
logger.error(f"μμΈ μ 보λ₯Ό κ°μ Έμ¬ λ λ³΄κ³ μ '{report_name}'μ(λ₯Ό) μ°Ύμ μ μμ΅λλ€.")
return jsonify({"error": f"λ³΄κ³ μ '{report_name}'μ(λ₯Ό) μ°Ύμ μ μμ΅λλ€."}), 404
report_file = selected_report_info.get('report_file')
image_file = selected_report_info.get('image_file')
report_text_content = "" # Default to empty if no report file is configured.
if report_file:
actual_server_report_path = config.BASE_DIR / report_file
try:
report_text_content = actual_server_report_path.read_text(encoding='utf-8').strip()
except Exception as e:
logger.error(f"λ³΄κ³ μ '{report_name}'μ νμΌ {actual_server_report_path} μ½κΈ° μ€λ₯: {e}", exc_info=True)
return jsonify({"error": "λ³΄κ³ μ νμΌμ μ½λ μ€ μ€λ₯κ° λ°μνμ΅λλ€."}), 500
# If report_file was empty, report_text_content remains "".
image_type_from_config = selected_report_info.get('image_type')
display_image_type = 'νλΆ X-λ μ΄' if image_type_from_config == 'CXR' else ('CT' if image_type_from_config == 'CT' else 'μλ£ μμ')
return jsonify({"text": report_text_content, "image_file": image_file, "image_type": display_image_type})
@main_bp.route('/explain', methods=['POST'])
def explain_sentence():
"""base64λ‘ μΈμ½λ©λ μ΄λ―Έμ§μ ν¨κ» LLM APIλ₯Ό μ¬μ©νμ¬ μ€λͺ
μμ²μ μ²λ¦¬ν©λλ€."""
if not llm_is_initialized():
logger.error("LLM ν΄λΌμ΄μΈνΈ(REST API)κ° μ΄κΈ°νλμ§ μμμ΅λλ€. μμ²μ μ²λ¦¬ν μ μμ΅λλ€.")
return jsonify({"error": "LLM ν΄λΌμ΄μΈνΈ(REST API)κ° μ΄κΈ°νλμ§ μμμ΅λλ€. API ν€μ λ² μ΄μ€ URLμ νμΈνμΈμ."}), 500
data = request.get_json()
if not data or 'sentence' not in data or 'report_name' not in data:
logger.warning("μμ² νμ΄λ‘λμ 'sentence' λλ 'report_name'μ΄ λλ½λμμ΅λλ€.")
return jsonify({"error": "μμ²μ 'sentence' λλ 'report_name'μ΄ λλ½λμμ΅λλ€"}), 400
selected_sentence = data['sentence']
report_name = data['report_name']
logger.info(f"μ€λͺ
μμ² μμ : '{selected_sentence}' (λ³΄κ³ μ: '{report_name}')")
# --- Find the selected report info ---
selected_report_info = next((item for item in config.AVAILABLE_REPORTS if item['name'] == report_name), None)
if not selected_report_info:
logger.error(f"μ¬μ© κ°λ₯ν λ³΄κ³ μμμ '{report_name}'μ(λ₯Ό) μ°Ύμ μ μμ΅λλ€.")
return jsonify({"error": f"λ³΄κ³ μ '{report_name}'μ(λ₯Ό) μ°Ύμ μ μμ΅λλ€."}), 404
image_file = selected_report_info.get('image_file')
report_file = selected_report_info.get('report_file')
image_type = selected_report_info.get('image_type')
if not image_file:
logger.error(f"λ³΄κ³ μ '{report_name}'μ μ€μ μμ μ΄λ―Έμ§ λλ λ³΄κ³ μ νμΌ κ²½λ‘(static κΈ°μ€ μλκ²½λ‘)κ° λλ½λμμ΅λλ€.")
return jsonify({"error": f"λ³΄κ³ μ '{report_name}'μ νμΌ μ€μ μ΄ λλ½λμμ΅λλ€."}), 500
# Construct absolute server paths using BASE_DIR as image_file and report_file include "static/"
server_image_path = config.BASE_DIR / image_file
# --- Prepare Base64 Image for API ---
if not server_image_path.is_file():
logger.error(f"μ΄λ―Έμ§ νμΌμ μ°Ύμ μ μμ΅λλ€: {server_image_path}")
return jsonify({"error": f"λ³΄κ³ μ '{report_name}'μ μ΄λ―Έμ§ νμΌμ μλ²μμ μ°Ύμ μ μμ΅λλ€."}), 500
base64_image_data_url = utils.image_to_base64_data_url(str(server_image_path))
if not base64_image_data_url:
logger.error("μ΄λ―Έμ§λ₯Ό base64λ‘ μΈμ½λ©νλλ° μ€ν¨νμ΅λλ€.")
return jsonify({"error": "API μμ²μ μν μ΄λ―Έμ§ μΈμ½λ©μ μ€ν¨νμ΅λλ€"}), 500
logger.info("APIλ₯Ό μν base64 λ°μ΄ν° URLλ‘ μ΄λ―Έμ§κ° μ±κ³΅μ μΌλ‘ μΈμ½λ©λμμ΅λλ€.")
full_report_text = ""
if report_file: # Only attempt to read if a report file is configured
server_report_path = config.BASE_DIR / report_file
try:
full_report_text = server_report_path.read_text(encoding='utf-8')
except FileNotFoundError:
logger.error(f"λ³΄κ³ μ νμΌμ μ°Ύμ μ μμ΅λλ€: {server_report_path}")
return jsonify({"error": f"λ³΄κ³ μ '{report_name}'μ νμΌμ μλ²μμ μ°Ύμ μ μμ΅λλ€."}), 500
except Exception as e:
logger.error(f"λ³΄κ³ μ νμΌ {server_report_path} μ½κΈ° μ€λ₯: {e}", exc_info=True)
return jsonify({"error": "λ³΄κ³ μ νμΌμ μ½λ μ€ μ€λ₯κ° λ°μνμ΅λλ€."}), 500
else: # If report_file is not configured (e.g. empty string from selected_report_info)
logger.info(f"λ³΄κ³ μ '{report_name}'μ λν λ³΄κ³ μ νμΌμ΄ μ€μ λμ§ μμμ΅λλ€. μμ€ν
ν둬ννΈμ μ 체 λ³΄κ³ μ ν
μ€νΈ μμ΄ μ§νν©λλ€.")
# μ΄λ―Έμ§ νμ
μ λ°λ₯Έ νκΈ νμ
image_type_korean = 'νλΆ X-λ μ΄' if image_type == 'CXR' else ('CT' if image_type == 'CT' else 'μλ£ μμ')
system_prompt = (
"λΉμ μ μΌλ°μΈμ λμμΌλ‘ μ€λͺ
νλ μμμμ
λλ€. "
f"νμ΅ μ€μΈ μ¬μ©μκ° λ°©μ¬μ λ³΄κ³ μμ ν λ¬Έμ₯μ μ 곡νκ³ ν¨κ» μ 곡λ {image_type_korean} μμμ λ³΄κ³ μμ΅λλ€. "
"λΉμ μ μ무λ μ 곡λ λ¬Έμ₯λ§μ μλ―Έλ₯Ό κ°λ¨νκ³ λͺ
νν μ©μ΄λ‘ μ€λͺ
νλ κ²μ
λλ€. μ λ¬Έ μ©μ΄μ μ½μ΄λ₯Ό μ€λͺ
νμΈμ. κ°κ²°νκ² μ μ§νμΈμ. "
"λ¬Έμ₯μ μλ―Έλ₯Ό μ§μ μ μΌλ‘ μ€λͺ
νμΈμ. 'λ€' λλ 'μκ² μ΅λλ€'μ κ°μ λμ
문ꡬλ₯Ό μ¬μ©νμ§ λ§κ³ , λ¬Έμ₯ μ체λ λ³΄κ³ μ μ체λ₯Ό μΈκΈνμ§ λ§μΈμ(μ: 'μ΄ λ¬Έμ₯μ...'). "
f"{f'μ€μν μ μ, μ¬μ©μκ° {image_type_korean} μμμ λ³΄κ³ μμΌλ―λ‘, ν΄λΉλλ κ²½μ° μ€λͺ
μ μ΄ν΄νκΈ° μν΄ μμμ μ΄λ λΆλΆμ λ΄μΌ νλμ§ μλ΄λ₯Ό μ 곡νμΈμ. ' if image_type != 'CT' else ''}"
"μ¬μ©μκ° λͺ
μμ μΌλ‘ μ 곡νμ§ μμ λ³΄κ³ μμ λ€λ₯Έ λΆλΆμ΄λ λ¬Έμ₯μ λν΄μλ λ
Όμνμ§ λ§μΈμ. ν
μ€νΈμ μλ μ¬μ€λ§μ κ³ μνμΈμ. μΆλ‘ νμ§ λ§μΈμ. \n"
"===\n"
f"μ°Έκ³ λ₯Ό μν μ 체 λ³΄κ³ μ:\n{full_report_text}"
)
user_prompt_text = f"λ°©μ¬μ λ³΄κ³ μμ μ΄ λ¬Έμ₯μ μ€λͺ
ν΄μ£ΌμΈμ: '{selected_sentence}'"
messages_for_api = [
{"role": "system", "content": system_prompt},
{
"role": "user",
"content": [
{"type": "text", "text": user_prompt_text}
]
}
]
cache_key = f"explain::{report_name}::{selected_sentence}"
cached_result = cache.get(cache_key)
if cached_result:
logger.info("μΊμλ μ€λͺ
μ λ°νν©λλ€.")
return jsonify({"explanation": cached_result})
try:
logger.info("base64 μ΄λ―Έμ§μ ν¨κ» LLM API(REST)λ‘ μμ²μ μ μ‘ μ€...")
response = make_chat_completion_request(
model="tgi",
messages=messages_for_api,
top_p=None,
temperature=0,
max_tokens=250,
stream=True,
seed=None,
stop=None,
frequency_penalty=None,
presence_penalty=None
)
logger.info("LLM API(REST)λ‘λΆν° μλ΅ μ€νΈλ¦Όμ μμ νμ΅λλ€.")
explanation_parts = []
for line in response.iter_lines():
if line:
decoded_line = line.decode('utf-8')
if decoded_line.startswith('data: '):
json_data_str = decoded_line[len('data: '):].strip()
if json_data_str == "[DONE]":
break
try:
chunk = json.loads(json_data_str)
if chunk.get("choices") and chunk["choices"][0].get("delta") and chunk["choices"][0]["delta"].get("content"):
explanation_parts.append(chunk["choices"][0]["delta"]["content"])
except json.JSONDecodeError:
logger.warning(f"μ€νΈλ¦Ό μ²ν¬μμ JSONμ λμ½λ©ν μ μμ΅λλ€: {json_data_str}")
# Depending on API, might need to handle partial JSON or other errors
elif decoded_line.strip() == "[DONE]": # Some APIs might send [DONE] without "data: "
break
explanation = "".join(explanation_parts).strip()
if explanation:
cache.set(cache_key, explanation, expire=None)
logger.info("μ€λͺ
μ΄ μ±κ³΅μ μΌλ‘ μμ±λμμ΅λλ€." if explanation else "APIλ‘λΆν° λΉ μ€λͺ
μ λ°μμ΅λλ€.")
return jsonify({"explanation": explanation or "APIλ‘λΆν° μ€λͺ
λ΄μ©μ λ°μ§ λͺ»νμ΅λλ€."})
except requests.exceptions.RequestException as e:
logger.error(f"LLM API(REST) νΈμΆ μ€ μ€λ₯ λ°μ: {e}", exc_info=True)
user_error_message = ("μ€λͺ
μμ±μ μ€ν¨νμ΅λλ€. μλΉμ€κ° μΌμμ μΌλ‘ μ¬μ©ν μ μμΌλ©° "
"νμ¬ μμ μ€μΌ μ μμ΅λλ€. μ μ ν λ€μ μλν΄ μ£ΌμΈμ.")
return jsonify({"error": user_error_message}), 500 |