Skip to content

Commit 1f685bd

Browse files
committed
Update grounding demo
1 parent 638cf30 commit 1f685bd

File tree

1 file changed

+45
-18
lines changed

1 file changed

+45
-18
lines changed

examples/multimodal_grounding_qwen.py

Lines changed: 45 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,17 @@
1414
QWidget,
1515
QMessageBox,
1616
)
17-
from PyQt5.QtGui import QPixmap, QImage, QTextCursor, QPainter, QPen
17+
from PyQt5.QtGui import (
18+
QPixmap,
19+
QImage,
20+
QTextCursor,
21+
QPainter,
22+
QPen,
23+
QFont,
24+
QColor,
25+
QRegion,
26+
)
1827
from PyQt5.QtCore import Qt, pyqtSignal, QRect, QBuffer
19-
from PyQt5.QtWidgets import QLabel
2028

2129
from PIL import Image
2230
import requests
@@ -38,10 +46,6 @@
3846
ExLlamaV2Sampler,
3947
)
4048

41-
# Qwen2-VL 7B:
42-
# https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct
43-
# https://huggingface.co/turboderp/Qwen2-VL-7B-Instruct-exl2
44-
4549
class Model:
4650

4751
current_image: Image or None = None
@@ -188,7 +192,7 @@ def get_grounding_bb(self, start, end) -> tuple:
188192
)
189193
bb_string = "(" + bb_string
190194

191-
print(f"Model returned bounding box string: {bb_string}")
195+
print(f"Generation: {bb_string}")
192196
pprint.pprint(res, indent = 4)
193197

194198
# BB string is in the format "(x0,y0),(x1,y1)" with integer coordinates normalized to a range of 1000x1000
@@ -279,10 +283,20 @@ def clear_bounding_box(self):
279283
def paintEvent(self, event):
280284
"""Override paintEvent to draw the bounding box."""
281285
super().paintEvent(event)
286+
282287
if self.bounding_box:
283288
painter = QPainter(self)
284-
pen = QPen(Qt.red, 3) # Red bounding box with a width of 3
289+
overlay_color = QColor(64, 64, 64, 150)
290+
painter.setBrush(overlay_color)
291+
painter.setPen(Qt.NoPen)
292+
full_region = QRegion(self.rect())
293+
exclude_region = QRegion(self.bounding_box)
294+
clip_region = full_region.subtracted(exclude_region)
295+
painter.setClipRegion(clip_region)
296+
painter.drawRect(self.rect())
297+
pen = QPen(Qt.white, 2)
285298
painter.setPen(pen)
299+
painter.setBrush(overlay_color)
286300
painter.drawRect(self.bounding_box)
287301

288302
def dragEnterEvent(self, event):
@@ -305,13 +319,16 @@ def dropEvent(self, event):
305319
else:
306320
event.ignore()
307321

308-
def __init__(self, model: Model):
322+
def __init__(self, model: Model, title: str):
309323
super().__init__()
310324
self.model = model
311325
self.no_events_plz = False
312326

313-
self.setWindowTitle("Grounding Demo")
314-
self.setGeometry(100, 100, 900, 800)
327+
self.setWindowTitle(f"Grounding Demo - {title}")
328+
self.setGeometry(100, 100, 1000, 1000)
329+
330+
font = QFont()
331+
font.setPointSize(11) # Set the font size to 16 points
315332

316333
# Main layout
317334
self.central_widget = QWidget()
@@ -322,35 +339,39 @@ def __init__(self, model: Model):
322339
self.image_label = self.CustomQLabel(self, self.load_dropped_image)
323340
self.image_label.setText("Image goes here")
324341
self.image_label.setAlignment(Qt.AlignCenter)
325-
self.image_label.setStyleSheet("background-color: #4E4E4E; color: white;")
326-
main_layout.addWidget(self.image_label, stretch = 6)
342+
self.image_label.setStyleSheet("background-color: #404040; color: white;")
343+
self.image_label.setFont(font)
344+
main_layout.addWidget(self.image_label, stretch = 5)
327345

328346
# Button row
329347
button_layout = QHBoxLayout()
330348
main_layout.addLayout(button_layout)
331349

332350
self.paste_button = QPushButton("Paste Image")
333351
self.paste_button.clicked.connect(self.paste_image)
352+
self.paste_button.setFont(font)
334353
button_layout.addWidget(self.paste_button)
335354

336355
self.load_button = QPushButton("Load Image")
337356
self.load_button.clicked.connect(self.load_image)
357+
self.load_button.setFont(font)
338358
button_layout.addWidget(self.load_button)
339359

340360
self.inference_button = QPushButton("Inference")
341361
self.inference_button.clicked.connect(self.run_inference)
362+
self.inference_button.setFont(font)
342363
button_layout.addWidget(self.inference_button)
343364

344365
# Model output
345366
self.output_label = QLabel("Model Output:", self)
346367
self.output_label.setStyleSheet("color: white;")
368+
self.output_label.setFont(font)
347369
main_layout.addWidget(self.output_label)
348370

349371
self.output_text = self.CustomTextEdit(self)
350372
self.output_text.setReadOnly(True)
351-
self.output_text.setStyleSheet(
352-
"background-color: #3C3C3C; color: white;"
353-
)
373+
self.output_text.setStyleSheet("background-color: #3C3C3C; color: white;")
374+
self.output_text.setFont(font)
354375
main_layout.addWidget(self.output_text, stretch = 2)
355376

356377
self.output_text.selection_complete.connect(self.on_selection_made)
@@ -454,11 +475,17 @@ def on_selection_made(self, pos):
454475
a, b = self.model.get_grounding_bb(start, end)
455476
self.image_label.set_bounding_box(a, b)
456477

478+
479+
# Qwen2-VL 7B:
480+
# https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct
481+
# https://huggingface.co/turboderp/Qwen2-VL-7B-Instruct-exl2
482+
457483
def main():
484+
model_dir = "/mnt/str/models/qwen2-vl-7b-instruct-exl2/6.0bpw"
458485
app = QApplication(sys.argv)
459-
model = Model("/mnt/str/models/qwen2-vl-7b-instruct-exl2/5.0bpw")
486+
model = Model(model_dir)
460487
model.load()
461-
window = GroundingDemo(model)
488+
window = GroundingDemo(model, model_dir)
462489
window.show()
463490
sys.exit(app.exec_())
464491

0 commit comments

Comments
 (0)