hugohabicht01 commited on
Commit
b2fb4ce
·
1 Parent(s): 3de588c

make it possible to edit system prompt

Browse files
Files changed (2) hide show
  1. app.py +16 -10
  2. utils.py +1 -1
app.py CHANGED
@@ -20,9 +20,7 @@ MAX_NEW_TOKENS = 2048
20
  TEMPERATURE = 1.0
21
  MIN_P = 0.1
22
  SYSTEM_PROMPT = """You are a helpful assistant for privacy analysis of images. Please always answer in English. Please obey the users instructions and follow the provided format."""
23
- DEFAULT_PROMPT = """
24
-
25
- You are an expert at pixel perfect image analysis and in privacy. Your task is to find all private data in the image and report its position, as well as explanations as to why it is private data. Private data is all data that relates to a unique person and can be used to identify them.
26
 
27
  First write down your thoughts within a <think> block.
28
  Please go through all objects in the image and consider whether they are private data or not.
@@ -52,9 +50,11 @@ Here is the image to analyse, start your analysis directly after:
52
  """
53
 
54
 
55
- def build_messages(image, history: Optional[list[dict[str, Any]]] = None, prompt: Optional[str] = None):
56
  if not prompt:
57
  prompt = DEFAULT_PROMPT
 
 
58
 
59
  if history:
