Add code demo
Browse files
README.md
CHANGED
@@ -42,6 +42,62 @@ To download our Excel data template, please click [here](https://huggingface.co/
|
|
42 |
|
43 |
---
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
### <font color="IndianRed">Authors </font>
|
46 |
Queenie Luo (queenieluo[at]g.harvard.edu)
|
47 |
<br>
|
|
|
42 |
|
43 |
---
|
44 |
|
45 |
+
### <font color="IndianRed">Code Demonstration: Loading and Using MetaDis Model </font>
|
46 |
+
|
47 |
+
The following section demonstrates how to directly load the MetaDis model and use it for predicting whether two sets of biographical information refer to the same person or not.
|
48 |
+
|
49 |
+
Please ensure that you have the `transformers` library installed in your Python environment. If not, you can install it using pip:
|
50 |
+
|
51 |
+
```python
|
52 |
+
pip install transformers
|
53 |
+
```
|
54 |
+
|
55 |
+
Now, let's load our model and make some predictions:
|
56 |
+
|
57 |
+
```python
|
58 |
+
# Import necessary libraries from HuggingFace Transformers
|
59 |
+
from transformers import AutoTokenizer, AutoModelForNextSentencePrediction
|
60 |
+
import torch
|
61 |
+
|
62 |
+
# Load our tokenizer and model
|
63 |
+
tokenizer = AutoTokenizer.from_pretrained("cbdb/MetaDis")
|
64 |
+
model = AutoModelForNextSentencePrediction.from_pretrained("cbdb/MetaDis")
|
65 |
+
|
66 |
+
# Define our sentences to compare
|
67 |
+
sentence1 = ['first biographical information of person name A', 'first biographical information of person name B']
|
68 |
+
sentence2 = ['second biographical information of person name A', 'first biographical information of person name B']
|
69 |
+
|
70 |
+
# Loop through each sentence pair
|
71 |
+
for s1, s2 in zip(sentence1, sentence2):
|
72 |
+
# Prepare the inputs
|
73 |
+
encoding = tokenizer(s1, s2, truncation=True, padding=True, return_tensors='pt')
|
74 |
+
|
75 |
+
# Move the inputs to the device where the model is
|
76 |
+
for key in encoding:
|
77 |
+
encoding[key] = encoding[key].to(model.device)
|
78 |
+
|
79 |
+
# Make the prediction
|
80 |
+
outputs = model(**encoding)
|
81 |
+
|
82 |
+
# Extract the prediction
|
83 |
+
logits = outputs.logits
|
84 |
+
preds = torch.argmax(logits, dim=-1)
|
85 |
+
|
86 |
+
# Display the results
|
87 |
+
if preds.item() == 1:
|
88 |
+
print('Same person')
|
89 |
+
print(s1, s2)
|
90 |
+
else:
|
91 |
+
print('Different person')
|
92 |
+
print(s1, s2)
|
93 |
+
```
|
94 |
+
|
95 |
+
This code demonstration shows how you can load our MetaDis model, prepare inputs in the necessary format, and extract predictions to determine if the biographical details refer to the same person or different individuals. Remember to replace the example sentences with your own data.
|
96 |
+
|
97 |
+
Remember to include a link or instructions on how users can install the `transformers` library if they don't already have it installed.
|
98 |
+
|
99 |
+
---
|
100 |
+
|
101 |
### <font color="IndianRed">Authors </font>
|
102 |
Queenie Luo (queenieluo[at]g.harvard.edu)
|
103 |
<br>
|