Spaces:
Sleeping
Sleeping
fixed problems
Browse files- .gitignore +1 -1
- src/app.py +39 -26
.gitignore
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
venv/
|
| 2 |
.venv/
|
| 3 |
__pycache__
|
| 4 |
-
|
|
|
|
| 1 |
venv/
|
| 2 |
.venv/
|
| 3 |
__pycache__
|
| 4 |
+
.cache/
|
src/app.py
CHANGED
|
@@ -82,31 +82,45 @@ BASE_DIR = os.path.dirname(os.path.dirname(__file__))
|
|
| 82 |
MODEL_DIR = os.path.join(BASE_DIR, "models")
|
| 83 |
MODEL_PATH = os.path.join(MODEL_DIR, MODEL_NAME)
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
filename=MODEL_NAME,
|
| 100 |
-
local_dir=MODEL_DIR,
|
| 101 |
-
local_dir_use_symlinks=False
|
| 102 |
-
)
|
| 103 |
-
print(f"Successfully downloaded model to {model_path}")
|
| 104 |
-
except Exception as e:
|
| 105 |
-
raise RuntimeError(f"Failed to download model: {str(e)}")
|
| 106 |
-
else:
|
| 107 |
-
print(f"Model already exists at {model_path}")
|
| 108 |
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 110 |
|
| 111 |
# Ensure model is downloaded
|
| 112 |
model_path = ensure_model()
|
|
@@ -225,7 +239,7 @@ def format_response_for_user(response_dict):
|
|
| 225 |
return message
|
| 226 |
|
| 227 |
# Build enhanced Gradio interface
|
| 228 |
-
with gr.Blocks(
|
| 229 |
gr.Markdown("""
|
| 230 |
# 🏥 Medical Symptom to ICD-10 Code Assistant
|
| 231 |
|
|
@@ -248,8 +262,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 248 |
chatbot = gr.Chatbot(
|
| 249 |
label="Medical Consultation",
|
| 250 |
height=500,
|
| 251 |
-
container=True
|
| 252 |
-
bubble=True
|
| 253 |
)
|
| 254 |
|
| 255 |
with gr.Row():
|
|
|
|
| 82 |
MODEL_DIR = os.path.join(BASE_DIR, "models")
|
| 83 |
MODEL_PATH = os.path.join(MODEL_DIR, MODEL_NAME)
|
| 84 |
|
| 85 |
+
from typing import Optional
|
| 86 |
+
|
| 87 |
+
def ensure_model(model_name: Optional[str] = None, repo_id: Optional[str] = None) -> str:
|
| 88 |
+
"""Ensures model is available, downloading only if needed."""
|
| 89 |
|
| 90 |
+
# Use HF Spaces cache directory
|
| 91 |
+
cache_dir = "/home/user/.cache/models"
|
| 92 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 93 |
+
|
| 94 |
+
# Get model details
|
| 95 |
+
if not model_name or not repo_id:
|
| 96 |
+
model_option = MODEL_OPTIONS["small"] # default to small model
|
| 97 |
+
model_name = model_option["name"]
|
| 98 |
+
repo_id = model_option["repo"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 99 |
|
| 100 |
+
# Ensure model_name and repo_id are not None
|
| 101 |
+
if model_name is None:
|
| 102 |
+
raise ValueError("model_name cannot be None")
|
| 103 |
+
if repo_id is None:
|
| 104 |
+
raise ValueError("repo_id cannot be None")
|
| 105 |
+
# Check if model already exists in cache
|
| 106 |
+
model_path = os.path.join(cache_dir, model_name)
|
| 107 |
+
if os.path.exists(model_path):
|
| 108 |
+
print(f"\nUsing cached model: {model_path}")
|
| 109 |
+
return model_path
|
| 110 |
+
|
| 111 |
+
print(f"\nDownloading model {model_name} from {repo_id}...")
|
| 112 |
+
try:
|
| 113 |
+
model_path = hf_hub_download(
|
| 114 |
+
repo_id=repo_id,
|
| 115 |
+
filename=model_name,
|
| 116 |
+
cache_dir=cache_dir,
|
| 117 |
+
local_dir=cache_dir
|
| 118 |
+
)
|
| 119 |
+
print(f"Model downloaded successfully to {model_path}")
|
| 120 |
+
return model_path
|
| 121 |
+
except Exception as e:
|
| 122 |
+
print(f"Error downloading model: {str(e)}")
|
| 123 |
+
raise
|
| 124 |
|
| 125 |
# Ensure model is downloaded
|
| 126 |
model_path = ensure_model()
|
|
|
|
| 239 |
return message
|
| 240 |
|
| 241 |
# Build enhanced Gradio interface
|
| 242 |
+
with gr.Blocks() as demo:
|
| 243 |
gr.Markdown("""
|
| 244 |
# 🏥 Medical Symptom to ICD-10 Code Assistant
|
| 245 |
|
|
|
|
| 262 |
chatbot = gr.Chatbot(
|
| 263 |
label="Medical Consultation",
|
| 264 |
height=500,
|
| 265 |
+
container=True
|
|
|
|
| 266 |
)
|
| 267 |
|
| 268 |
with gr.Row():
|