60
  return [
@@ -68,7 +68,7 @@ def build_messages(image, history: Optional[list[dict[str, Any]]] = None, prompt
68
  "content": [
69
  {
70
  "type": "text",
71
- "text": SYSTEM_PROMPT,
72
  }
73
  ],
74
  },
@@ -118,7 +118,7 @@ def anonymise_image(input_image_np: np.ndarray, boxes: list[BoundingBox]):
118
  raise gr.Error(f"Failed to initialize Blurnonymizer. Check logs. Error: {e}")
119
 
120
 
121
- def run_model_inference(input_image_pil: Image.Image, prompt_text: str):
122
  """
123
  Runs model inference on the input image and prompt.
124
  """
@@ -127,7 +127,9 @@ def run_model_inference(input_image_pil: Image.Image, prompt_text: str):
127
  print("Running model inference...")
128
  messages = build_messages(
129
  input_image_pil,
130
- prompt=prompt_text)
 
 
131
 
132
  input_text = tokenizer.apply_chat_template(
133
  messages, tokenize=False, add_generation_prompt=True
@@ -176,7 +178,7 @@ def run_model_inference(input_image_pil: Image.Image, prompt_text: str):
176
 
177
 
178
  @spaces.GPU(duration=90) # Request GPU for this function, allow up to 120 seconds
179
- def analyze_image(input_image_pil: Image.Image, prompt_text: str):
180
  """
181
  Analyzes the input image using the VLM, visualizes findings, and anonymizes.
182
  """
@@ -184,9 +186,11 @@ def analyze_image(input_image_pil: Image.Image, prompt_text: str):
184
  raise gr.Error("Please upload an image.")
185
  if not prompt_text:
186
  raise gr.Error("Please provide a prompt.")
 
 
187
 
188
  try:
189
- raw_model_output, image_height, image_width = run_model_inference(input_image_pil, prompt_text)
190
  except Exception as e:
191
  print(f"Error during model inference: {e}")
192
  print(traceback.format_exc())
@@ -196,6 +200,7 @@ def analyze_image(input_image_pil: Image.Image, prompt_text: str):
196
 
197
  return raw_model_output, visualized_image_np, anonymized_image_np
198
 
 
199
  @spaces.GPU(duration=90)
200
  def perform_anonymisation(input_image_pil: Image.Image, raw_model_output: str) -> tuple[np.ndarray, np.ndarray]:
201
  original_image_np = np.array(input_image_pil)
@@ -276,6 +281,7 @@ with gr.Blocks() as demo:
276
  with gr.Row():
277
  with gr.Column(scale=1):
278
  input_image = gr.Image(type="pil", label="Upload Image")
 
279
  prompt_textbox = gr.Textbox(
280
  label="Analysis Prompt", value=DEFAULT_PROMPT, lines=4
281
  )
@@ -295,7 +301,7 @@ with gr.Blocks() as demo:
295
 
296
  analyze_button.click(
297
  fn=analyze_image,
298
- inputs=[input_image, prompt_textbox],
299
  outputs=[raw_output, output_visualized, output_anonymized],
300
  )
301
 
 
20
  TEMPERATURE = 1.0
21
  MIN_P = 0.1
22
  SYSTEM_PROMPT = """You are a helpful assistant for privacy analysis of images. Please always answer in English. Please obey the users instructions and follow the provided format."""
23
+ DEFAULT_PROMPT = """You are an expert at pixel perfect image analysis and in privacy. Your task is to find all private data in the image and report its position, as well as explanations as to why it is private data. Private data is all data that relates to a unique person and can be used to identify them.
 
 
24
 
25
  First write down your thoughts within a <think> block.
26
  Please go through all objects in the image and consider whether they are private data or not.
 
50
  """
51
 
52
 
53
+ def build_messages(image, history: Optional[list[dict[str, Any]]] = None, prompt: Optional[str] = None, system_prompt_text: Optional[str] = None):
54
  if not prompt:
55
  prompt = DEFAULT_PROMPT
56
+ if not system_prompt_text:
57
+ system_prompt_text = SYSTEM_PROMPT # Fallback if not provided
58
 
59
  if history:
60
  return [
 
68
  "content": [
69
  {
70
  "type": "text",
71
+ "text": system_prompt_text, # Use the passed system prompt
72
  }
73
  ],
74
  },
 
118
  raise gr.Error(f"Failed to initialize Blurnonymizer. Check logs. Error: {e}")
119
 
120
 
121
+ def run_model_inference(input_image_pil: Image.Image, prompt_text: str, system_prompt_text: str):
122
  """
123
  Runs model inference on the input image and prompt.
124
  """
 
127
  print("Running model inference...")
128
  messages = build_messages(
129
  input_image_pil,
130
+ prompt=prompt_text,
131
+ system_prompt_text=system_prompt_text # Pass system prompt here
132
+ )
133
 
134
  input_text = tokenizer.apply_chat_template(
135
  messages, tokenize=False, add_generation_prompt=True
 
178
 
179
 
180
  @spaces.GPU(duration=90) # Request GPU for this function, allow up to 120 seconds
181
+ def analyze_image(input_image_pil: Image.Image, prompt_text: str, system_prompt_text: str):
182
  """
183
  Analyzes the input image using the VLM, visualizes findings, and anonymizes.
184
  """
 
186
  raise gr.Error("Please upload an image.")
187
  if not prompt_text:
188
  raise gr.Error("Please provide a prompt.")
189
+ if not system_prompt_text:
190
+ raise gr.Error("Please provide a system prompt.") # Added check
191
 
192
  try:
193
+ raw_model_output, image_height, image_width = run_model_inference(input_image_pil, prompt_text, system_prompt_text) # Pass system prompt
194
  except Exception as e:
195
  print(f"Error during model inference: {e}")
196
  print(traceback.format_exc())
 
200
 
201
  return raw_model_output, visualized_image_np, anonymized_image_np
202
 
203
+
204
  @spaces.GPU(duration=90)
205
  def perform_anonymisation(input_image_pil: Image.Image, raw_model_output: str) -> tuple[np.ndarray, np.ndarray]:
206
  original_image_np = np.array(input_image_pil)
 
281
  with gr.Row():
282
  with gr.Column(scale=1):
283
  input_image = gr.Image(type="pil", label="Upload Image")
284
+ system_prompt_input = gr.Textbox(label="System Prompt", value=SYSTEM_PROMPT, lines=5, interactive=True) # New system prompt input
285
  prompt_textbox = gr.Textbox(
286
  label="Analysis Prompt", value=DEFAULT_PROMPT, lines=4
287
  )
 
301
 
302
  analyze_button.click(
303
  fn=analyze_image,
304
+ inputs=[input_image, prompt_textbox, system_prompt_input], # Add system_prompt_input here
305
  outputs=[raw_output, output_visualized, output_anonymized],
306
  )
307
 
utils.py CHANGED
@@ -361,7 +361,7 @@ def visualize_boxes_annotated(image: np.ndarray | Image.Image, boxes: list[Bound
361
  ax.add_patch(rect)
362
 
363
  # Add label text above the box
364
- ax.text(x_min, y_min-5, label, color=color, fontsize=5,
365
  bbox=dict(facecolor='white', alpha=0.7, edgecolor='none'))
366
 
367
  # Instead of displaying, save to numpy array
 
361
  ax.add_patch(rect)
362
 
363
  # Add label text above the box
364
+ ax.text(x_min, y_min-5, label, color=color, fontsize=4,
365
  bbox=dict(facecolor='white', alpha=0.7, edgecolor='none'))
366
 
367
  # Instead of displaying, save to numpy array