14
14
QWidget ,
15
15
QMessageBox ,
16
16
)
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
+ )
18
27
from PyQt5 .QtCore import Qt , pyqtSignal , QRect , QBuffer
19
- from PyQt5 .QtWidgets import QLabel
20
28
21
29
from PIL import Image
22
30
import requests
38
46
ExLlamaV2Sampler ,
39
47
)
40
48
41
- # Qwen2-VL 7B:
42
- # https://huggingface.co/Qwen/Qwen2-VL-7B-Instruct
43
- # https://huggingface.co/turboderp/Qwen2-VL-7B-Instruct-exl2
44
-
45
49
class Model :
46
50
47
51
current_image : Image or None = None
@@ -188,7 +192,7 @@ def get_grounding_bb(self, start, end) -> tuple:
188
192
)
189
193
bb_string = "(" + bb_string
190
194
191
- print (f"Model returned bounding box string : { bb_string } " )
195
+ print (f"Generation : { bb_string } " )
192
196
pprint .pprint (res , indent = 4 )
193
197
194
198
# 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):
279
283
def paintEvent (self , event ):
280
284
"""Override paintEvent to draw the bounding box."""
281
285
super ().paintEvent (event )
286
+
282
287
if self .bounding_box :
283
288
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 )
285
298
painter .setPen (pen )
299
+ painter .setBrush (overlay_color )
286
300
painter .drawRect (self .bounding_box )
287
301
288
302
def dragEnterEvent (self , event ):
@@ -305,13 +319,16 @@ def dropEvent(self, event):
305
319
else :
306
320
event .ignore ()
307
321
308
- def __init__ (self , model : Model ):
322
+ def __init__ (self , model : Model , title : str ):
309
323
super ().__init__ ()
310
324
self .model = model
311
325
self .no_events_plz = False
312
326
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
315
332
316
333
# Main layout
317
334
self .central_widget = QWidget ()
@@ -322,35 +339,39 @@ def __init__(self, model: Model):
322
339
self .image_label = self .CustomQLabel (self , self .load_dropped_image )
323
340
self .image_label .setText ("Image goes here" )
324
341
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 )
327
345
328
346
# Button row
329
347
button_layout = QHBoxLayout ()
330
348
main_layout .addLayout (button_layout )
331
349
332
350
self .paste_button = QPushButton ("Paste Image" )
333
351
self .paste_button .clicked .connect (self .paste_image )
352
+ self .paste_button .setFont (font )
334
353
button_layout .addWidget (self .paste_button )
335
354
336
355
self .load_button = QPushButton ("Load Image" )
337
356
self .load_button .clicked .connect (self .load_image )
357
+ self .load_button .setFont (font )
338
358
button_layout .addWidget (self .load_button )
339
359
340
360
self .inference_button = QPushButton ("Inference" )
341
361
self .inference_button .clicked .connect (self .run_inference )
362
+ self .inference_button .setFont (font )
342
363
button_layout .addWidget (self .inference_button )
343
364
344
365
# Model output
345
366
self .output_label = QLabel ("Model Output:" , self )
346
367
self .output_label .setStyleSheet ("color: white;" )
368
+ self .output_label .setFont (font )
347
369
main_layout .addWidget (self .output_label )
348
370
349
371
self .output_text = self .CustomTextEdit (self )
350
372
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 )
354
375
main_layout .addWidget (self .output_text , stretch = 2 )
355
376
356
377
self .output_text .selection_complete .connect (self .on_selection_made )
@@ -454,11 +475,17 @@ def on_selection_made(self, pos):
454
475
a , b = self .model .get_grounding_bb (start , end )
455
476
self .image_label .set_bounding_box (a , b )
456
477
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
+
457
483
def main ():
484
+ model_dir = "/mnt/str/models/qwen2-vl-7b-instruct-exl2/6.0bpw"
458
485
app = QApplication (sys .argv )
459
- model = Model ("/mnt/str/models/qwen2-vl-7b-instruct-exl2/5.0bpw" )
486
+ model = Model (model_dir )
460
487
model .load ()
461
- window = GroundingDemo (model )
488
+ window = GroundingDemo (model , model_dir )
462
489
window .show ()
463
490
sys .exit (app .exec_ ())
464
491
0 commit comments