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
+ from typing import List, Dict, Any, Union, Optional, Tuple
11
+ import threading
12
+ from tabulate import tabulate
13
+ import tempfile
14
+ import shutil
15
+
16
+ # Import transformers modules
17
+ try:
18
+ from transformers import Qwen2_5_VLForConditionalGeneration, AutoProcessor
19
+ except ImportError:
20
+ print("Error: Could not import Qwen2_5_VLForConditionalGeneration")
21
+ print("Please install transformers from source:")
22
+ print("pip install git+https://github.com/huggingface/transformers")
23
+
24
+ # Global variables for tracking progress
25
+ total_images = 0
26
+ processed_images = 0
27
+ successful_images = 0
28
+ failed_images = 0
29
+ print_lock = threading.Lock()
30
+ model = None
31
+ processor = None
32
+
33
+ # =============== QWEN BATCH EXTRACTOR FUNCTIONS ===============
34
+
35
+ def load_image(image_path: str, max_size: int = 1024) -> Image.Image:
36
+ """
37
+ Load an image from a file path and resize it if needed to save memory.
38
+
39
+ Args:
40
+ image_path: Path to the image
41
+ max_size: Maximum dimension (width or height) for the image
42
+
43
+ Returns:
44
+ Resized PIL Image
45
+ """
46
+ try:
47
+ image = Image.open(image_path)
48
+
49
+ # Resize large images to save memory while maintaining aspect ratio
50
+ width, height = image.size
51
+ if width > max_size or height > max_size:
52
+ scale = max_size / max(width, height)
53
+ new_width = int(width * scale)
54
+ new_height = int(height * scale)
55
+
56
+ image = image.resize((new_width, new_height), Image.LANCZOS)
57
+
58
+ return image
59
+ except Exception as e:
60
+ raise ValueError(f"Failed to load or resize image {image_path}: {str(e)}")
61
+
62
+
63
+ def process_vision_info(messages: List[Dict[str, Any]]) -> tuple:
64
+ """Extract image inputs from messages."""
65
+ image_inputs = []
66
+ video_inputs = None # Setting to None instead of empty list
67
+
68
+ for message in messages:
69
+ if message["role"] != "user":
70
+ continue
71
+
72
+ for content in message["content"]:
73
+ if content["type"] == "image":
74
+ if isinstance(content["image"], str):
75
+ # Load image if it's a path or URL
76
+ image = load_image(content["image"])
77
+ image_inputs.append(image)
78
+ else:
79
+ # Assume it's already a PIL Image
80
+ image_inputs.append(content["image"])
81
+
82
+ return image_inputs, video_inputs
83
+
84
+
85
+ def extract_fields_from_response(response: str) -> Tuple[str, str, str]:
86
+ """
87
+ Extract name, affiliation, and town from the model's response.
88
+
89
+ Args:
90
+ response: The response from the model
91
+
92
+ Returns:
93
+ Tuple containing (name, affiliation, town)
94
+ """
95
+ # Initialize default values
96
+ name = ""
97
+ affiliation = ""
98
+ town = ""
99
+
100
+ # Use regex to extract fields
101
+ name_match = re.search(r"Name:\s*([^\n]+)", response)
102
+ affiliation_match = re.search(r"Affiliation:\s*([^\n]+)", response)
103
+ town_match = re.search(r"Town:\s*([^\n]+)", response)
104
+
105
+ # Extract fields if matches found
106
+ if name_match:
107
+ name = name_match.group(1).strip()
108
+ if affiliation_match:
109
+ affiliation = affiliation_match.group(1).strip()
110
+ if town_match:
111
+ town = town_match.group(1).strip()
112
+
113
+ return name, affiliation, town
114
+
115
+
116
+ def process_single_image(image_path: str, model, processor, device: str,
117
+ prompt: str, max_image_size: int, max_tokens: int,
118
+ progress=None) -> Dict:
119
+ """
120
+ Process a single image and extract name, affiliation, and town.
121
+
122
+ Args:
123
+ image_path: Path to the image
124
+ model: The loaded Qwen model
125
+ processor: The loaded processor
126
+ device: Device to run inference on ("cuda" or "cpu")
127
+ prompt: Text prompt to send to the model
128
+ max_image_size: Maximum dimension for input images
129
+ max_tokens: Maximum number of tokens to generate
130
+ progress: Gradio progress object
131
+
132
+ Returns:
133
+ Dictionary with extracted fields and metadata
134
+ """
135
+ global processed_images, successful_images, failed_images
136
+
137
+ result = {
138
+ "image_path": image_path,
139
+ "name": "",
140
+ "affiliation": "",
141
+ "town": "",
142
+ "success": False,
143
+ "error": "",
144
+ "time_taken": 0,
145
+ "response": ""
146
+ }
147
+
148
+ try:
149
+ t0 = time.time()
150
+
151
+ # Load and prepare image
152
+ image = load_image(image_path, max_size=max_image_size)
153
+
154
+ # Create message format expected by Qwen models
155
+ messages = [
156
+ {
157
+ "role": "user",
158
+ "content": [
159
+ {"type": "image", "image": image},
160
+ {"type": "text", "text": prompt}
161
+ ]
162
+ }
163
+ ]
164
+
165
+ # Prepare inputs
166
+ text = processor.apply_chat_template(
167
+ messages,
168
+ tokenize=False,
169
+ add_generation_prompt=True
170
+ )
171
+
172
+ image_inputs, video_inputs = process_vision_info(messages)
173
+
174
+ # Check if video_inputs is None, and handle accordingly
175
+ if video_inputs is None:
176
+ inputs = processor(
177
+ text=[text],
178
+ images=image_inputs,
179
+ padding=True,
180
+ return_tensors="pt"
181
+ )
182
+ else:
183
+ inputs = processor(
184
+ text=[text],
185
+ images=image_inputs,
186
+ videos=video_inputs,
187
+ padding=True,
188
+ return_tensors="pt"
189
+ )
190
+
191
+ # Move inputs to the appropriate device
192
+ inputs = inputs.to(device)
193
+
194
+ # Free some memory before generation
195
+ if torch.cuda.is_available():
196
+ torch.cuda.empty_cache()
197
+ gc.collect()
198
+
199
+ # Generate response with memory optimizations
200
+ with torch.no_grad():
201
+ generate_kwargs = {
202
+ "max_new_tokens": max_tokens,
203
+ "do_sample": False, # Use greedy decoding to save memory
204
+ "use_cache": True,
205
+ }
206
+
207
+ generated_ids = model.generate(
208
+ **inputs,
209
+ **generate_kwargs
210
+ )
211
+
212
+ # Decode only the newly generated tokens
213
+ generated_ids_trimmed = [
214
+ out_ids[len(in_ids):] for in_ids, out_ids in zip(inputs.input_ids, generated_ids)
215
+ ]
216
+
217
+ response = processor.batch_decode(
218
+ generated_ids_trimmed,
219
+ skip_special_tokens=True,
220
+ clean_up_tokenization_spaces=False
221
+ )[0] # Get first (and only) response
222
+
223
+ time_taken = time.time() - t0
224
+
225
+ # Extract fields from response
226
+ name, affiliation, town = extract_fields_from_response(response)
227
+
228
+ # Update result dictionary
229
+ result["name"] = name
230
+ result["affiliation"] = affiliation
231
+ result["town"] = town
232
+ result["success"] = True
233
+ result["time_taken"] = time_taken
234
+ result["response"] = response
235
+
236
+ with print_lock:
237
+ processed_images += 1
238
+ successful_images += 1
239
+ if progress is not None:
240
+ progress(processed_images / total_images, f"Processed: {processed_images}/{total_images} (Success: {successful_images}, Failed: {failed_images})")
241
+
242
+ except Exception as e:
243
+ error_msg = str(e)
244
+ stack_trace = traceback.format_exc()
245
+
246
+ with print_lock:
247
+ processed_images += 1
248
+ failed_images += 1
249
+ if progress is not None:
250
+ progress(processed_images / total_images, f"Processed: {processed_images}/{total_images} (Success: {successful_images}, Failed: {failed_images})")
251
+
252
+ result["error"] = error_msg
253
+ result["time_taken"] = time.time() - t0
254
+
255
+ # Clean up to free memory
256
+ if torch.cuda.is_available():
257
+ torch.cuda.empty_cache()
258
+ gc.collect()
259
+
260
+ return result
261
+
262
+
263
+ def load_model_and_processor(model_name, device, half_precision):
264
+ """Load model and processor for vision processing"""
265
+ global model, processor
266
+
267
+ # Set up dtype for model loading
268
+ if half_precision and device == "cuda":
269
+ dtype = torch.float16
270
+ else:
271
+ dtype = "auto"
272
+
273
+ # Low memory options for CUDA
274
+ attn_implementation = "sdpa" if device == "cuda" else None
275
+
276
+ # Load model and processor
277
+ print(f"Loading {model_name} model...")
278
+ t0 = time.time()
279
+
280
+ try:
281
+ model = Qwen2_5_VLForConditionalGeneration.from_pretrained(
282
+ model_name,
283
+ torch_dtype=dtype,
284
+ device_map=device,
285
+ attn_implementation=attn_implementation,
286
+ max_memory={0: "10GiB"} if device == "cuda" else None, # Limit GPU memory usage
287
+ )
288
+
289
+ processor = AutoProcessor.from_pretrained(model_name)
290
+ print(f"Model loaded in {time.time() - t0:.2f} s")
291
+
292
+ return True, f"Model loaded successfully in {time.time() - t0:.2f}s"
293
+ except Exception as e:
294
+ error_msg = f"Error loading model: {str(e)}"
295
+ print(error_msg)
296
+ return False, error_msg
297
+
298
+
299
+ def process_directory(directory_path: str, output_csv: str, model_name: str,
300
+ prompt: str, device: str, half_precision: bool,
301
+ max_image_size: int, max_tokens: int, progress=None) -> List[Dict]:
302
+ """
303
+ Process all images in a directory and save results to CSV.
304
+
305
+ Args:
306
+ directory_path: Path to directory containing images
307
+ output_csv: Path to output CSV file
308
+ model_name: Name of the Qwen model to use
309
+ prompt: Text prompt to send to the model
310
+ device: Device to run inference on ("auto", "cuda", or "cpu")
311
+ half_precision: Whether to use half precision for model
312
+ max_image_size: Maximum dimension for input images
313
+ max_tokens: Maximum number of tokens to generate
314
+ progress: Gradio progress object
315
+
316
+ Returns:
317
+ List of results for each image
318
+ """
319
+ global total_images, processed_images, successful_images, failed_images, model, processor
320
+
321
+ # Reset counters
322
+ total_images = 0
323
+ processed_images = 0
324
+ successful_images = 0
325
+ failed_images = 0
326
+
327
+ # Validate directory
328
+ if not os.path.isdir(directory_path):
329
+ raise ValueError(f"Directory does not exist: {directory_path}")
330
+
331
+ # Find all image files in directory
332
+ image_extensions = ('.jpg', '.jpeg', '.png', '.bmp', '.gif', '.webp')
333
+ image_files = [
334
+ os.path.join(directory_path, f) for f in os.listdir(directory_path)
335
+ if os.path.isfile(os.path.join(directory_path, f)) and
336
+ f.lower().endswith(image_extensions)
337
+ ]
338
+
339
+ if not image_files:
340
+ raise ValueError(f"No image files found in directory: {directory_path}")
341
+
342
+ total_images = len(image_files)
343
+
344
+ if progress is not None:
345
+ progress(0, f"Found {total_images} images to process")
346
+
347
+ # Enable garbage collection
348
+ gc.enable()
349
+
350
+ # Determine device
351
+ if device == "auto":
352
+ device = "cuda" if torch.cuda.is_available() else "cpu"
353
+
354
+ # Check if model is already loaded
355
+ if model is None or processor is None:
356
+ model_map = {
357
+ "qwen2-vl-2b": "Qwen/Qwen2-VL-2B-Instruct",
358
+ "qwen2.5-vl-3b": "Qwen/Qwen2.5-VL-3B-Instruct",
359
+ "qwen2.5-vl-7b": "Qwen/Qwen2.5-VL-7B-Instruct",
360
+ }
361
+ success, message = load_model_and_processor(model_map[model_name], device, half_precision)
362
+ if not success:
363
+ return [], message
364
+
365
+ results = []
366
+
367
+ # Process images sequentially
368
+ for i, image_path in enumerate(image_files):
369
+ if progress is not None:
370
+ progress(i / total_images, f"Processing image {i+1}/{total_images}: {os.path.basename(image_path)}")
371
+
372
+ result = process_single_image(
373
+ image_path, model, processor, device,
374
+ prompt, max_image_size, max_tokens, progress
375
+ )
376
+ results.append(result)
377
+
378
+ # Write results to CSV
379
+ with open(output_csv, 'w', newline='', encoding='utf-8') as csvfile:
380
+ import csv
381
+ fieldnames = ['image_path', 'name', 'affiliation', 'town', 'success', 'error', 'time_taken']
382
+ writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
383
+
384
+ writer.writeheader()
385
+ for result in results:
386
+ # Create a copy without the 'response' field for CSV output
387
+ csv_result = {k: v for k, v in result.items() if k != 'response'}
388
+ writer.writerow(csv_result)
389
+
390
+ # Create summary
391
+ summary = f"""
392
+ Processing complete!
393
+ Total images processed: {total_images}
394
+ Successful extractions: {successful_images}
395
+ Failed extractions: {failed_images}
396
+ Results saved to: {output_csv}
397
+ """
398
+
399
+ if progress is not None:
400
+ progress(1.0, f"Complete! Processed {total_images} images: {successful_images} successful, {failed_images} failed")
401
+
402
+ return results, summary
403
+
404
+
405
+ # =============== DATA ANALYZER FUNCTIONS ===============
406
+
407
+ def load_data(file_path):
408
+ """Load data from CSV file."""
409
+ if not os.path.exists(file_path):
410
+ return None, f"Error: File '{file_path}' not found."
411
+
412
+ try:
413
+ # Load CSV file with headers for name, affiliation, town
414
+ df = pd.read_csv(file_path)
415
+
416
+ # Ensure expected columns exist
417
+ required_columns = ['name', 'affiliation', 'town']
418
+ if not all(col.lower() in map(str.lower, df.columns) for col in required_columns):
419
+ return None, f"Error: CSV must contain columns for name, affiliation, and town."
420
+
421
+ # Standardize column names (case insensitive)
422
+ column_map = {}
423
+ for col in df.columns:
424
+ if col.lower() == 'name':
425
+ column_map[col] = 'name'
426
+ elif col.lower() == 'affiliation':
427
+ column_map[col] = 'affiliation'
428
+ elif col.lower() == 'town':
429
+ column_map[col] = 'town'
430
+
431
+ df = df.rename(columns=column_map)
432
+
433
+ # Convert all string columns to lowercase for case-insensitive operations
434
+ for col in ['name', 'affiliation', 'town']:
435
+ if df[col].dtype == object: # Check if column contains strings
436
+ df[col] = df[col].str.lower()
437
+
438
+ return df, "Data loaded successfully"
439
+
440
+ except Exception as e:
441
+ return None, f"Error loading CSV file: {e}"
442
+
443
+ def summary_by_town(df):
444
+ """Generate summary statistics by town - improved formatting."""
445
+ if df is None or len(df) == 0:
446
+ return "No data available for summary."
447
+
448
+ town_summary = df.groupby('town').agg(
449
+ total_people=('name', 'count'),
450
+ affiliations=('affiliation', lambda x: len(set(x)))
451
+ ).reset_index()
452
+
453
+ town_summary = town_summary.sort_values('total_people', ascending=False)
454
+
455
+ # Better column formatting
456
+ display_summary = town_summary.copy()
457
+ display_summary['town'] = display_summary['town'].str.title()
458
+ display_summary.columns = ['Town', 'People', 'Affiliations']
459
+
460
+ result = "\n" + "="*50 + "\n"
461
+ result += "SUMMARY BY TOWN\n"
462
+ result += "="*50 + "\n"
463
+
464
+ result += tabulate(
465
+ display_summary,
466
+ headers='keys',
467
+ tablefmt='psql',
468
+ showindex=False,
469
+ floatfmt='.0f'
470
+ )
471
+
472
+ # Display top affiliations for each town
473
+ result += "\n\n" + "="*50 + "\n"
474
+ result += "TOP AFFILIATIONS BY TOWN\n"
475
+ result += "="*50 + "\n"
476
+
477
+ for town in town_summary['town']:
478
+ town_data = df[df['town'] == town]
479
+ top_affiliations = town_data['affiliation'].value_counts().head(3)
480
+
481
+ result += f"\n🏙️ {town.upper()}:\n"
482
+ result += " " + "-"*30 + "\n"
483
+
484
+ for rank, (affiliation, count) in enumerate(top_affiliations.items(), 1):
485
+ result += f" {rank}. {affiliation.title():<20} → {count} people\n"
486
+
487
+ if len(top_affiliations) == 0:
488
+ result += " No data available\n"
489
+
490
+ return result
491
+
492
+ def summary_by_affiliation(df):
493
+ """Generate summary statistics by affiliation - improved version of your current function."""
494
+ if df is None or len(df) == 0:
495
+ return "No data available for summary."
496
+
497
+ affiliation_summary = df.groupby('affiliation').agg(
498
+ total_people=('name', 'count'),
499
+ towns=('town', lambda x: len(set(x)))
500
+ ).reset_index()
501
+
502
+ affiliation_summary = affiliation_summary.sort_values('total_people', ascending=False)
503
+
504
+ # Better column formatting
505
+ display_summary = affiliation_summary.copy()
506
+ display_summary['affiliation'] = display_summary['affiliation'].str.title()
507
+ display_summary.columns = ['Affiliation', 'People', 'Towns']
508
+
509
+ result = "\n" + "="*50 + "\n"
510
+ result += "SUMMARY BY AFFILIATION\n"
511
+ result += "="*50 + "\n"
512
+
513
+ # Use 'psql' format for better readability
514
+ result += tabulate(
515
+ display_summary,
516
+ headers='keys',
517
+ tablefmt='psql', # Changed from 'simple' to 'psql'
518
+ showindex=False,
519
+ floatfmt='.0f'
520
+ )
521
+
522
+ # Display top towns for each affiliation
523
+ result += "\n\n" + "="*50 + "\n"
524
+ result += "TOP TOWNS BY AFFILIATION\n"
525
+ result += "="*50 + "\n"
526
+
527
+ for affiliation in affiliation_summary['affiliation'].head(5).tolist():
528
+ affiliation_data = df[df['affiliation'] == affiliation]
529
+ top_towns = affiliation_data['town'].value_counts().head(3)
530
+
531
+ result += f"\n🏛️ {affiliation.upper()}:\n"
532
+ result += " " + "-"*30 + "\n"
533
+
534
+ for rank, (town, count) in enumerate(top_towns.items(), 1):
535
+ result += f" {rank}. {town.title():<20} → {count} people\n"
536
+
537
+ if len(top_towns) == 0:
538
+ result += " No data available\n"
539
+
540
+ return result
541
+
542
+
543
+ def search_data(df, search_term, search_field=None):
544
+ """Search for records by name, town, or affiliation."""
545
+ if df is None or len(df) == 0:
546
+ return "No data available for search."
547
+
548
+ if not search_term:
549
+ return "Please enter a search term."
550
+
551
+ search_term = search_term.lower() # Convert search term to lowercase for case-insensitive matching
552
+
553
+ if search_field and search_field.lower() in ['name', 'town', 'affiliation']:
554
+ # Search in specific field
555
+ field = search_field.lower()
556
+ results = df[df[field].str.contains(search_term, na=False)]
557
+ else:
558
+ # Search in all fields
559
+ results = df[
560
+ df['name'].str.contains(search_term, na=False) |
561
+ df['town'].str.contains(search_term, na=False) |
562
+ df['affiliation'].str.contains(search_term, na=False)
563
+ ]
564
+
565
+ if len(results) == 0:
566
+ return f"No results found for '{search_term}'"
567
+ else:
568
+ # Format results for display, converting back to title case for readability
569
+ display_results = results.copy()
570
+ for col in ['name', 'town', 'affiliation']:
571
+ display_results[col] = display_results[col].str.title()
572
+
573
+ # Only select the columns we want to display
574
+ display_results = display_results[['name', 'affiliation', 'town']]
575
+
576
+ result = f"=== SEARCH RESULTS ({len(results)} matches) ===\n"
577
+ result += tabulate(display_results, headers='keys', tablefmt='simple', showindex=False)
578
+ return result
579
+
580
+
581
+ # =============== GRADIO APP INTERFACE ===============
582
+
583
+ def copy_to_temp_dir(file_list):
584
+ """Copy uploaded files to a temporary directory"""
585
+ temp_dir = tempfile.mkdtemp()
586
+ file_paths = []
587
+
588
+ for file in file_list:
589
+ file_name = os.path.basename(file.name)
590
+ dst_path = os.path.join(temp_dir, file_name)
591
+ shutil.copy(file.name, dst_path)
592
+ file_paths.append(dst_path)
593
+
594
+ return temp_dir, file_paths
595
+
596
+
597
+ def unload_model():
598
+ """Unload the model to free up GPU memory"""
599
+ global model, processor
600
+
601
+ if model is not None:
602
+ del model
603
+ model = None
604
+
605
+ if processor is not None:
606
+ del processor
607
+ processor = None
608
+
609
+ if torch.cuda.is_available():
610
+ torch.cuda.empty_cache()
611
+ gc.collect()
612
+
613
+ return "Model unloaded successfully"
614
+
615
+
616
+ def process_images_tab(files, model_name, prompt, device, half_precision, max_image_size, max_tokens, progress=gr.Progress()):
617
+ """Function to handle the image processing tab"""
618
+ if not files:
619
+ return "", "Please upload some image files."
620
+
621
+ try:
622
+ # Copy uploaded files to a temporary directory
623
+ temp_dir, _ = copy_to_temp_dir(files)
624
+
625
+ # Process the directory of images
626
+ output_csv = os.path.join(temp_dir, "name_tags_results.csv")
627
+
628
+ # Determine device
629
+ if device == "auto":
630
+ device = "cuda" if torch.cuda.is_available() else "cpu"
631
+
632
+ # Process images
633
+ results, summary = process_directory(
634
+ directory_path=temp_dir,
635
+ output_csv=output_csv,
636
+ model_name=model_name,
637
+ prompt=prompt,
638
+ device=device,
639
+ half_precision=half_precision,
640
+ max_image_size=max_image_size,
641
+ max_tokens=max_tokens,
642
+ progress=progress
643
+ )
644
+
645
+ # Create a DataFrame from results
646
+ df = pd.DataFrame([{k: v for k, v in r.items() if k != 'response'} for r in results])
647
+
648
+ return output_csv, summary
649
+
650
+ except Exception as e:
651
+ return "", f"Error: {str(e)}\n{traceback.format_exc()}"
652
+
653
+
654
+ def analyze_csv_tab(csv_file):
655
+ """Function to handle the CSV analysis tab"""
656
+ if not csv_file:
657
+ return "Please upload or generate a CSV file first."
658
+
659
+ # Get the file path from the file object or string
660
+ if isinstance(csv_file, str):
661
+ file_path = csv_file
662
+ else:
663
+ file_path = csv_file.name
664
+
665
+ # Load data from CSV
666
+ df, message = load_data(file_path)
667
+ if df is None:
668
+ return message
669
+
670
+ # Generate overview
671
+ overview = f"""=== DATA OVERVIEW ===
672
+ Total records: {len(df)}
673
+ Unique towns: {df['town'].nunique()}
674
+ Unique affiliations: {df['affiliation'].nunique()}
675
+ """
676
+
677
+ return overview
678
+
679
+
680
+ def search_csv(csv_file, search_term, search_field):
681
+ """Function to search the CSV data"""
682
+ if not csv_file:
683
+ return "Please upload or generate a CSV file first."
684
+
685
+ if not search_term:
686
+ return "Please enter a search term."
687
+
688
+ # Get the file path from the file object or string
689
+ if isinstance(csv_file, str):
690
+ file_path = csv_file
691
+ else:
692
+ file_path = csv_file.name
693
+
694
+ # Load data from CSV
695
+ df, message = load_data(file_path)
696
+ if df is None:
697
+ return message
698
+
699
+ # Search the data
700
+ result = search_data(df, search_term, search_field)
701
+ return result
702
+
703
+
704
+ def summary_csv(csv_file, summary_type):
705
+ """Function to generate summaries from the CSV data"""
706
+ if not csv_file:
707
+ return "Please upload or generate a CSV file first."
708
+
709
+ # Get the file path from the file object or string
710
+ if isinstance(csv_file, str):
711
+ file_path = csv_file
712
+ else:
713
+ file_path = csv_file.name
714
+
715
+ # Load data from CSV
716
+ df, message = load_data(file_path)
717
+ if df is None:
718
+ return message
719
+
720
+ # Generate appropriate summary
721
+ if summary_type == "By Town":
722
+ result = summary_by_town(df)
723
+ elif summary_type == "By Affiliation":
724
+ result = summary_by_affiliation(df)
725
+ else:
726
+ result = "Please select a summary type."
727
+
728
+ return result
729
+
730
+
731
+ # Create the Gradio interface
732
+ with gr.Blocks(title="People Tag Analyzer") as app:
733
+ gr.Markdown("# People Tag Analyzer")
734
+ gr.Markdown("This app processes images of name tags to extract information and provides analysis tools.")
735
+
736
+ # Store CSV file path between tabs
737
+ csv_file_path = gr.State("")
738
+
739
+ with gr.Tabs():
740
+ # Image Processing Tab
741
+ with gr.Tab("Process Images"):
742
+ gr.Markdown("### Step 1: Upload Images")
743
+ with gr.Row():
744
+ image_files = gr.File(file_count="multiple", label="Upload Name Tag Images")
745
+
746
+ gr.Markdown("### Step 2: Configure Model")
747
+ with gr.Row():
748
+ with gr.Column():
749
+ model_name = gr.Dropdown(
750
+ choices=["qwen2-vl-2b", "qwen2.5-vl-3b", "qwen2.5-vl-7b"],
751
+ value="qwen2.5-vl-3b",
752
+ label="Vision Model"
753
+ )
754
+ device = gr.Dropdown(
755
+ choices=["auto", "cuda", "cpu"],
756
+ value="auto",
757
+ label="Device"
758
+ )
759
+
760
+ with gr.Column():
761
+ half_precision = gr.Checkbox(
762
+ value=True,
763
+ label="Use Half Precision (FP16)"
764
+ )
765
+ max_image_size = gr.Slider(
766
+ minimum=256,
767
+ maximum=2048,
768
+ value=768,
769
+ step=64,
770
+ label="Max Image Size"
771
+ )
772
+ max_tokens = gr.Slider(
773
+ minimum=64,
774
+ maximum=512,
775
+ value=256,
776
+ step=32,
777
+ label="Max Output Tokens"
778
+ )
779
+
780
+ gr.Markdown("### Step 3: Set Prompt")
781
+ prompt = gr.Textbox(
782
+ value="Extract 'name of the person', 'affiliation of the attendee' and also extract the town name you have to get it from the affiliation, then return the results in the format 'Name: Affiliation: Town:'",
783
+ label="Prompt",
784
+ lines=3
785
+ )
786
+
787
+ gr.Markdown("### Step 4: Process Images")
788
+ process_button = gr.Button("Process Images")
789
+ unload_button = gr.Button("Unload Model (Free Memory)")
790
+
791
+ with gr.Row():
792
+ output_csv = gr.Textbox(label="Output CSV Path")
793
+ processing_output = gr.Textbox(label="Processing Status", lines=10)
794
+
795
+ # Connect the process button
796
+ process_button.click(
797
+ fn=process_images_tab,
798
+ inputs=[image_files, model_name, prompt, device, half_precision, max_image_size, max_tokens],
799
+ outputs=[output_csv, processing_output],
800
+ api_name="process_images"
801
+ )
802
+
803
+ # Connect the unload button
804
+ unload_button.click(
805
+ fn=unload_model,
806
+ inputs=[],
807
+ outputs=[processing_output]
808
+ )
809
+
810
+ # Update state when CSV is generated
811
+ output_csv.change(
812
+ fn=lambda x: x,
813
+ inputs=[output_csv],
814
+ outputs=[csv_file_path]
815
+ )
816
+
817
+ # Data Analysis Tab
818
+ with gr.Tab("Analyze Data"):
819
+ gr.Markdown("### Data Input")
820
+ with gr.Row():
821
+ csv_input = gr.File(label="Upload CSV File")
822
+ use_processed = gr.Button("Use Processed CSV")
823
+
824
+ csv_status = gr.Textbox(label="CSV Status", lines=5)
825
+
826
+ # Analyze data when CSV is uploaded or selected
827
+ csv_input.change(
828
+ fn=analyze_csv_tab,
829
+ inputs=[csv_input],
830
+ outputs=[csv_status]
831
+ )
832
+
833
+ # Use processed CSV from first tab
834
+ use_processed.click(
835
+ fn=lambda x: x,
836
+ inputs=[csv_file_path],
837
+ outputs=[csv_input]
838
+ ).then(
839
+ fn=analyze_csv_tab,
840
+ inputs=[csv_file_path],
841
+ outputs=[csv_status]
842
+ )
843
+
844
+ gr.Markdown("### Summary")
845
+ with gr.Row():
846
+ summary_type = gr.Radio(
847
+ choices=["By Town", "By Affiliation"],
848
+ value="By Town",
849
+ label="Summary Type"
850
+ )
851
+ summary_button = gr.Button("Generate Summary")
852
+
853
+ summary_output = gr.Textbox(label="Summary Results", lines=20)
854
+
855
+ # Generate summary when button is clicked
856
+ summary_button.click(
857
+ fn=summary_csv,
858
+ inputs=[csv_input, summary_type],
859
+ outputs=[summary_output]
860
+ )
861
+
862
+ gr.Markdown("### Search")
863
+ with gr.Row():
864
+ with gr.Column():
865
+ search_term = gr.Textbox(label="Search Term")
866
+ search_field = gr.Dropdown(
867
+ choices=["All Fields", "Name", "Town", "Affiliation"],
868
+ value="All Fields",
869
+ label="Search In"
870
+ )
871
+ with gr.Column():
872
+ search_button = gr.Button("Search")
873
+
874
+ search_output = gr.Textbox(label="Search Results", lines=15)
875
+
876
+ # Search when button is clicked
877
+ search_button.click(
878
+ fn=search_csv,
879
+ inputs=[csv_input, search_term, search_field],
880
+ outputs=[search_output]
881
+ )
882
+
883
+ # Also search when Enter is pressed in search term
884
+ search_term.submit(
885
+ fn=search_csv,
886
+ inputs=[csv_input, search_term, search_field],
887
+ outputs=[search_output]
888
+ )
889
+
890
+ # Help/Instructions Tab
891
+ with gr.Tab("Help & Instructions"):
892
+ gr.Markdown("""
893
+ # People Tag Analyzer - User Guide
894
+
895
+ ## Overview
896
+ This application uses advanced vision models to extract names, affiliations, and towns from name tag images, then provides powerful analysis tools for the extracted data.
897
+
898
+ ## How to Use
899
+
900
+ ### 1. Process Images Tab
901
+
902
+ #### Step 1: Upload Images
903
+ - Click "Browse files" to select multiple name tag images
904
+ - Supported formats: JPG, JPEG, PNG, BMP, GIF, WEBP
905
+ - You can upload multiple images at once
906
+
907
+ #### Step 2: Configure Model
908
+ - **Vision Model**: Choose from available Qwen vision models
909
+ - `qwen2-vl-2b`: Fastest, least memory usage
910
+ - `qwen2.5-vl-3b`: Balanced performance (recommended)
911
+ - `qwen2.5-vl-7b`: Best accuracy, requires more memory
912
+ - **Device**:
913
+ - `auto`: Automatically detects GPU/CPU
914
+ - `cuda`: Force GPU usage (if available)
915
+ - `cpu`: Force CPU usage
916
+ - **Half Precision**: Use FP16 to save GPU memory (recommended for CUDA)
917
+ - **Max Image Size**: Resize large images to save memory (768px recommended)
918
+ - **Max Output Tokens**: Limit model output length (256 recommended)
919
+
920
+ #### Step 3: Set Prompt
921
+ The default prompt works well for most name tags. You can customize it if needed:
922
+ - The prompt tells the model what information to extract
923
+ - Format should specify the expected output structure
924
+
925
+ #### Step 4: Process Images
926
+ - Click "Process Images" to start extraction
927
+ - Progress will be shown in real-time
928
+ """)
929
+
930
+ # Main execution block
931
+ if __name__ == "__main__":
932
+ app.launch()