kevinconka commited on
Commit
b3f650d
·
1 Parent(s): b0a9338

Add decode_blob_data function to handle Gradio image blob conversion in app.py

Browse files
Files changed (1) hide show
  1. app.py +105 -2
app.py CHANGED
@@ -7,6 +7,8 @@ Any new model should implement the following functions:
7
 
8
  import os
9
  import glob
 
 
10
 
11
  # import spaces
12
  import gradio as gr
@@ -20,7 +22,7 @@ from utils import (
20
  from flagging import HuggingFaceDatasetSaver
21
 
22
  import install_private_repos # noqa: F401
23
- from seavision import load_model, imread
24
 
25
 
26
  TITLE = """
@@ -59,12 +61,113 @@ def inference(image):
59
  return results.draw(image)
60
 
61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
  def flag_img_input(
63
  image: gr.Image, flag_option: str = "misdetection", username: str = "anonymous"
64
  ):
65
  """Wrapper for flagging"""
66
  print(f"{image=}, {flag_option=}, {username=}")
67
- hf_writer.flag([image], flag_option=flag_option, username=username)
 
 
 
 
68
 
69
 
70
  # Flagging
 
7
 
8
  import os
9
  import glob
10
+ import hashlib
11
+ import struct
12
 
13
  # import spaces
14
  import gradio as gr
 
22
  from flagging import HuggingFaceDatasetSaver
23
 
24
  import install_private_repos # noqa: F401
25
+ from seavision import load_model
26
 
27
 
28
  TITLE = """
 
61
  return results.draw(image)
62
 
63
 
64
+ def decode_blob_data(image_data):
65
+ """
66
+ Decode blob data from Gradio image component.
67
+ Handles blob format and converts to proper image file format.
68
+ """
69
+ if not isinstance(image_data, dict):
70
+ return image_data
71
+
72
+ print(f"DEBUG: Original input - image: {image_data}")
73
+
74
+ # Check if this is blob data - more comprehensive check
75
+ is_blob = (
76
+ 'path' in image_data and
77
+ 'blob' in image_data['path'] and
78
+ image_data.get('size') is None and
79
+ image_data.get('orig_name') is None and
80
+ image_data.get('mime_type') is None
81
+ )
82
+
83
+ if is_blob:
84
+ print(f"DEBUG: Converting blob data: {image_data}")
85
+ print("DEBUG: Detected blob format, converting...")
86
+
87
+ blob_path = image_data['path']
88
+ print(f"DEBUG: Blob path: {blob_path}")
89
+
90
+ # Read the blob file
91
+ with open(blob_path, 'rb') as f:
92
+ blob_content = f.read()
93
+
94
+ file_size = len(blob_content)
95
+ print(f"DEBUG: File size: {file_size}")
96
+
97
+ # Check file header to determine format
98
+ if len(blob_content) >= 8:
99
+ header = blob_content[:8].hex()
100
+ print(f"DEBUG: File header: {header}")
101
+
102
+ # PNG header: 89 50 4E 47 0D 0A 1A 0A
103
+ if header.startswith('89504e470d0a1a0a'):
104
+ extension = '.png'
105
+ mime_type = 'image/png'
106
+ # JPEG header: FF D8 FF
107
+ elif header.startswith('ffd8ff'):
108
+ extension = '.jpg'
109
+ mime_type = 'image/jpeg'
110
+ # GIF header: 47 49 46 38
111
+ elif header.startswith('47494638'):
112
+ extension = '.gif'
113
+ mime_type = 'image/gif'
114
+ else:
115
+ # Default to PNG if we can't determine
116
+ extension = '.png'
117
+ mime_type = 'image/png'
118
+ else:
119
+ extension = '.png'
120
+ mime_type = 'image/png'
121
+
122
+ print(f"DEBUG: Detected extension: {extension}, MIME type: {mime_type}")
123
+
124
+ # Generate a unique filename
125
+ content_hash = hashlib.md5(blob_content).hexdigest()[:8]
126
+ new_filename = f"flagged_image_{content_hash}{extension}"
127
+ print(f"DEBUG: Generated filename: {new_filename}")
128
+
129
+ # Create new path in the same directory
130
+ import tempfile
131
+ temp_dir = os.path.dirname(blob_path)
132
+ new_path = os.path.join(temp_dir, new_filename)
133
+ print(f"DEBUG: New path: {new_path}")
134
+
135
+ # Write the content to the new file
136
+ with open(new_path, 'wb') as f:
137
+ f.write(blob_content)
138
+
139
+ print(f"DEBUG: Successfully renamed blob to: {new_path}")
140
+
141
+ # Update the image data
142
+ converted_data = {
143
+ 'path': new_path,
144
+ 'url': image_data['url'].replace('blob', new_filename),
145
+ 'size': file_size,
146
+ 'orig_name': new_filename,
147
+ 'mime_type': mime_type,
148
+ 'is_stream': False,
149
+ 'meta': image_data.get('meta', {})
150
+ }
151
+
152
+ print(f"DEBUG: Converted data: {converted_data}")
153
+ return converted_data
154
+ else:
155
+ print("DEBUG: Not a blob, skipping conversion")
156
+
157
+ print(f"DEBUG: Converted image: {image_data}")
158
+ return image_data
159
+
160
+
161
  def flag_img_input(
162
  image: gr.Image, flag_option: str = "misdetection", username: str = "anonymous"
163
  ):
164
  """Wrapper for flagging"""
165
  print(f"{image=}, {flag_option=}, {username=}")
166
+
167
+ # Decode blob data if necessary
168
+ decoded_image = decode_blob_data(image)
169
+
170
+ hf_writer.flag([decoded_image], flag_option=flag_option, username=username)
171
 
172
 
173
  # Flagging