SamMec commited on
Commit
f3cc9d3
·
verified ·
1 Parent(s): 03ca62f

Sync from Sam-MECHH/lead_final_project_api via hub-sync

Browse files
.dockerignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ .venv/
2
+ venv/
3
+ __pycache__/
4
+ *.pyc
5
+ .git/
6
+ secrets.env
7
+ .env
8
+ .github/
.ruff_cache/.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ # Automatically created by ruff.
2
+ *
.ruff_cache/0.16.5/10810121999000877369 ADDED
Binary file (196 Bytes). View file
 
.ruff_cache/0.16.5/6238877333258014334 ADDED
Binary file (204 Bytes). View file
 
.ruff_cache/CACHEDIR.TAG ADDED
@@ -0,0 +1 @@
 
 
1
+ Signature: 8a477f597d28d172789f06886806bc55
Dockerfile ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM continuumio/miniconda3
2
+
3
+ # Update packages and install nano and curl
4
+ RUN apt-get update -y
5
+ RUN apt-get install nano curl -y
6
+
7
+ # Force Conda to downgrade Python to a stable ML baseline (Python 3.10)
8
+ RUN conda install -y python=3.10
9
+
10
+ # THIS IS SPECIFIC TO HUGGINFACE
11
+ # We create a new user named "user" with ID of 1000
12
+ RUN useradd -m -u 1000 user
13
+ # We switch from "root" (default user when creating an image) to "user"
14
+ USER user
15
+ # We set two environmnet variables
16
+ # so that we can give ownership to all files in there afterwards
17
+ # we also add /home/user/.local/bin in the $PATH environment variable
18
+ # PATH environment variable sets paths to look for installed binaries
19
+ # We update it so that Linux knows where to look for binaries if we were to install them with "user".
20
+ ENV HOME=/home/user \
21
+ PATH=/home/user/.local/bin:$PATH \
22
+ PORT=7860
23
+
24
+ # We set working directory to $HOME/app (<=> /home/user/app)
25
+ WORKDIR $HOME/app
26
+
27
+ # Copy requirements first to leverage Docker layer caching
28
+ COPY --chown=user requirements.txt $HOME/app/requirements.txt
29
+
30
+ # Install dependencies and clear cache in the same layer to save space
31
+ RUN pip install -r requirements.txt
32
+
33
+ # Copy the rest of the application files
34
+ COPY --chown=user . $HOME/app
35
+
36
+ EXPOSE 7860
37
+
38
+ # Run FastAPI
39
+ CMD ["sh", "-c", "fastapi run src/radio_check_app.py --port ${PORT}"]
README.md CHANGED
@@ -1,13 +1,11 @@
1
  ---
2
- title: Fastapi Gradio
3
- emoji: 💻
4
  colorFrom: indigo
5
  colorTo: blue
6
- sdk: gradio
7
- sdk_version: 6.26.0
8
- python_version: '3.12'
9
- app_file: app.py
10
  pinned: false
 
11
  ---
12
 
13
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Demoday Fastapi
3
+ emoji: 📊
4
  colorFrom: indigo
5
  colorTo: blue
6
+ sdk: docker
 
 
 
7
  pinned: false
8
+ license: apache-2.0
9
  ---
10
 
11
  Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ import uvicorn
