matth-meyer commited on
Commit
b7b7354
1 Parent(s): a54d7b0

Add description and how to get started

Browse files
Files changed (1) hide show
  1. README.md +74 -4
README.md CHANGED
@@ -6,7 +6,7 @@ tags: []
6
  # Model Card for Model ID
7
 
8
  <!-- Provide a quick summary of what the model is/does. -->
9
-
10
 
11
 
12
  ## Model Details
@@ -23,14 +23,14 @@ This is the model card of a 🤗 transformers model that has been pushed on the
23
  - **Model type:** [More Information Needed]
24
  - **Language(s) (NLP):** [More Information Needed]
25
  - **License:** [More Information Needed]
26
- - **Finetuned from model [optional]:** [More Information Needed]
27
 
28
  ### Model Sources [optional]
29
 
30
  <!-- Provide the basic links for the model. -->
31
 
32
  - **Repository:** [More Information Needed]
33
- - **Paper [optional]:** [More Information Needed]
34
  - **Demo [optional]:** [More Information Needed]
35
 
36
  ## Uses
@@ -71,7 +71,77 @@ Users (both direct and downstream) should be made aware of the risks, biases and
71
 
72
  Use the code below to get started with the model.
73
 
74
- [More Information Needed]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
  ## Training Details
77
 
 
6
  # Model Card for Model ID
7
 
8
  <!-- Provide a quick summary of what the model is/does. -->
9
+ Fine tuned REBEL model on 1k GPT labeled documents to perform Named Entity Extraction and Relation Extraction
10
 
11
 
12
  ## Model Details
 
23
  - **Model type:** [More Information Needed]
24
  - **Language(s) (NLP):** [More Information Needed]
25
  - **License:** [More Information Needed]
26
+ - **Finetuned from model [optional]:** https://huggingface.co/Babelscape/rebel-large
27
 
28
  ### Model Sources [optional]
29
 
30
  <!-- Provide the basic links for the model. -->
31
 
32
  - **Repository:** [More Information Needed]
33
+ - **Paper [optional]:** https://aclanthology.org/2021.findings-emnlp.204/
34
  - **Demo [optional]:** [More Information Needed]
35
 
36
  ## Uses
 
71
 
72
  Use the code below to get started with the model.
73
 
74
+ ```python
75
+ from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
76
+ import torch
77
+
78
+ gen_kwargs = {
79
+ "max_length": 128,
80
+ "length_penalty": 0,
81
+ "num_beams": 3,
82
+ "no_repeat_ngram_size": 0,
83
+ "early_stopping": False
84
+ }
85
+ MODEL = "text2tech/REBEL_NER_RE_001_1000docs"
86
+
87
+ tokenizer = AutoTokenizer.from_pretrained(MODEL)
88
+ model = AutoModelForSeq2SeqLM.from_pretrained(MODEL)
89
+ if torch.cuda.is_available():
90
+ _ = model.to("cuda:3") # comment if no GPU available
91
+ _ = model.eval()
92
+
93
+ def extract_triplets(text, mapping_types={"<mat>": "material", "<meth>": "method", "<tsys>": "technological_system", "<org>": "organization", "<tfield>": "technical_field"}):
94
+ triplets = []
95
+ relation = ''
96
+ text = text.strip()
97
+ current = 'x'
98
+ subject, relation, object_, object_type, subject_type = '','','','',''
99
+
100
+ for token in text.replace("<s>", "").replace("<pad>", "").replace("</s>", "").split():
101
+ if token == "<triplet>":
102
+ current = 't'
103
+ if relation != '':
104
+ triplets.append({'head': subject.strip(), 'head_type': subject_type, 'type': relation.strip(),'tail': object_.strip(), 'tail_type': object_type})
105
+ relation = ''
106
+ subject = ''
107
+ elif token in mapping_types:
108
+ if current == 't' or current == 'o':
109
+ current = 's'
110
+ if relation != '':
111
+ triplets.append({'head': subject.strip(), 'head_type': subject_type, 'type': relation.strip(),'tail': object_.strip(), 'tail_type': object_type})
112
+ object_ = ''
113
+ subject_type = mapping_types[token]
114
+ else:
115
+ current = 'o'
116
+ object_type = mapping_types[token]
117
+ relation = ''
118
+ else:
119
+ if current == 't':
120
+ subject += ' ' + token
121
+ elif current == 's':
122
+ object_ += ' ' + token
123
+ elif current == 'o':
124
+ relation += ' ' + token
125
+ if subject != '' and relation != '' and object_ != '' and object_type != '' and subject_type != '':
126
+ triplets.append({'head': subject.strip(), 'head_type': subject_type, 'type': relation.strip(),'tail': object_.strip(), 'tail_type': object_type})
127
+ return triplets
128
+
129
+ def generate(text):
130
+ model_inputs = tokenizer(text, max_length=1024, padding=True, truncation=True, return_tensors = 'pt')
131
+ generated_tokens = model.generate(
132
+ model_inputs["input_ids"].to(model.device),
133
+ attention_mask=model_inputs["attention_mask"].to(model.device),
134
+ **gen_kwargs,
135
+ )
136
+ decoded_preds = tokenizer.batch_decode(generated_tokens, skip_special_tokens=False)
137
+ return decoded_preds[0]
138
+
139
+ text = """As much mystique as they've been given by companies with vested interest, diamonds are little more than lumps of carbon. In science applications, diamond is useful as a tough protective coating and for optical devices, but its relative rarity on Earth makes it difficult to get. Now, researchers at North Carolina State University have demonstrated a new way to convert carbon nanofibers and nanotubes into diamond fibers that can be performed in a lab more easily than existing techniques. In nature, diamonds are forged deep in the Earth, where carbon is subjected to high pressure and temperatures, so it makes sense that artificial methods of manufacturing them requires similar conditions. And the equipment involved in that process can be quite cumbersome and energy - intensive. By contrast, the new technique developed by the NCSU team can apparently be done at room temperature and normal pressure levels. First, carbon nanofibers are hit by a laser pulse lasting just 100 nanoseconds, which instantly heats the carbon to about 3,727°C (6,740°F) and melts it. Normally that heat would be enough to vaporize the carbon, which obviously is n't the desired outcome. To stop that, the team uses a substrate of sapphire, glass or plastic polymer, which restricts the heat flow enough to prevent the phase change. Then the material is quickly cooled, causing it to crystallize into diamond. This process can create diamond nanofibers for use in electronics and even quantum computers, or to seed carbon nanofibers with tiny diamonds. In the case of the latter, larger diamond structures can then be made using more traditional techniques like chemical vapor deposition. The structures created this way could end up as coatings to toughen up tools or for jewelry. The research was published in the journal Nanoscale. Source : North Carolina State UniversityView gallery - 2 images"""
140
+
141
+ output = generate(text)
142
+ encoded = extract_triplets(output)
143
+ print(encoded)
144
+ ```
145
 
146
  ## Training Details
147