capradeepgujaran commited on
Commit
30f620c
·
verified ·
1 Parent(s): 29a93ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -40
app.py CHANGED
@@ -11,25 +11,6 @@ import base64
11
  def create_monitor_interface():
12
  api_key = os.getenv("GROQ_API_KEY")
13
 
14
- if not api_key:
15
- with gr.Blocks() as demo:
16
- gr.Markdown("""
17
- # ⚠️ Groq API Key Required
18
-
19
- ## Setup Instructions for Hugging Face Space:
20
- 1. Go to your Space's Settings tab
21
- 2. Scroll down to "Repository Secrets"
22
- 3. Click "New Secret"
23
- 4. Enter:
24
- - Secret name: `GROQ_API_KEY`
25
- - Secret value: Your Groq API key
26
- 5. Click "Add secret"
27
- 6. Rebuild the Space
28
-
29
- Once configured, the safety monitoring system will be available.
30
- """)
31
- return demo
32
-
33
  class SafetyMonitor:
34
  def __init__(self, model_name: str = "mixtral-8x7b-vision"):
35
  self.client = Groq(api_key=api_key)
@@ -40,9 +21,9 @@ def create_monitor_interface():
40
  return "No frame received"
41
 
42
  # Convert numpy array to PIL Image
43
- if len(frame.shape) == 2: # If grayscale
44
  frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
45
- elif len(frame.shape) == 3 and frame.shape[2] == 4: # If RGBA
46
  frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2RGB)
47
 
48
  frame_pil = PILImage.fromarray(frame)
@@ -52,34 +33,24 @@ def create_monitor_interface():
52
  frame_pil.save(buffered, format="JPEG")
53
  img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
54
 
55
- # Construct the prompt with proper format
56
- system_prompt = """You are a workplace safety expert. Analyze the image for safety concerns, focusing on:
57
- 1. PPE usage (helmets, vests, etc.)
58
- 2. Unsafe behaviors
59
- 3. Equipment safety
60
- 4. Environmental hazards
61
-
62
- Provide clear, specific observations."""
63
-
64
  try:
65
  completion = self.client.chat.completions.create(
66
  messages=[
67
- {
68
- "role": "system",
69
- "content": system_prompt
70
- },
71
  {
72
  "role": "user",
73
  "content": [
74
  {
75
  "type": "text",
76
- "text": "Please analyze this image for safety concerns."
 
 
 
 
 
77
  },
78
  {
79
- "type": "image",
80
- "image": {
81
- "url": f"data:image/jpeg;base64,{img_base64}"
82
- }
83
  }
84
  ]
85
  }
@@ -113,7 +84,7 @@ def create_monitor_interface():
113
  cv2.putText(display_frame, line[:80], (10, y_position),
114
  cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
115
  y_position += 30
116
- if y_position >= 90: # Prevent text from going outside the overlay
117
  break
118
 
119
  return display_frame, analysis
@@ -140,6 +111,7 @@ def create_monitor_interface():
140
  processed_frame, analysis = monitor.process_frame(image)
141
  return processed_frame, analysis
142
  except Exception as e:
 
143
  return None, f"Error processing image: {str(e)}"
144
 
145
  input_image.change(
 
11
  def create_monitor_interface():
12
  api_key = os.getenv("GROQ_API_KEY")
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  class SafetyMonitor:
15
  def __init__(self, model_name: str = "mixtral-8x7b-vision"):
16
  self.client = Groq(api_key=api_key)
 
21
  return "No frame received"
22
 
23
  # Convert numpy array to PIL Image
24
+ if len(frame.shape) == 2:
25
  frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2RGB)
26
+ elif len(frame.shape) == 3 and frame.shape[2] == 4:
27
  frame = cv2.cvtColor(frame, cv2.COLOR_RGBA2RGB)
28
 
29
  frame_pil = PILImage.fromarray(frame)
 
33
  frame_pil.save(buffered, format="JPEG")
34
  img_base64 = base64.b64encode(buffered.getvalue()).decode('utf-8')
35
 
 
 
 
 
 
 
 
 
 
36
  try:
37
  completion = self.client.chat.completions.create(
38
  messages=[
 
 
 
 
39
  {
40
  "role": "user",
41
  "content": [
42
  {
43
  "type": "text",
44
+ "text": """Please analyze this image for workplace safety issues. Focus on:
45
+ 1. PPE usage (helmets, safety glasses, vests)
46
+ 2. Unsafe behaviors or positions
47
+ 3. Equipment and machinery safety
48
+ 4. Environmental hazards
49
+ Provide specific observations."""
50
  },
51
  {
52
+ "type": "image_url",
53
+ "url": f"data:image/jpeg;base64,{img_base64}"
 
 
54
  }
55
  ]
56
  }
 
84
  cv2.putText(display_frame, line[:80], (10, y_position),
85
  cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)
86
  y_position += 30
87
+ if y_position >= 90:
88
  break
89
 
90
  return display_frame, analysis
 
111
  processed_frame, analysis = monitor.process_frame(image)
112
  return processed_frame, analysis
113
  except Exception as e:
114
+ print(f"Processing error: {str(e)}") # For debugging
115
  return None, f"Error processing image: {str(e)}"
116
 
117
  input_image.change(