2
+ # Import your FastAPI instance from src/demoday_dashboard_app.py
3
+ from src.demoday_dashboard_app import app
4
+
5
+ if __name__ == "__main__":
6
+ # Hugging Face Spaces require port 7860
7
+ uvicorn.run(app, host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # --- Web & API Core ---
2
+ fastapi
3
+ uvicorn
4
+
5
+ # --- Deep Learning & NLP ---
6
+ transformers
7
+ accelerate
8
+
9
+ # --- Computer Vision & Imaging ---
10
+ Pillow
11
+ hi-ml-multimodal
12
+
13
+ # --- MLOps & Remote Model Registry (S3/MLflow) ---
14
+ mlflow==3.5.0
15
+ boto3
16
+ fsspec
17
+ s3fs
src/radio_check_app.py ADDED
@@ -0,0 +1,225 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import io
2
+ import json
3
+ import os
4
+ import uuid
5
+ from datetime import datetime
6
+
7
+ import boto3
8
+ import mlflow.pytorch
9
+ import torch
10
+ from fastapi import BackgroundTasks, FastAPI, File, Form, HTTPException, UploadFile
11
+ from health_multimodal.image.data.transforms import (
12
+ create_chest_xray_transform_for_inference,
13
+ )
14
+ from health_multimodal.image.model.pretrained import get_biovil_t_image_encoder
15
+ from PIL import Image
16
+ from transformers import AutoModel, AutoTokenizer
17
+
18
+ app = FastAPI(title="BioVil Cross-Attention+MLP API")
19
+
20
+ # Global instances
21
+ device = None
22
+ tokenizer = None
23
+ text_model = None
24
+ image_model = None
25
+ image_transform = None
26
+ cross_att_classifier = None
27
+ s3_client = None
28
+ S3_BUCKET_NAME = os.environ.get("S3_MONITORING_BUCKET", "smec-lead-fp-mlops-monitoring")
29
+ REGISTERED_MODEL_NAME = "biovil_t_lead_demo"
30
+ MODEL_ALIAS = "prod"
31
+ MODEL_URI = f"models:/{REGISTERED_MODEL_NAME}@{MODEL_ALIAS}"
32
+
33
+
34
+ # Startup component
35
+ @app.on_event("startup")
36
+ def load_all_models_and_assets():
37
+ global \
38
+ device, \
39
+ tokenizer, \
40
+ text_model, \
41
+ image_model, \
42
+ image_transform, \
43
+ cross_att_classifier, \
44
+ s3_client
45
+ try:
46
+ # Setup device configuration
47
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
48
+ print(f"Using device: {device}")
49
+
50
+ # Initialize S3 Client
51
+ s3_client = boto3.client("s3")
52
+
53
+ # Load the comprehensive BioViL-T repo for Text
54
+ model_id = "microsoft/BiomedVLP-BioViL-T"
55
+
56
+ # Specialized CXR-BERT tokenizer and model
57
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
58
+ text_model = AutoModel.from_pretrained(model_id, trust_remote_code=True).to(
59
+ device
60
+ )
61
+
62
+ # Instantiate the BioViL-T Image Engine
63
+ image_model = get_biovil_t_image_encoder().to(device)
64
+ image_transform = create_chest_xray_transform_for_inference(
65
+ resize=512, center_crop_size=448
66
+ )
67
+
68
+ # Connect to Hugging Face MLflow instance and pull the Cross-Attention Classifier
69
+ mlflow.set_tracking_uri(os.environ.get("MLFLOW_URI"))
70
+ cross_att_classifier = mlflow.pytorch.load_model(
71
+ MODEL_URI, map_location=torch.device("cpu")
72
+ )
73
+ cross_att_classifier.to(device).eval()
74
+
75
+ print("Models and processors loaded successfully!")
76
+ except Exception as e:
77
+ print(f"❌ Startup Error: {e!s}")
78
+ raise e
79
+
80
+
81
+ # Background task for S3 logging
82
+ def save_production_data_to_s3(
83
+ request_id: str,
84
+ raw_text: str,
85
+ image_bytes: bytes,
86
+ prediction: int,
87
+ probability: float,
88
+ ):
89
+ try:
90
+ # 1. Save raw Image file to S3
91
+ image_s3_key = f"production_data/images/{request_id}.jpg"
92
+ s3_client.put_object(
93
+ Bucket=S3_BUCKET_NAME,
94
+ Key=image_s3_key,
95
+ Body=image_bytes,
96
+ ContentType="image/jpg",
97
+ )
98
+
99
+ # 2. Save Metadata & Text to S3 as JSON (for Evidently AI reading)
100
+ metadata = {
101
+ "request_id": request_id,
102
+ "timestamp": datetime.utcnow().isoformat(),
103
+ "raw_text": raw_text,
104
+ "image_s3_uri": f"s3://{S3_BUCKET_NAME}/{image_s3_key}",
105
+ "prediction": prediction,
106
+ "probability": probability,
107
+ }
108
+
109
+ metadata_s3_key = f"production_data/reports_metadata/{request_id}.json"
110
+ s3_client.put_object(
111
+ Bucket=S3_BUCKET_NAME,
112
+ Key=metadata_s3_key,
113
+ Body=json.dumps(metadata),
114
+ ContentType="application/json",
115
+ )
116
+ print(f"Logged request {request_id} to S3 successfully.")
117
+ except Exception as e:
118
+ print(f" Failed to log to S3 for request {request_id}: {e!s}")
119
+
120
+
121
+ # Preprocessing pipelines
122
+ def get_text_embeddings(report_text):
123
+ inputs = tokenizer(
124
+ report_text,
125
+ padding="max_length",
126
+ truncation=True,
127
+ max_length=512,
128
+ return_tensors="pt",
129
+ ).to(device)
130
+
131
+ with torch.no_grad():
132
+ outputs = text_model(
133
+ input_ids=inputs.input_ids,
134
+ attention_mask=inputs.attention_mask,
135
+ return_dict=True,
136
+ )
137
+ return outputs.last_hidden_state
138
+
139
+
140
+ def get_image_embeddings_from_pil(pil_image):
141
+ # Adjusted to accept the PIL image object directly from memory
142
+ raw_image = pil_image.convert("L")
143
+ processed_tensor = image_transform(raw_image).unsqueeze(0).to(device)
144
+
145
+ with torch.no_grad():
146
+ image_outputs = image_model(processed_tensor)
147
+ return image_outputs.projected_patch_embeddings
148
+
149
+
150
+ # Predict endpoint
151
+ @app.post("/predict")
152
+ async def predict(
153
+ background_tasks: BackgroundTasks,
154
+ text_input: str = Form(...),
155
+ image_file: UploadFile = File(...),
156
+ ):
157
+ # Guard against queries hitting the server before models are fully loaded
158
+ if None in (cross_att_classifier, text_model, image_model):
159
+ raise HTTPException(
160
+ status_code=503, detail="Models are initializing. Try again shortly."
161
+ )
162
+
163
+ try:
164
+ # Read incoming file stream directly into memory as a PIL Image
165
+ image_bytes = await image_file.read()
166
+ pil_image = Image.open(io.BytesIO(image_bytes))
167
+
168
+ # Use custom preprocessing pipelines
169
+ sequence_outputs = get_text_embeddings(text_input)
170
+ patch_img_emb = get_image_embeddings_from_pil(pil_image)
171
+
172
+ # Run inputs through registered Cross-Attention Classifier
173
+ with torch.no_grad():
174
+ outputs = cross_att_classifier(
175
+ patch_img_emb, sequence_outputs[:, :256, :]
176
+ ).squeeze(1)
177
+ probability = torch.sigmoid(outputs).item()
178
+ prediction = int(probability >= 0.5)
179
+
180
+ # Generate unique ID for matching image and metadata in S3
181
+ request_id = str(uuid.uuid4())
182
+
183
+ # Schedule S3 upload in background (Executes AFTER function returns)
184
+ background_tasks.add_task(
185
+ save_production_data_to_s3,
186
+ request_id=request_id,
187
+ raw_text=text_input,
188
+ image_bytes=image_bytes,
189
+ prediction=prediction,
190
+ probability=probability,
191
+ )
192
+ # Return JSON response back to Streamlit
193
+ return {
194
+ "status": "success",
195
+ "prediction": prediction,
196
+ "probability": round(probability, 4),
197
+ }
198
+
199
+ except Exception as e:
200
+ raise HTTPException(status_code=500, detail=f"Inference error: {e!s}")
201
+
202
+
203
+ @app.post("/reload-model")
204
+ async def reload_model():
205
+ """
206
+ Reload the model from MLflow without reastarting API
207
+ """
208
+ global cross_att_classifier
209
+
210
+ try:
211
+ # Retrieve the model with production alias
212
+ cross_att_classifier = mlflow.pytorch.load_model(
213
+ MODEL_URI, map_location=torch.device("cpu")
214
+ )
215
+ cross_att_classifier.to(device).eval()
216
+
217
+ return {
218
+ "status": "success",
219
+ "message": f"Model {REGISTERED_MODEL_NAME}@{MODEL_ALIAS} loaded successfully !",
220
+ }
221
+ except Exception as e:
222
+ return {
223
+ "status": "error",
224
+ "message": f"Fail to load the model : {e!s}",
225
+ }