dwb2023 commited on
Commit
57a0425
·
verified ·
1 Parent(s): 8d7bc9c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -11
app.py CHANGED
@@ -1,15 +1,12 @@
1
  from huggingface_hub import login
2
- # import spaces
3
  import os
4
  import time
5
  import gradio as gr
6
- from typing import *
7
  from pillow_heif import register_heif_opener
8
  register_heif_opener()
9
  import vision_agent as va
10
- from vision_agent.tools import register_tool
11
- from vision_agent.tools import load_image, owl_v2, overlay_bounding_boxes, save_image
12
-
13
 
14
  # Perform login using the token
15
  hf_token = os.getenv("HF_TOKEN")
@@ -18,6 +15,32 @@ login(token=hf_token, add_to_git_credential=True)
18
  import numpy as np
19
  from PIL import Image
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  def detect_brain_tumor(image, seg_input, debug: bool = True):
22
  """
23
  Detects a brain tumor in the given image and returns the annotated image.
@@ -31,20 +54,20 @@ def detect_brain_tumor(image, seg_input, debug: bool = True):
31
  tuple: (numpy array of image, list of (label, (x1, y1, x2, y2)) tuples)
32
  """
33
  if debug:
34
- print(f"Image received, shape: {image.shape}")
35
 
36
  # Step 2: Detect brain tumor using owl_v2
37
  prompt = "detect brain tumor"
38
  detections = owl_v2(prompt, image)
39
  if debug:
40
- print(f"Raw detections: {detections}")
41
 
42
  # Step 3: Overlay bounding boxes on the image
43
  image_with_bboxes = overlay_bounding_boxes(image, detections)
44
  if debug:
45
- print("Bounding boxes overlaid on the image")
46
 
47
- # Prepare annotations for AnnotatedImage output
48
  annotations = []
49
  for detection in detections:
50
  label = detection['label']
@@ -57,7 +80,7 @@ def detect_brain_tumor(image, seg_input, debug: bool = True):
57
  annotations.append(((x1, y1, x2, y2), f"{label} {score:.2f}"))
58
 
59
  if debug:
60
- print(f"Annotations: {annotations}")
61
 
62
  # Convert image to numpy array if it's not already
63
  if isinstance(image_with_bboxes, Image.Image):
@@ -106,4 +129,4 @@ with gr.Blocks(css="style.css") as demo:
106
  )
107
 
108
  if __name__ == "__main__":
109
- demo.queue(max_size=10).launch(debug=True)
 
1
  from huggingface_hub import login
 
2
  import os
3
  import time
4
  import gradio as gr
5
+ from typing import * # Ensure that the typing module is imported
6
  from pillow_heif import register_heif_opener
7
  register_heif_opener()
8
  import vision_agent as va
9
+ from vision_agent.tools import register_tool, load_image, owl_v2, overlay_bounding_boxes, save_image
 
 
10
 
11
  # Perform login using the token
12
  hf_token = os.getenv("HF_TOKEN")
 
15
  import numpy as np
16
  from PIL import Image
17
 
18
+ # Set up logging
19
+ import logging
20
+ import sys
21
+
22
+ def get_logging_level(default_level=logging.INFO):
23
+ log_level_str = os.getenv('VISION_AGENT_LOG_LEVEL', '').upper()
24
+ if log_level_str == 'DEBUG':
25
+ return logging.DEBUG
26
+ elif log_level_str == 'INFO':
27
+ return logging.INFO
28
+ elif log_level_str == 'WARNING':
29
+ return logging.WARNING
30
+ elif log_level_str == 'ERROR':
31
+ return logging.ERROR
32
+ elif log_level_str == 'CRITICAL':
33
+ return logging.CRITICAL
34
+ else:
35
+ return default_level
36
+
37
+ logging_level = get_logging_level()
38
+ logging.basicConfig(stream=sys.stdout, level=logging_level)
39
+ _LOGGER = logging.getLogger(__name__)
40
+
41
+ # Print the logging level to verify it's set correctly
42
+ print(f"Logging level set to: {logging.getLevelName(logging_level)}")
43
+
44
  def detect_brain_tumor(image, seg_input, debug: bool = True):
45
  """
46
  Detects a brain tumor in the given image and returns the annotated image.
 
54
  tuple: (numpy array of image, list of (label, (x1, y1, x2, y2)) tuples)
55
  """
56
  if debug:
57
+ _LOGGER.debug(f"Image received, shape: {image.shape}")
58
 
59
  # Step 2: Detect brain tumor using owl_v2
60
  prompt = "detect brain tumor"
61
  detections = owl_v2(prompt, image)
62
  if debug:
63
+ _LOGGER.debug(f"Raw detections: {detections}")
64
 
65
  # Step 3: Overlay bounding boxes on the image
66
  image_with_bboxes = overlay_bounding_boxes(image, detections)
67
  if debug:
68
+ _LOGGER.debug("Bounding boxes overlaid on the image")
69
 
70
+ # Prepare annotations for AnnotatedImage output
71
  annotations = []
72
  for detection in detections:
73
  label = detection['label']
 
80
  annotations.append(((x1, y1, x2, y2), f"{label} {score:.2f}"))
81
 
82
  if debug:
83
+ _LOGGER.debug(f"Annotations: {annotations}")
84
 
85
  # Convert image to numpy array if it's not already
86
  if isinstance(image_with_bboxes, Image.Image):
 
129
  )
130
 
131
  if __name__ == "__main__":
132
+ demo.queue(max_size=10).launch(debug=True)