Spaces:
Running
Running
File size: 11,870 Bytes
50a465a 57a0425 640c7a9 4038683 640c7a9 57a0425 ec81974 640c7a9 57a0425 640c7a9 57a0425 d1d6d52 66e71e9 d1d6d52 640c7a9 d1d6d52 640c7a9 270aac5 a78c4d2 5020c09 a78c4d2 2ff2fb2 5020c09 2ff2fb2 464a3bb 59ad67f a78c4d2 2ff2fb2 270aac5 43ba1c2 640c7a9 270aac5 640c7a9 270aac5 640c7a9 2ff2fb2 57a0425 5020c09 0dc767e 640c7a9 270aac5 640c7a9 b485212 640c7a9 5020c09 464a3bb 4038683 8d7bc9c 9f9688c 4038683 a2e1737 640c7a9 5c76b5d d9f0542 4221a85 10b8cf9 fcf9342 6f04993 a2e1737 10b8cf9 fe94638 96e3a47 fe94638 2ff2fb2 a2e1737 10b8cf9 79605da 10b8cf9 a2e1737 5dae26f 640c7a9 4221a85 640c7a9 fe94638 96e3a47 fe94638 640c7a9 79605da 640c7a9 4221a85 640c7a9 fe94638 4221a85 640c7a9 79605da 640c7a9 4038683 fe94638 4038683 640c7a9 5dae26f 57a0425 |
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 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 |
import os
import logging
import sys
from config import WEAVE_PROJECT, WANDB_API_KEY
import weave
from model_utils import get_model_summary, install_flash_attn
# Install required package
install_flash_attn()
weave.init(WEAVE_PROJECT)
# Function to get logging level from environment variable
def get_logging_level(default_level=logging.INFO): # Default to DEBUG for detailed logs
log_level_str = os.getenv('VISION_AGENT_LOG_LEVEL', '').upper()
if log_level_str == 'DEBUG':
return logging.DEBUG
elif log_level_str == 'INFO':
return logging.INFO
elif log_level_str == 'WARNING':
return logging.WARNING
elif log_level_str == 'ERROR':
return logging.ERROR
elif log_level_str == 'CRITICAL':
return logging.CRITICAL
else:
return default_level
# Initialize logger
logging.basicConfig(level=get_logging_level(), format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger('vision_agent')
from huggingface_hub import login
import time
import gradio as gr
from typing import *
from pillow_heif import register_heif_opener
register_heif_opener()
import vision_agent as va
from vision_agent.tools import register_tool, load_image, owl_v2, grounding_dino, florencev2_object_detection, overlay_bounding_boxes, save_image
# Perform login using the token
hf_token = os.getenv("HF_TOKEN")
login(token=hf_token, add_to_git_credential=True)
import numpy as np
from PIL import Image
@weave.op()
def detect_object_owlv2(image, seg_input, debug: bool = True):
"""
Detects a brain tumor in the given image and returns the annotated image.
Parameters:
image: The input image (as numpy array provided by Gradio).
seg_input: The segmentation input (not used in this function, but required for Gradio).
debug (bool): Flag to enable logging for debugging purposes.
Returns:
tuple: (numpy array of image, list of (label, (x1, y1, x2, y2)) tuples)
"""
# Step 2: Detect brain tumor using owl_v2
prompt = seg_input
detections = owl_v2(prompt, image)
# Step 3: Overlay bounding boxes on the image
image_with_bboxes = overlay_bounding_boxes(image, detections)
# Prepare annotations for AnnotatedImage output
annotations = []
for detection in detections:
label = detection['label']
score = detection['score']
bbox = detection['bbox']
x1, y1, x2, y2 = bbox
# Convert normalized coordinates to pixel coordinates
height, width = image.shape[:2]
x1, y1, x2, y2 = int(x1*width), int(y1*height), int(x2*width), int(y2*height)
annotations.append(((x1, y1, x2, y2), f"{label} {score:.2f}"))
# Convert image to numpy array if it's not already
if isinstance(image_with_bboxes, Image.Image):
image_with_bboxes = np.array(image_with_bboxes)
return (image_with_bboxes, annotations)
@weave.op()
def detect_object_dino(image, seg_input, debug: bool = True):
"""
Detects a brain tumor in the given image and returns the annotated image.
Parameters:
image: The input image (as numpy array provided by Gradio).
seg_input: The segmentation input (not used in this function, but required for Gradio).
debug (bool): Flag to enable logging for debugging purposes.
Returns:
tuple: (numpy array of image, list of (label, (x1, y1, x2, y2)) tuples)
"""
# Step 2: Detect brain tumor using grounding_dino
prompt = seg_input
detections = grounding_dino(prompt, image)
# Step 3: Overlay bounding boxes on the image
image_with_bboxes = overlay_bounding_boxes(image, detections)
# Prepare annotations for AnnotatedImage output
annotations = []
for detection in detections:
label = detection['label']
score = detection['score']
bbox = detection['bbox']
x1, y1, x2, y2 = bbox
# Convert normalized coordinates to pixel coordinates
height, width = image.shape[:2]
x1, y1, x2, y2 = int(x1*width), int(y1*height), int(x2*width), int(y2*height)
annotations.append(((x1, y1, x2, y2), f"{label} {score:.2f}"))
# Convert image to numpy array if it's not already
if isinstance(image_with_bboxes, Image.Image):
image_with_bboxes = np.array(image_with_bboxes)
return (image_with_bboxes, annotations)
@weave.op()
def detect_object_florence2(image, seg_input, debug: bool = True):
"""
Detects a brain tumor in the given image and returns the annotated image.
Parameters:
image: The input image (as numpy array provided by Gradio).
seg_input: The segmentation input (not used in this function, but required for Gradio).
debug (bool): Flag to enable logging for debugging purposes.
Returns:
tuple: (numpy array of image, list of (label, (x1, y1, x2, y2)) tuples)
"""
# Step 2: Detect brain tumor using florencev2 - NO PROMPT
detections = florencev2_object_detection(image)
# Step 3: Overlay bounding boxes on the image
image_with_bboxes = overlay_bounding_boxes(image, detections)
# Prepare annotations for AnnotatedImage output
annotations = []
for detection in detections:
label = detection['label']
score = detection['score']
bbox = detection['bbox']
x1, y1, x2, y2 = bbox
# Convert normalized coordinates to pixel coordinates
height, width = image.shape[:2]
x1, y1, x2, y2 = int(x1*width), int(y1*height), int(x2*width), int(y2*height)
annotations.append(((x1, y1, x2, y2), f"{label} {score:.2f}"))
# Convert image to numpy array if it's not already
if isinstance(image_with_bboxes, Image.Image):
image_with_bboxes = np.array(image_with_bboxes)
return (image_with_bboxes, annotations)
def handle_model_summary(model_name):
model_summary, error_message = get_model_summary(model_name)
if error_message:
return error_message, ""
return model_summary, ""
INTRO_TEXT="# 🔬🧠 OmniScience -- Agentic Imaging Analysis 🤖🧫"
with gr.Blocks(theme="sudeepshouche/minimalist") as demo:
gr.Markdown(INTRO_TEXT)
with gr.Tab("Object Detection - Owl V2"):
with gr.Row():
with gr.Column():
image = gr.Image(type="numpy")
seg_input = gr.Text(label="Entities to Segment/Detect")
with gr.Column():
annotated_image = gr.AnnotatedImage(label="Output")
seg_btn = gr.Button("Submit")
examples = [
["./examples/BloodImage_00099_jpg.rf.0a65e56401cdd71253e7bc04917c3558.jpg", "detect blood cell"],
["./examples/15_242_212_25_25_jpg.rf.f6bbadf4260dd2c1f5b4ace1b09b0a1b.jpg", "detect liver disease"],
["./examples/194_jpg.rf.3e3dd592d034bb5ee27a978553819f42.jpg", "detect brain tumor"],
["./examples/239_jpg.rf.3dcc0799277fb78a2ab21db7761ccaeb.jpg", "detect brain tumor"],
["./examples/2871_jpg.rf.3b6eadfbb369abc2b3bcb52b406b74f2.jpg", "detect brain tumor"],
["./examples/2921_jpg.rf.3b952f91f27a6248091e7601c22323ad.jpg", "detect brain tumor"],
]
gr.Examples(
examples=examples,
inputs=[image, seg_input],
)
seg_inputs = [
image,
seg_input
]
seg_outputs = [
annotated_image
]
seg_btn.click(
fn=detect_object_owlv2,
inputs=seg_inputs,
outputs=seg_outputs,
)
with gr.Tab("Object Detection - DINO"):
with gr.Row():
with gr.Column():
image = gr.Image(type="numpy")
seg_input = gr.Text(label="Entities to Segment/Detect")
with gr.Column():
annotated_image = gr.AnnotatedImage(label="Output")
seg_btn = gr.Button("Submit")
examples = [
["./examples/BloodImage_00099_jpg.rf.0a65e56401cdd71253e7bc04917c3558.jpg", "detect blood cell"],
["./examples/15_242_212_25_25_jpg.rf.f6bbadf4260dd2c1f5b4ace1b09b0a1b.jpg", "detect liver disease"],
["./examples/194_jpg.rf.3e3dd592d034bb5ee27a978553819f42.jpg", "detect brain tumor"],
["./examples/239_jpg.rf.3dcc0799277fb78a2ab21db7761ccaeb.jpg", "detect brain tumor"],
["./examples/2871_jpg.rf.3b6eadfbb369abc2b3bcb52b406b74f2.jpg", "detect brain tumor"],
["./examples/2921_jpg.rf.3b952f91f27a6248091e7601c22323ad.jpg", "detect brain tumor"],
]
gr.Examples(
examples=examples,
inputs=[image, seg_input],
)
seg_inputs = [
image,
seg_input
]
seg_outputs = [
annotated_image
]
seg_btn.click(
fn=detect_object_dino,
inputs=seg_inputs,
outputs=seg_outputs,
)
with gr.Tab("Object Detection - Florence2"):
with gr.Row():
with gr.Column():
image = gr.Image(type="numpy")
seg_input = gr.Text(label="Entities to Segment/Detect")
with gr.Column():
annotated_image = gr.AnnotatedImage(label="Output")
seg_btn = gr.Button("Submit")
examples = [
["./examples/BloodImage_00099_jpg.rf.0a65e56401cdd71253e7bc04917c3558.jpg", "<OD>"],
["./examples/15_242_212_25_25_jpg.rf.f6bbadf4260dd2c1f5b4ace1b09b0a1b.jpg", "<OD>"],
["./examples/194_jpg.rf.3e3dd592d034bb5ee27a978553819f42.jpg", "<OD>"],
["./examples/239_jpg.rf.3dcc0799277fb78a2ab21db7761ccaeb.jpg", "<OD>"],
["./examples/2871_jpg.rf.3b6eadfbb369abc2b3bcb52b406b74f2.jpg", "<OD>"],
["./examples/2921_jpg.rf.3b952f91f27a6248091e7601c22323ad.jpg", "<OD>"],
]
gr.Examples(
examples=examples,
inputs=[image, seg_input],
)
seg_inputs = [
image,
seg_input
]
seg_outputs = [
annotated_image
]
seg_btn.click(
fn=detect_object_florence2,
inputs=seg_inputs,
outputs=seg_outputs,
)
with gr.Tab("Model Explorer"):
gr.Markdown("## Retrieve and Display Model Architecture")
model_name_input = gr.Textbox(label="Model Name", placeholder="Enter the model name to retrieve its architecture...")
vision_examples = gr.Examples(
examples=[
["facebook/sam-vit-huge"],
["google/owlv2-base-patch16-ensemble"],
["IDEA-Research/grounding-dino-base"],
["microsoft/Florence-2-large-ft"],
["google/paligemma-3b-mix-224"],
["llava-hf/llava-v1.6-mistral-7b-hf"],
["vikhyatk/moondream2"],
["microsoft/Phi-3-vision-128k-instruct"],
["HuggingFaceM4/idefics2-8b-chatty"]
],
inputs=model_name_input
)
model_submit_button = gr.Button("Submit")
model_output = gr.Textbox(label="Model Architecture", lines=20, placeholder="Model architecture will appear here...", show_copy_button=True)
error_output = gr.Textbox(label="Error", lines=10, placeholder="Exceptions will appear here...", show_copy_button=True)
model_submit_button.click(fn=handle_model_summary, inputs=model_name_input, outputs=[model_output, error_output])
if __name__ == "__main__":
demo.queue(max_size=10).launch(debug=True)
|