capradeepgujaran commited on
Commit
5f3406b
1 Parent(s): 1666373

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -17
app.py CHANGED
@@ -6,23 +6,20 @@ import time
6
  from PIL import Image as PILImage
7
  import io
8
  import os
9
- import base64
 
10
 
11
  def create_monitor_interface():
12
  api_key = os.getenv("GROQ_API_KEY")
13
 
14
  class SafetyMonitor:
15
- def __init__(self, model_name: str = "llama-3.2-90b-vision-preview"):
16
  self.client = Groq(api_key=api_key)
17
- self.model_name = model_name
18
- self.max_image_size = (128, 128) # Drastically reduced size
19
- self.jpeg_quality = 20 # Very low quality
20
-
21
  def resize_image(self, image):
22
- """Resize image while maintaining aspect ratio"""
23
  height, width = image.shape[:2]
24
-
25
- # Calculate aspect ratio
26
  aspect = width / height
27
 
28
  if width > height:
@@ -32,8 +29,7 @@ def create_monitor_interface():
32
  new_height = min(self.max_image_size[1], height)
33
  new_width = int(new_height * aspect)
34
 
35
- resized = cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
36
- return resized
37
 
38
  def analyze_frame(self, frame: np.ndarray) -> str:
39
  if frame is None:
@@ -48,23 +44,37 @@ def create_monitor_interface():
48
  frame = self.resize_image(frame)
49
  frame_pil = PILImage.fromarray(frame)
50
 
51
- # Convert to base64 with minimal size
52
  buffered = io.BytesIO()
53
  frame_pil.save(buffered,
54
  format="JPEG",
55
- quality=self.jpeg_quality,
56
  optimize=True)
57
  img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
58
 
59
  try:
60
- # Minimal prompt
61
- prompt = f"""List safety issues: <image>data:image/jpeg;base64,{img_base64}</image>"""
62
-
63
  completion = self.client.chat.completions.create(
64
  messages=[
65
  {
66
  "role": "user",
67
- "content": prompt
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
68
  }
69
  ],
70
  model=self.model_name,
 
6
  from PIL import Image as PILImage
7
  import io
8
  import os
9
+ from tempfile import NamedTemporaryFile
10
+ from pathlib import Path
11
 
12
  def create_monitor_interface():
13
  api_key = os.getenv("GROQ_API_KEY")
14
 
15
  class SafetyMonitor:
16
+ def __init__(self):
17
  self.client = Groq(api_key=api_key)
18
+ self.model_name = "llama-3.2-90b-vision-preview"
19
+ self.max_image_size = (128, 128)
20
+
 
21
  def resize_image(self, image):
 
22
  height, width = image.shape[:2]
 
 
23
  aspect = width / height
24
 
25
  if width > height:
 
29
  new_height = min(self.max_image_size[1], height)
30
  new_width = int(new_height * aspect)
31
 
32
+ return cv2.resize(image, (new_width, new_height), interpolation=cv2.INTER_AREA)
 
33
 
34
  def analyze_frame(self, frame: np.ndarray) -> str:
35
  if frame is None:
 
44
  frame = self.resize_image(frame)
45
  frame_pil = PILImage.fromarray(frame)
46
 
47
+ # Convert to base64 with minimal quality
48
  buffered = io.BytesIO()
49
  frame_pil.save(buffered,
50
  format="JPEG",
51
+ quality=20,
52
  optimize=True)
53
  img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
54
 
55
  try:
 
 
 
56
  completion = self.client.chat.completions.create(
57
  messages=[
58
  {
59
  "role": "user",
60
+ "content": "You are a workplace safety expert. Analyze the following image for safety concerns."
61
+ },
62
+ {
63
+ "role": "assistant",
64
+ "content": "I'll analyze the image for workplace safety concerns and provide specific observations."
65
+ },
66
+ {
67
+ "role": "user",
68
+ "content": [
69
+ {
70
+ "type": "text",
71
+ "text": "What safety issues do you see?"
72
+ },
73
+ {
74
+ "type": "image_url",
75
+ "url": f"data:image/jpeg;base64,{img_base64}"
76
+ }
77
+ ]
78
  }
79
  ],
80
  model=self.model_name,