piky commited on
Commit
3b1b323
·
verified ·
1 Parent(s): bb2bd9b

Update app.py

Browse files

Fix the summary logic

Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -42,12 +42,7 @@ def detect_objects(image_url, uploaded_image, text_prompt):
42
  return None, "Please provide an image URL or upload an image."
43
 
44
  # Default prompt fallback
45
- if not text_prompt or not text_prompt.strip():
46
- text_prompt = "capsule"
47
-
48
- text_prompt = normalize_prompt(text_prompt)
49
-
50
- display_prompt = text_prompt.strip() if text_prompt else "capsule"
51
  model_prompt = normalize_prompt(display_prompt)
52
 
53
  # Inference
@@ -59,7 +54,7 @@ def detect_objects(image_url, uploaded_image, text_prompt):
59
  results = processor.post_process_grounded_object_detection(
60
  outputs,
61
  inputs.input_ids,
62
- threshold=0.35,
63
  target_sizes=[image.size[::-1]]
64
  )
65
 
@@ -101,17 +96,28 @@ def detect_objects(image_url, uploaded_image, text_prompt):
101
  """, image
102
 
103
  counts = Counter(detected_labels)
104
- total = sum(counts.values())
105
 
106
  summary_rows = []
107
 
108
  for label, count in counts.items():
109
- object_name = label if count == 1 else label + "s"
110
  summary_rows.append(
111
- f"<tr><td style='padding:4px 12px'>{object_name}</td><td style='padding:4px 12px'><b>{count}</b></td></tr>"
 
112
  )
113
 
114
- summary = f"<h3>Detected {total} object(s) for: {display_prompt}</h3>"
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  return summary, image
117
 
 
42
  return None, "Please provide an image URL or upload an image."
43
 
44
  # Default prompt fallback
45
+ display_prompt = text_prompt.strip() if text_prompt and text_prompt.strip() else "capsule"
 
 
 
 
 
46
  model_prompt = normalize_prompt(display_prompt)
47
 
48
  # Inference
 
54
  results = processor.post_process_grounded_object_detection(
55
  outputs,
56
  inputs.input_ids,
57
+ threshold=0.34,
58
  target_sizes=[image.size[::-1]]
59
  )
60
 
 
96
  """, image
97
 
98
  counts = Counter(detected_labels)
 
99
 
100
  summary_rows = []
101
 
102
  for label, count in counts.items():
 
103
  summary_rows.append(
104
+ f"<tr><td style='padding:4px 12px'>{label}</td>"
105
+ f"<td style='padding:4px 12px'><b>{count}</b></td></tr>"
106
  )
107
 
108
+ total_types = len(counts)
109
+
110
+ summary = f"""
111
+ <h3>Detected {total_types} object type(s) for: {display_prompt}</h3>
112
+
113
+ <table style='border-collapse: collapse; width: 100%;'>
114
+ <tr>
115
+ <th style='text-align:left; padding:4px 12px;'>Object</th>
116
+ <th style='text-align:left; padding:4px 12px;'>Count</th>
117
+ </tr>
118
+ {''.join(summary_rows)}
119
+ </table>
120
+ """
121
 
122
  return summary, image
123