File size: 48,102 Bytes
25d2eb7 2827b8a 7a1cd7a f5eb405 2827b8a f5eb405 3b4c438 f5eb405 3b4c438 f5eb405 3b4c438 f5eb405 5422464 2827b8a 5422464 504b6fc 2827b8a f5eb405 2827b8a 4b1ac5a 2827b8a f5eb405 4b1ac5a 2827b8a 5422464 2827b8a f5eb405 2827b8a f5eb405 2827b8a 5422464 2827b8a 5422464 504b6fc 2827b8a 4b1ac5a 2827b8a f5eb405 4b1ac5a 2827b8a 5422464 f5eb405 2827b8a f5eb405 2827b8a 25d2eb7 7a1cd7a 892ceeb 7a1cd7a 2827b8a 7a1cd7a 7ed3881 3b4c438 892ceeb 2827b8a 5422464 f5eb405 5422464 f5eb405 9f13004 f5eb405 3b4c438 9f13004 f5eb405 4b1ac5a 9f13004 4b1ac5a f5eb405 4b1ac5a 9f13004 4b1ac5a f5eb405 9f13004 5422464 3b4c438 f5eb405 5422464 9f13004 f5eb405 3b4c438 9f13004 f5eb405 3b4c438 9f13004 f5eb405 3b4c438 9f13004 4b1ac5a 3b4c438 9f13004 4b1ac5a f5eb405 3b4c438 9f13004 4b1ac5a 3b4c438 9f13004 4b1ac5a f5eb405 9f13004 5422464 3b4c438 f5eb405 5422464 9f13004 f5eb405 5422464 3b4c438 2827b8a 3b4c438 7ed3881 3b4c438 7ed3881 3b4c438 7ed3881 7a1cd7a 3b4c438 7ed3881 3b4c438 7ed3881 3b4c438 2827b8a 3b4c438 9f13004 3b4c438 7ed3881 2827b8a 7ed3881 3b4c438 7ed3881 3b4c438 2827b8a 7a1cd7a 9f13004 2827b8a 9f13004 2827b8a f5eb405 5422464 4b1ac5a 504b6fc 2ba6e60 471be58 2ba6e60 3b4c438 2ba6e60 471be58 2ba6e60 3b4c438 2ba6e60 3b4c438 2ba6e60 3b4c438 2ba6e60 471be58 2ba6e60 471be58 2ba6e60 3b4c438 2ba6e60 3b4c438 2ba6e60 f5eb405 3b4c438 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 |
import gradio as gr
from datasets import load_dataset
import numpy as np
from model2vec import StaticModel
from reach import Reach
from difflib import ndiff
import sys
import tqdm
# Load the model at startup
model = StaticModel.from_pretrained("minishlab/M2V_base_output")
# Update default dataset to 'sst2' and set default threshold to 0.9
default_dataset1_name = "sst2"
default_dataset1_split = "train"
default_dataset2_name = "sst2"
default_dataset2_split = "validation"
default_text_column = "sentence"
default_threshold = 0.9
# Load the default datasets at startup
ds_default1 = load_dataset(default_dataset1_name, split=default_dataset1_split)
ds_default2 = load_dataset(default_dataset2_name, split=default_dataset2_split)
def deduplicate(embedding_matrix: np.ndarray, threshold: float, batch_size: int = 1024) -> tuple[np.ndarray, dict[int, int]]:
"""
Deduplicate embeddings and return the deduplicated indices and a mapping of removed indices to their corresponding original indices.
"""
# Building the index
reach = Reach(vectors=embedding_matrix, items=[str(i) for i in range(len(embedding_matrix))])
deduplicated_indices = set(range(len(embedding_matrix)))
duplicate_to_original_mapping = {}
# Finding nearest neighbors
results = reach.nearest_neighbor_threshold(
embedding_matrix,
threshold=threshold,
batch_size=batch_size,
show_progressbar=True # Allow internal progress bar
)
# Processing duplicates
for i, similar_items in enumerate(results):
if i not in deduplicated_indices:
continue
similar_indices = [int(item[0]) for item in similar_items if int(item[0]) != i]
for sim_idx in similar_indices:
if sim_idx in deduplicated_indices:
deduplicated_indices.remove(sim_idx)
duplicate_to_original_mapping[sim_idx] = i
return np.array(list(deduplicated_indices)), duplicate_to_original_mapping
def deduplicate_across_datasets(embedding_matrix_1: np.ndarray, embedding_matrix_2: np.ndarray, threshold: float, batch_size: int = 1024) -> tuple[list[int], dict[int, int]]:
"""
Deduplicate embeddings across two datasets and return the indices of duplicates between them.
"""
# Building the index from Dataset 1
reach = Reach(vectors=embedding_matrix_1, items=[str(i) for i in range(len(embedding_matrix_1))])
duplicate_indices_in_test = []
duplicate_to_original_mapping = {}
# Finding nearest neighbors between datasets
results = reach.nearest_neighbor_threshold(
embedding_matrix_2,
threshold=threshold,
batch_size=batch_size,
show_progressbar=True # Allow internal progress bar
)
# Processing duplicates
for i, similar_items in enumerate(results):
similar_indices = [int(item[0]) for item in similar_items if item[1] >= threshold]
if similar_indices:
duplicate_indices_in_test.append(i)
duplicate_to_original_mapping[i] = similar_indices[0]
return duplicate_indices_in_test, duplicate_to_original_mapping
def display_word_differences(x: str, y: str) -> str:
diff = ndiff(x.split(), y.split())
return " ".join([word for word in diff if word.startswith(('+', '-'))])
def perform_deduplication(
deduplication_type,
dataset1_name,
dataset1_split,
dataset1_text_column,
dataset2_name="",
dataset2_split="",
dataset2_text_column="",
threshold=default_threshold,
progress=gr.Progress(track_tqdm=True)
):
# Deep Monkey-Patching of tqdm
original_tqdm = tqdm.tqdm
tqdm.tqdm = progress.tqdm
for mod_name in list(sys.modules.keys()):
if 'tqdm' in mod_name:
sys.modules[mod_name].tqdm = progress.tqdm
try:
# Convert threshold to float
threshold = float(threshold)
# Initialize status message
status = ""
if deduplication_type == "Single dataset":
# Load Dataset 1
status = "Loading Dataset 1..."
yield status, ""
if dataset1_name == default_dataset1_name and dataset1_split == default_dataset1_split:
ds = ds_default1
else:
ds = load_dataset(dataset1_name, split=dataset1_split)
# Extract texts
status = "Extracting texts from Dataset 1..."
yield status, ""
texts = [example[dataset1_text_column] for example in ds]
# Compute embeddings
status = "Computing embeddings for Dataset 1..."
yield status, ""
embedding_matrix = model.encode(texts, show_progressbar=True) # Enable internal progress bar
# Deduplicate
status = "Deduplicating embeddings..."
yield status, ""
deduplicated_indices, duplicate_to_original_mapping = deduplicate(
embedding_matrix, threshold
)
# Prepare the results
num_duplicates = len(duplicate_to_original_mapping)
num_total = len(texts)
num_deduplicated = len(deduplicated_indices)
result_text = f"**Total documents:** {num_total}\n"
result_text += f"**Number of duplicates found:** {num_duplicates}\n"
result_text += f"**Number of unique documents after deduplication:** {num_deduplicated}\n\n"
# Show deduplicated examples
if num_duplicates > 0:
result_text += "**Examples of duplicates found:**\n\n"
num_examples = min(5, num_duplicates)
for duplicate_idx, original_idx in list(duplicate_to_original_mapping.items())[:num_examples]:
original_text = texts[original_idx]
duplicate_text = texts[duplicate_idx]
differences = display_word_differences(original_text, duplicate_text)
result_text += f"**Original text:**\n{original_text}\n\n"
result_text += f"**Duplicate text:**\n{duplicate_text}\n\n"
result_text += f"**Differences:**\n{differences}\n"
result_text += "-" * 50 + "\n\n"
else:
result_text += "No duplicates found."
# Final status
status = "Deduplication completed."
yield status, result_text
elif deduplication_type == "Cross-dataset":
# Load Dataset 1
status = "Loading Dataset 1..."
yield status, ""
if dataset1_name == default_dataset1_name and dataset1_split == default_dataset1_split:
ds1 = ds_default1
else:
ds1 = load_dataset(dataset1_name, split=dataset1_split)
# Load Dataset 2
status = "Loading Dataset 2..."
yield status, ""
if dataset2_name == default_dataset2_name and dataset2_split == default_dataset2_split:
ds2 = ds_default2
else:
ds2 = load_dataset(dataset2_name, split=dataset2_split)
# Extract texts from Dataset 1
status = "Extracting texts from Dataset 1..."
yield status, ""
texts1 = [example[dataset1_text_column] for example in ds1]
# Extract texts from Dataset 2
status = "Extracting texts from Dataset 2..."
yield status, ""
texts2 = [example[dataset2_text_column] for example in ds2]
# Compute embeddings for Dataset 1
status = "Computing embeddings for Dataset 1..."
yield status, ""
embedding_matrix1 = model.encode(texts1, show_progressbar=True)
# Compute embeddings for Dataset 2
status = "Computing embeddings for Dataset 2..."
yield status, ""
embedding_matrix2 = model.encode(texts2, show_progressbar=True)
# Deduplicate across datasets
status = "Deduplicating embeddings across datasets..."
yield status, ""
duplicate_indices_in_ds2, duplicate_to_original_mapping = deduplicate_across_datasets(
embedding_matrix1, embedding_matrix2, threshold
)
num_duplicates = len(duplicate_indices_in_ds2)
num_total_ds2 = len(texts2)
num_unique_ds2 = num_total_ds2 - num_duplicates
result_text = f"**Total documents in {dataset2_name}/{dataset2_split}:** {num_total_ds2}\n"
result_text += f"**Number of duplicates found in {dataset2_name}/{dataset2_split}:** {num_duplicates}\n"
result_text += f"**Number of unique documents in {dataset2_name}/{dataset2_split} after deduplication:** {num_unique_ds2}\n\n"
# Show deduplicated examples
if num_duplicates > 0:
result_text += "**Examples of duplicates found in Dataset 2:**\n\n"
num_examples = min(5, num_duplicates)
for duplicate_idx in duplicate_indices_in_ds2[:num_examples]:
original_idx = duplicate_to_original_mapping[duplicate_idx]
original_text = texts1[original_idx]
duplicate_text = texts2[duplicate_idx]
differences = display_word_differences(original_text, duplicate_text)
result_text += f"**Original text (Dataset 1):**\n{original_text}\n\n"
result_text += f"**Duplicate text (Dataset 2):**\n{duplicate_text}\n\n"
result_text += f"**Differences:**\n{differences}\n"
result_text += "-" * 50 + "\n\n"
else:
result_text += "No duplicates found."
# Final status
status = "Deduplication completed."
yield status, result_text
finally:
# Restore original tqdm
tqdm.tqdm = original_tqdm
for mod_name in list(sys.modules.keys()):
if 'tqdm' in mod_name:
sys.modules[mod_name].tqdm = original_tqdm
with gr.Blocks() as demo:
gr.Markdown("# Semantic Deduplication")
deduplication_type = gr.Radio(
choices=["Single dataset", "Cross-dataset"],
label="Deduplication Type",
value="Single dataset"
)
with gr.Row():
dataset1_name = gr.Textbox(value=default_dataset1_name, label="Dataset 1 Name")
dataset1_split = gr.Textbox(value=default_dataset1_split, label="Dataset 1 Split")
dataset1_text_column = gr.Textbox(value=default_text_column, label="Text Column Name")
dataset2_inputs = gr.Column(visible=False)
with dataset2_inputs:
gr.Markdown("### Dataset 2")
with gr.Row():
dataset2_name = gr.Textbox(value=default_dataset2_name, label="Dataset 2 Name")
dataset2_split = gr.Textbox(value=default_dataset2_split, label="Dataset 2 Split")
dataset2_text_column = gr.Textbox(value=default_text_column, label="Text Column Name")
threshold = gr.Slider(
minimum=0.0,
maximum=1.0,
value=default_threshold,
label="Similarity Threshold"
)
compute_button = gr.Button("Compute")
status_output = gr.Markdown()
result_output = gr.Markdown()
# Function to update the visibility of dataset2_inputs
def update_visibility(deduplication_type_value):
if deduplication_type_value == "Cross-dataset":
return gr.update(visible=True)
else:
return gr.update(visible=False)
deduplication_type.change(
update_visibility,
inputs=deduplication_type,
outputs=dataset2_inputs
)
compute_button.click(
fn=perform_deduplication,
inputs=[
deduplication_type,
dataset1_name,
dataset1_split,
dataset1_text_column,
dataset2_name,
dataset2_split,
dataset2_text_column,
threshold
],
outputs=[status_output, result_output]
)
demo.launch()
# import gradio as gr
# from datasets import load_dataset
# import numpy as np
# from model2vec import StaticModel
# from reach import Reach
# from difflib import ndiff
# import sys
# import tqdm
# # Load the model at startup
# model = StaticModel.from_pretrained("minishlab/M2V_base_output")
# # Update default dataset to 'sst2' and set default threshold to 0.9
# default_dataset1_name = "sst2"
# default_dataset1_split = "train"
# default_dataset2_name = "sst2"
# default_dataset2_split = "validation"
# default_text_column = "sentence"
# default_threshold = 0.9
# # Load the default datasets at startup
# ds_default1 = load_dataset(default_dataset1_name, split=default_dataset1_split)
# ds_default2 = load_dataset(default_dataset2_name, split=default_dataset2_split)
# def deduplicate(embedding_matrix: np.ndarray, threshold: float, batch_size: int = 1024, progress=None) -> tuple[np.ndarray, dict[int, int]]:
# """
# Deduplicate embeddings and return the deduplicated indices and a mapping of removed indices to their corresponding original indices.
# """
# # Update progress to indicate building the index
# progress(0, desc="Building search index...")
# reach = Reach(vectors=embedding_matrix, items=[str(i) for i in range(len(embedding_matrix))])
# deduplicated_indices = set(range(len(embedding_matrix)))
# duplicate_to_original_mapping = {}
# # Finding nearest neighbors
# progress(0, desc="Finding nearest neighbors...")
# results = reach.nearest_neighbor_threshold(
# embedding_matrix,
# threshold=threshold,
# batch_size=batch_size,
# show_progressbar=True # Allow internal progress bar
# )
# # Processing duplicates with a progress bar
# total_items = len(embedding_matrix)
# for i, similar_items in enumerate(progress.tqdm(results, desc="Processing duplicates", total=total_items)):
# if i not in deduplicated_indices:
# continue
# similar_indices = [int(item[0]) for item in similar_items if int(item[0]) != i]
# for sim_idx in similar_indices:
# if sim_idx in deduplicated_indices:
# deduplicated_indices.remove(sim_idx)
# duplicate_to_original_mapping[sim_idx] = i
# return np.array(list(deduplicated_indices)), duplicate_to_original_mapping
# def deduplicate_across_datasets(embedding_matrix_1: np.ndarray, embedding_matrix_2: np.ndarray, threshold: float, batch_size: int = 1024, progress=None) -> tuple[list[int], dict[int, int]]:
# """
# Deduplicate embeddings across two datasets and return the indices of duplicates between them.
# """
# # Update progress to indicate building the index
# progress(0, desc="Building search index from Dataset 1...")
# reach = Reach(vectors=embedding_matrix_1, items=[str(i) for i in range(len(embedding_matrix_1))])
# duplicate_indices_in_test = []
# duplicate_to_original_mapping = {}
# # Finding nearest neighbors between datasets
# progress(0, desc="Finding nearest neighbors between datasets...")
# results = reach.nearest_neighbor_threshold(
# embedding_matrix_2,
# threshold=threshold,
# batch_size=batch_size,
# show_progressbar=True # Allow internal progress bar
# )
# total_items = len(embedding_matrix_2)
# # Processing duplicates with a progress bar
# for i, similar_items in enumerate(progress.tqdm(results, desc="Processing duplicates across datasets", total=total_items)):
# similar_indices = [int(item[0]) for item in similar_items if item[1] >= threshold]
# if similar_indices:
# duplicate_indices_in_test.append(i)
# duplicate_to_original_mapping[i] = similar_indices[0]
# return duplicate_indices_in_test, duplicate_to_original_mapping
# def display_word_differences(x: str, y: str) -> str:
# diff = ndiff(x.split(), y.split())
# return " ".join([word for word in diff if word.startswith(('+', '-'))])
# def perform_deduplication(
# deduplication_type,
# dataset1_name,
# dataset1_split,
# dataset1_text_column,
# dataset2_name="",
# dataset2_split="",
# dataset2_text_column="",
# threshold=default_threshold,
# progress=gr.Progress(track_tqdm=True)
# ):
# # Monkey-patch tqdm
# original_tqdm = tqdm.tqdm
# original_reach_tqdm = Reach.__dict__['tqdm'] if 'tqdm' in Reach.__dict__ else None
# tqdm.tqdm = progress.tqdm
# sys.modules['tqdm'].tqdm = progress.tqdm
# sys.modules['tqdm.auto'].tqdm = progress.tqdm
# Reach.tqdm = progress.tqdm # Monkey-patch reach's tqdm
# try:
# # Convert threshold to float
# threshold = float(threshold)
# if deduplication_type == "Single dataset":
# # Load Dataset 1
# progress(0, desc="Loading Dataset 1...")
# if dataset1_name == default_dataset1_name and dataset1_split == default_dataset1_split:
# ds = ds_default1
# else:
# ds = load_dataset(dataset1_name, split=dataset1_split)
# # Extract texts
# progress(0, desc="Extracting texts from Dataset 1...")
# texts = [example[dataset1_text_column] for example in ds]
# # Compute embeddings
# progress(0, desc="Computing embeddings for Dataset 1...")
# embedding_matrix = model.encode(texts, show_progressbar=True) # Enable internal progress bar
# # Deduplicate
# result_text = deduplicate_and_prepare_results_single(
# embedding_matrix, texts, threshold, progress
# )
# return result_text
# elif deduplication_type == "Cross-dataset":
# # Load Dataset 1
# progress(0, desc="Loading Dataset 1...")
# if dataset1_name == default_dataset1_name and dataset1_split == default_dataset1_split:
# ds1 = ds_default1
# else:
# ds1 = load_dataset(dataset1_name, split=dataset1_split)
# # Load Dataset 2
# progress(0, desc="Loading Dataset 2...")
# if dataset2_name == default_dataset2_name and dataset2_split == default_dataset2_split:
# ds2 = ds_default2
# else:
# ds2 = load_dataset(dataset2_name, split=dataset2_split)
# # Extract texts from Dataset 1
# progress(0, desc="Extracting texts from Dataset 1...")
# texts1 = [example[dataset1_text_column] for example in ds1]
# # Extract texts from Dataset 2
# progress(0, desc="Extracting texts from Dataset 2...")
# texts2 = [example[dataset2_text_column] for example in ds2]
# # Compute embeddings for Dataset 1
# progress(0, desc="Computing embeddings for Dataset 1...")
# embedding_matrix1 = model.encode(texts1, show_progressbar=True)
# # Compute embeddings for Dataset 2
# progress(0, desc="Computing embeddings for Dataset 2...")
# embedding_matrix2 = model.encode(texts2, show_progressbar=True)
# # Deduplicate across datasets
# result_text = deduplicate_and_prepare_results_cross(
# embedding_matrix1, embedding_matrix2, texts1, texts2, threshold, progress, dataset2_name, dataset2_split
# )
# return result_text
# finally:
# # Restore original tqdm
# tqdm.tqdm = original_tqdm
# sys.modules['tqdm'].tqdm = original_tqdm
# sys.modules['tqdm.auto'].tqdm = original_tqdm
# # Restore reach's original tqdm
# if original_reach_tqdm is not None:
# Reach.tqdm = original_reach_tqdm
# else:
# del Reach.tqdm # If it wasn't originally in Reach's __dict__
# def deduplicate_and_prepare_results_single(embedding_matrix, texts, threshold, progress):
# # Deduplicate
# deduplicated_indices, duplicate_to_original_mapping = deduplicate(
# embedding_matrix, threshold, progress=progress
# )
# # Prepare the results
# num_duplicates = len(duplicate_to_original_mapping)
# num_total = len(texts)
# num_deduplicated = len(deduplicated_indices)
# result_text = f"**Total documents:** {num_total}\n"
# result_text += f"**Number of duplicates found:** {num_duplicates}\n"
# result_text += f"**Number of unique documents after deduplication:** {num_deduplicated}\n\n"
# # Show deduplicated examples
# if num_duplicates > 0:
# result_text += "**Examples of duplicates found:**\n\n"
# num_examples = min(5, num_duplicates)
# for duplicate_idx, original_idx in list(duplicate_to_original_mapping.items())[:num_examples]:
# original_text = texts[original_idx]
# duplicate_text = texts[duplicate_idx]
# differences = display_word_differences(original_text, duplicate_text)
# result_text += f"**Original text:**\n{original_text}\n\n"
# result_text += f"**Duplicate text:**\n{duplicate_text}\n\n"
# result_text += f"**Differences:**\n{differences}\n"
# result_text += "-" * 50 + "\n\n"
# else:
# result_text += "No duplicates found."
# return result_text
# def deduplicate_and_prepare_results_cross(embedding_matrix1, embedding_matrix2, texts1, texts2, threshold, progress, dataset2_name, dataset2_split):
# # Deduplicate across datasets
# duplicate_indices_in_ds2, duplicate_to_original_mapping = deduplicate_across_datasets(
# embedding_matrix1, embedding_matrix2, threshold, progress=progress
# )
# num_duplicates = len(duplicate_indices_in_ds2)
# num_total_ds2 = len(texts2)
# num_unique_ds2 = num_total_ds2 - num_duplicates
# result_text = f"**Total documents in {dataset2_name}/{dataset2_split}:** {num_total_ds2}\n"
# result_text += f"**Number of duplicates found in {dataset2_name}/{dataset2_split}:** {num_duplicates}\n"
# result_text += f"**Number of unique documents in {dataset2_name}/{dataset2_split} after deduplication:** {num_unique_ds2}\n\n"
# # Show deduplicated examples
# if num_duplicates > 0:
# result_text += "**Examples of duplicates found in Dataset 2:**\n\n"
# num_examples = min(5, num_duplicates)
# for duplicate_idx in duplicate_indices_in_ds2[:num_examples]:
# original_idx = duplicate_to_original_mapping[duplicate_idx]
# original_text = texts1[original_idx]
# duplicate_text = texts2[duplicate_idx]
# differences = display_word_differences(original_text, duplicate_text)
# result_text += f"**Original text (Dataset 1):**\n{original_text}\n\n"
# result_text += f"**Duplicate text (Dataset 2):**\n{duplicate_text}\n\n"
# result_text += f"**Differences:**\n{differences}\n"
# result_text += "-" * 50 + "\n\n"
# else:
# result_text += "No duplicates found."
# return result_text
# with gr.Blocks() as demo:
# gr.Markdown("# Semantic Deduplication")
# deduplication_type = gr.Radio(
# choices=["Single dataset", "Cross-dataset"],
# label="Deduplication Type",
# value="Single dataset"
# )
# with gr.Row():
# dataset1_name = gr.Textbox(value=default_dataset1_name, label="Dataset 1 Name")
# dataset1_split = gr.Textbox(value=default_dataset1_split, label="Dataset 1 Split")
# dataset1_text_column = gr.Textbox(value=default_text_column, label="Text Column Name")
# dataset2_inputs = gr.Column(visible=False)
# with dataset2_inputs:
# gr.Markdown("### Dataset 2")
# with gr.Row():
# dataset2_name = gr.Textbox(value=default_dataset2_name, label="Dataset 2 Name")
# dataset2_split = gr.Textbox(value=default_dataset2_split, label="Dataset 2 Split")
# dataset2_text_column = gr.Textbox(value=default_text_column, label="Text Column Name")
# threshold = gr.Slider(
# minimum=0.0,
# maximum=1.0,
# value=default_threshold,
# label="Similarity Threshold"
# )
# compute_button = gr.Button("Compute")
# output = gr.Markdown()
# # Function to update the visibility of dataset2_inputs
# def update_visibility(deduplication_type_value):
# if deduplication_type_value == "Cross-dataset":
# return gr.update(visible=True)
# else:
# return gr.update(visible=False)
# deduplication_type.change(
# update_visibility,
# inputs=deduplication_type,
# outputs=dataset2_inputs
# )
# compute_button.click(
# fn=perform_deduplication,
# inputs=[
# deduplication_type,
# dataset1_name,
# dataset1_split,
# dataset1_text_column,
# dataset2_name,
# dataset2_split,
# dataset2_text_column,
# threshold
# ],
# outputs=output
# )
# demo.launch()
# import gradio as gr
# from datasets import load_dataset
# import numpy as np
# from model2vec import StaticModel
# from reach import Reach
# from difflib import ndiff
# import sys
# import tqdm
# # Load the model at startup
# model = StaticModel.from_pretrained("minishlab/M2V_base_output")
# # Load the default datasets at startup
# default_dataset1_name = "ag_news"
# default_dataset1_split = "train"
# default_dataset2_name = "ag_news"
# default_dataset2_split = "test"
# ds_default1 = load_dataset(default_dataset1_name, split=default_dataset1_split)
# ds_default2 = load_dataset(default_dataset2_name, split=default_dataset2_split)
# def deduplicate(embedding_matrix: np.ndarray, threshold: float, batch_size: int = 1024, progress=None) -> tuple[np.ndarray, dict[int, int]]:
# """
# Deduplicate embeddings and return the deduplicated indices and a mapping of removed indices to their corresponding original indices.
# """
# reach = Reach(vectors=embedding_matrix, items=[str(i) for i in range(len(embedding_matrix))])
# deduplicated_indices = set(range(len(embedding_matrix)))
# duplicate_to_original_mapping = {}
# results = reach.nearest_neighbor_threshold(
# embedding_matrix,
# threshold=threshold,
# batch_size=batch_size,
# show_progressbar=True # Allow internal progress bar
# )
# # Process duplicates
# for i, similar_items in enumerate(progress.tqdm(results, desc="Processing duplicates", total=len(embedding_matrix))):
# if i not in deduplicated_indices:
# continue
# similar_indices = [int(item[0]) for item in similar_items if int(item[0]) != i]
# for sim_idx in similar_indices:
# if sim_idx in deduplicated_indices:
# deduplicated_indices.remove(sim_idx)
# duplicate_to_original_mapping[sim_idx] = i
# return np.array(list(deduplicated_indices)), duplicate_to_original_mapping
# def deduplicate_across_datasets(embedding_matrix_1: np.ndarray, embedding_matrix_2: np.ndarray, threshold: float, batch_size: int = 1024, progress=None) -> tuple[list[int], dict[int, int]]:
# """
# Deduplicate embeddings across two datasets and return the indices of duplicates between them.
# """
# reach = Reach(vectors=embedding_matrix_1, items=[str(i) for i in range(len(embedding_matrix_1))])
# duplicate_indices_in_test = []
# duplicate_to_original_mapping = {}
# results = reach.nearest_neighbor_threshold(
# embedding_matrix_2,
# threshold=threshold,
# batch_size=batch_size,
# show_progressbar=True # Allow internal progress bar
# )
# for i, similar_items in enumerate(progress.tqdm(results, desc="Processing duplicates across datasets", total=len(embedding_matrix_2))):
# similar_indices = [int(item[0]) for item in similar_items if item[1] >= threshold]
# if similar_indices:
# duplicate_indices_in_test.append(i)
# duplicate_to_original_mapping[i] = similar_indices[0]
# return duplicate_indices_in_test, duplicate_to_original_mapping
# def display_word_differences(x: str, y: str) -> str:
# diff = ndiff(x.split(), y.split())
# return " ".join([word for word in diff if word.startswith(('+', '-'))])
# def perform_deduplication(
# deduplication_type,
# dataset1_name,
# dataset1_split,
# dataset1_text_column,
# dataset2_name="",
# dataset2_split="",
# dataset2_text_column="",
# threshold=0.8,
# progress=gr.Progress(track_tqdm=True)
# ):
# # Monkey-patch tqdm
# original_tqdm = tqdm.tqdm
# original_reach_tqdm = Reach.__dict__['tqdm'] if 'tqdm' in Reach.__dict__ else None
# tqdm.tqdm = progress.tqdm
# sys.modules['tqdm'].tqdm = progress.tqdm
# sys.modules['tqdm.auto'].tqdm = progress.tqdm
# Reach.tqdm = progress.tqdm # Monkey-patch reach's tqdm
# try:
# # Convert threshold to float
# threshold = float(threshold)
# if deduplication_type == "Single dataset":
# # Check if the dataset is the default one
# if dataset1_name == default_dataset1_name and dataset1_split == default_dataset1_split:
# ds = ds_default1
# else:
# ds = load_dataset(dataset1_name, split=dataset1_split)
# # Extract texts
# texts = [example[dataset1_text_column] for example in ds]
# # Compute embeddings
# embedding_matrix = model.encode(texts, show_progressbar=True) # Enable internal progress bar
# # Deduplicate
# deduplicated_indices, duplicate_to_original_mapping = deduplicate(embedding_matrix, threshold, progress=progress)
# # Prepare the results
# num_duplicates = len(duplicate_to_original_mapping)
# num_total = len(texts)
# num_deduplicated = len(deduplicated_indices)
# result_text = f"**Total documents:** {num_total}\n"
# result_text += f"**Number of duplicates found:** {num_duplicates}\n"
# result_text += f"**Number of unique documents after deduplication:** {num_deduplicated}\n\n"
# # Show deduplicated examples
# result_text += "**Examples of duplicates found:**\n\n"
# num_examples = min(5, num_duplicates)
# for duplicate_idx, original_idx in list(duplicate_to_original_mapping.items())[:num_examples]:
# original_text = texts[original_idx]
# duplicate_text = texts[duplicate_idx]
# differences = display_word_differences(original_text, duplicate_text)
# result_text += f"**Original text:**\n{original_text}\n\n"
# result_text += f"**Duplicate text:**\n{duplicate_text}\n\n"
# result_text += f"**Differences:**\n{differences}\n"
# result_text += "-" * 50 + "\n\n"
# return result_text
# elif deduplication_type == "Cross-dataset":
# # Dataset 1
# if dataset1_name == default_dataset1_name and dataset1_split == default_dataset1_split:
# ds1 = ds_default1
# else:
# ds1 = load_dataset(dataset1_name, split=dataset1_split)
# # Dataset 2
# if dataset2_name == default_dataset2_name and dataset2_split == default_dataset2_split:
# ds2 = ds_default2
# else:
# ds2 = load_dataset(dataset2_name, split=dataset2_split)
# # Extract texts
# texts1 = [example[dataset1_text_column] for example in ds1]
# texts2 = [example[dataset2_text_column] for example in ds2]
# # Compute embeddings
# embedding_matrix1 = model.encode(texts1, show_progressbar=True) # Enable internal progress bar
# embedding_matrix2 = model.encode(texts2, show_progressbar=True) # Enable internal progress bar
# # Deduplicate across datasets
# duplicate_indices_in_ds2, duplicate_to_original_mapping = deduplicate_across_datasets(
# embedding_matrix1, embedding_matrix2, threshold, progress=progress)
# num_duplicates = len(duplicate_indices_in_ds2)
# num_total_ds2 = len(texts2)
# num_unique_ds2 = num_total_ds2 - num_duplicates
# result_text = f"**Total documents in {dataset2_name}/{dataset2_split}:** {num_total_ds2}\n"
# result_text += f"**Number of duplicates found in {dataset2_name}/{dataset2_split}:** {num_duplicates}\n"
# result_text += f"**Number of unique documents in {dataset2_name}/{dataset2_split} after deduplication:** {num_unique_ds2}\n\n"
# # Show deduplicated examples
# result_text += "**Examples of duplicates found in Dataset 2:**\n\n"
# num_examples = min(5, num_duplicates)
# for duplicate_idx in duplicate_indices_in_ds2[:num_examples]:
# original_idx = duplicate_to_original_mapping[duplicate_idx]
# original_text = texts1[original_idx]
# duplicate_text = texts2[duplicate_idx]
# differences = display_word_differences(original_text, duplicate_text)
# result_text += f"**Original text (Dataset 1):**\n{original_text}\n\n"
# result_text += f"**Duplicate text (Dataset 2):**\n{duplicate_text}\n\n"
# result_text += f"**Differences:**\n{differences}\n"
# result_text += "-" * 50 + "\n\n"
# return result_text
# finally:
# # Restore original tqdm
# tqdm.tqdm = original_tqdm
# sys.modules['tqdm'].tqdm = original_tqdm
# sys.modules['tqdm.auto'].tqdm = original_tqdm
# # Restore reach's original tqdm
# if original_reach_tqdm is not None:
# Reach.tqdm = original_reach_tqdm
# else:
# del Reach.tqdm # If it wasn't originally in Reach's __dict__
# with gr.Blocks() as demo:
# gr.Markdown("# Semantic Deduplication")
# deduplication_type = gr.Radio(
# choices=["Single dataset", "Cross-dataset"],
# label="Deduplication Type",
# value="Single dataset"
# )
# with gr.Row():
# dataset1_name = gr.Textbox(value="ag_news", label="Dataset 1 Name")
# dataset1_split = gr.Textbox(value="train", label="Dataset 1 Split")
# dataset1_text_column = gr.Textbox(value="text", label="Text Column Name")
# dataset2_inputs = gr.Column(visible=False)
# with dataset2_inputs:
# gr.Markdown("### Dataset 2")
# with gr.Row():
# dataset2_name = gr.Textbox(value="ag_news", label="Dataset 2 Name")
# dataset2_split = gr.Textbox(value="test", label="Dataset 2 Split")
# dataset2_text_column = gr.Textbox(value="text", label="Text Column Name")
# threshold = gr.Slider(
# minimum=0.0,
# maximum=1.0,
# value=0.8,
# label="Similarity Threshold"
# )
# compute_button = gr.Button("Compute")
# output = gr.Markdown()
# # Function to update the visibility of dataset2_inputs
# def update_visibility(deduplication_type_value):
# if deduplication_type_value == "Cross-dataset":
# return gr.update(visible=True)
# else:
# return gr.update(visible=False)
# deduplication_type.change(
# update_visibility,
# inputs=deduplication_type,
# outputs=dataset2_inputs
# )
# compute_button.click(
# fn=perform_deduplication,
# inputs=[
# deduplication_type,
# dataset1_name,
# dataset1_split,
# dataset1_text_column,
# dataset2_name,
# dataset2_split,
# dataset2_text_column,
# threshold
# ],
# outputs=output
# )
# demo.launch()
# # import gradio as gr
# # from datasets import load_dataset
# # import numpy as np
# # from model2vec import StaticModel
# # from reach import Reach
# # from difflib import ndiff
# # import sys
# # import tqdm
# # # Load the model at startup
# # model = StaticModel.from_pretrained("minishlab/M2V_base_output")
# # # Load the default datasets at startup
# # default_dataset1_name = "ag_news"
# # default_dataset1_split = "train"
# # default_dataset2_name = "ag_news"
# # default_dataset2_split = "test"
# # ds_default1 = load_dataset(default_dataset1_name, split=default_dataset1_split)
# # ds_default2 = load_dataset(default_dataset2_name, split=default_dataset2_split)
# # def deduplicate(embedding_matrix: np.ndarray, threshold: float, batch_size: int = 1024, progress=None) -> tuple[np.ndarray, dict[int, int]]:
# # """
# # Deduplicate embeddings and return the deduplicated indices and a mapping of removed indices to their corresponding original indices.
# # """
# # reach = Reach(vectors=embedding_matrix, items=[str(i) for i in range(len(embedding_matrix))])
# # deduplicated_indices = set(range(len(embedding_matrix)))
# # duplicate_to_original_mapping = {}
# # results = reach.nearest_neighbor_threshold(
# # embedding_matrix,
# # threshold=threshold,
# # batch_size=batch_size,
# # show_progressbar=True # Allow internal progress bar
# # )
# # # Process duplicates
# # for i, similar_items in enumerate(progress.tqdm(results, desc="Processing duplicates")):
# # if i not in deduplicated_indices:
# # continue
# # similar_indices = [int(item[0]) for item in similar_items if int(item[0]) != i]
# # for sim_idx in similar_indices:
# # if sim_idx in deduplicated_indices:
# # deduplicated_indices.remove(sim_idx)
# # duplicate_to_original_mapping[sim_idx] = i
# # return np.array(list(deduplicated_indices)), duplicate_to_original_mapping
# # def deduplicate_across_datasets(embedding_matrix_1: np.ndarray, embedding_matrix_2: np.ndarray, threshold: float, batch_size: int = 1024, progress=None) -> tuple[list[int], dict[int, int]]:
# # """
# # Deduplicate embeddings across two datasets and return the indices of duplicates between them.
# # """
# # reach = Reach(vectors=embedding_matrix_1, items=[str(i) for i in range(len(embedding_matrix_1))])
# # duplicate_indices_in_test = []
# # duplicate_to_original_mapping = {}
# # results = reach.nearest_neighbor_threshold(
# # embedding_matrix_2,
# # threshold=threshold,
# # batch_size=batch_size,
# # show_progressbar=True # Allow internal progress bar
# # )
# # # Process duplicates
# # for i, similar_items in enumerate(progress.tqdm(results, desc="Processing duplicates across datasets")):
# # similar_indices = [int(item[0]) for item in similar_items if item[1] >= threshold]
# # if similar_indices:
# # duplicate_indices_in_test.append(i)
# # duplicate_to_original_mapping[i] = similar_indices[0]
# # return duplicate_indices_in_test, duplicate_to_original_mapping
# # def display_word_differences(x: str, y: str) -> str:
# # diff = ndiff(x.split(), y.split())
# # return " ".join([word for word in diff if word.startswith(('+', '-'))])
# # def perform_deduplication(
# # deduplication_type,
# # dataset1_name,
# # dataset1_split,
# # dataset1_text_column,
# # dataset2_name="",
# # dataset2_split="",
# # dataset2_text_column="",
# # threshold=0.8,
# # progress=gr.Progress(track_tqdm=True)
# # ):
# # # Monkey-patch tqdm
# # original_tqdm = tqdm.tqdm
# # tqdm.tqdm = progress.tqdm
# # sys.modules['tqdm'].tqdm = progress.tqdm
# # sys.modules['tqdm.auto'].tqdm = progress.tqdm
# # try:
# # # Convert threshold to float
# # threshold = float(threshold)
# # if deduplication_type == "Single dataset":
# # # Check if the dataset is the default one
# # if dataset1_name == default_dataset1_name and dataset1_split == default_dataset1_split:
# # ds = ds_default1
# # else:
# # ds = load_dataset(dataset1_name, split=dataset1_split)
# # # Extract texts
# # texts = [example[dataset1_text_column] for example in ds]
# # # Compute embeddings
# # embedding_matrix = model.encode(texts, show_progressbar=True) # Enable internal progress bar
# # # Deduplicate
# # deduplicated_indices, duplicate_to_original_mapping = deduplicate(embedding_matrix, threshold, progress=progress)
# # # Prepare the results
# # num_duplicates = len(duplicate_to_original_mapping)
# # num_total = len(texts)
# # num_deduplicated = len(deduplicated_indices)
# # result_text = f"**Total documents:** {num_total}\n"
# # result_text += f"**Number of duplicates found:** {num_duplicates}\n"
# # result_text += f"**Number of unique documents after deduplication:** {num_deduplicated}\n\n"
# # # Show deduplicated examples
# # result_text += "**Examples of duplicates found:**\n\n"
# # num_examples = min(5, num_duplicates)
# # for duplicate_idx, original_idx in list(duplicate_to_original_mapping.items())[:num_examples]:
# # original_text = texts[original_idx]
# # duplicate_text = texts[duplicate_idx]
# # differences = display_word_differences(original_text, duplicate_text)
# # result_text += f"**Original text:**\n{original_text}\n\n"
# # result_text += f"**Duplicate text:**\n{duplicate_text}\n\n"
# # result_text += f"**Differences:**\n{differences}\n"
# # result_text += "-" * 50 + "\n\n"
# # return result_text
# # elif deduplication_type == "Cross-dataset":
# # # Dataset 1
# # if dataset1_name == default_dataset1_name and dataset1_split == default_dataset1_split:
# # ds1 = ds_default1
# # else:
# # ds1 = load_dataset(dataset1_name, split=dataset1_split)
# # # Dataset 2
# # if dataset2_name == default_dataset2_name and dataset2_split == default_dataset2_split:
# # ds2 = ds_default2
# # else:
# # ds2 = load_dataset(dataset2_name, split=dataset2_split)
# # # Extract texts
# # texts1 = [example[dataset1_text_column] for example in ds1]
# # texts2 = [example[dataset2_text_column] for example in ds2]
# # # Compute embeddings
# # embedding_matrix1 = model.encode(texts1, show_progressbar=True) # Enable internal progress bar
# # embedding_matrix2 = model.encode(texts2, show_progressbar=True) # Enable internal progress bar
# # # Deduplicate across datasets
# # duplicate_indices_in_ds2, duplicate_to_original_mapping = deduplicate_across_datasets(embedding_matrix1, embedding_matrix2, threshold, progress=progress)
# # num_duplicates = len(duplicate_indices_in_ds2)
# # num_total_ds2 = len(texts2)
# # num_unique_ds2 = num_total_ds2 - num_duplicates
# # result_text = f"**Total documents in {dataset2_name}/{dataset2_split}:** {num_total_ds2}\n"
# # result_text += f"**Number of duplicates found in {dataset2_name}/{dataset2_split}:** {num_duplicates}\n"
# # result_text += f"**Number of unique documents in {dataset2_name}/{dataset2_split} after deduplication:** {num_unique_ds2}\n\n"
# # # Show deduplicated examples
# # result_text += "**Examples of duplicates found in Dataset 2:**\n\n"
# # num_examples = min(5, num_duplicates)
# # for duplicate_idx in duplicate_indices_in_ds2[:num_examples]:
# # original_idx = duplicate_to_original_mapping[duplicate_idx]
# # original_text = texts1[original_idx]
# # duplicate_text = texts2[duplicate_idx]
# # differences = display_word_differences(original_text, duplicate_text)
# # result_text += f"**Original text (Dataset 1):**\n{original_text}\n\n"
# # result_text += f"**Duplicate text (Dataset 2):**\n{duplicate_text}\n\n"
# # result_text += f"**Differences:**\n{differences}\n"
# # result_text += "-" * 50 + "\n\n"
# # return result_text
# # finally:
# # # Restore original tqdm
# # tqdm.tqdm = original_tqdm
# # sys.modules['tqdm'].tqdm = original_tqdm
# # sys.modules['tqdm.auto'].tqdm = original_tqdm
# # with gr.Blocks() as demo:
# # gr.Markdown("# Semantic Deduplication")
# # deduplication_type = gr.Radio(
# # choices=["Single dataset", "Cross-dataset"],
# # label="Deduplication Type",
# # value="Single dataset"
# # )
# # with gr.Row():
# # dataset1_name = gr.Textbox(value="ag_news", label="Dataset 1 Name")
# # dataset1_split = gr.Textbox(value="train", label="Dataset 1 Split")
# # dataset1_text_column = gr.Textbox(value="text", label="Text Column Name")
# # dataset2_inputs = gr.Column(visible=False)
# # with dataset2_inputs:
# # gr.Markdown("### Dataset 2")
# # with gr.Row():
# # dataset2_name = gr.Textbox(value="ag_news", label="Dataset 2 Name")
# # dataset2_split = gr.Textbox(value="test", label="Dataset 2 Split")
# # dataset2_text_column = gr.Textbox(value="text", label="Text Column Name")
# # threshold = gr.Slider(
# # minimum=0.0,
# # maximum=1.0,
# # value=0.8,
# # label="Similarity Threshold"
# # )
# # compute_button = gr.Button("Compute")
# # output = gr.Markdown()
# # # Function to update the visibility of dataset2_inputs
# # def update_visibility(deduplication_type_value):
# # if deduplication_type_value == "Cross-dataset":
# # return gr.update(visible=True)
# # else:
# # return gr.update(visible=False)
# # deduplication_type.change(
# # update_visibility,
# # inputs=deduplication_type,
# # outputs=dataset2_inputs
# # )
# # compute_button.click(
# # fn=perform_deduplication,
# # inputs=[
# # deduplication_type,
# # dataset1_name,
# # dataset1_split,
# # dataset1_text_column,
# # dataset2_name,
# # dataset2_split,
# # dataset2_text_column,
# # threshold
# # ],
# # outputs=output
# # )
# # demo.launch()
|