jordimas commited on
Commit
f7a7751
·
1 Parent(s): ae5f0e5

Readme and others

Browse files
Files changed (2) hide show
  1. README.md +19 -0
  2. inference.py +111 -0
README.md ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+
4
+
5
+
6
+ ## Model description
7
+
8
+ This is a model based on Google's MT5 finetuned for paraphrasing in Catalan language
9
+
10
+ Sample:
11
+
12
+ ```
13
+ original: Aquesta és una associació sense ànim de lucre amb la missió de fomentar la presència i l'ús del català.
14
+ Aquesta és una organització sense ànim de lucre amb la finalitat de promoure la presència i l'ús del català.
15
+ Aquesta és una organització sense ànim de lucre que té com a objectiu promoure la presència i l'ús del català.
16
+
17
+ ```
18
+
19
+ To run inference check the inference.py file in the repository.
inference.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import T5ForConditionalGeneration, T5Tokenizer
2
+ from transformers import AutoTokenizer, MT5Model
3
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
4
+ from transformers import MT5ForConditionalGeneration, AutoTokenizer
5
+ from simpletransformers.t5 import T5Model
6
+ import datetime
7
+ import logging
8
+ import os
9
+ import sys
10
+
11
+
12
+ class Inference:
13
+ def _discard_recommendations(self, original, proposal):
14
+ proposal = proposal.lower()
15
+ original = original.lower()
16
+ if proposal == original:
17
+ return True
18
+
19
+ chars = [".", "!", " ", "?", ","]
20
+ _proposal = proposal
21
+ _original = original
22
+ for char in chars:
23
+ proposal = proposal.replace(char, "")
24
+ original = original.replace(char, "")
25
+
26
+ if proposal == original:
27
+ return True
28
+
29
+ return False
30
+
31
+ # https://github.com/Vamsi995/Paraphrase-Generator/blob/master/evaluate.py
32
+ def get_paraphrases(
33
+ self,
34
+ model_name,
35
+ sentence,
36
+ temperature,
37
+ prefix="paraphrase: ",
38
+ n_predictions=2,
39
+ top_k=120,
40
+ max_length=256,
41
+ device="cpu",
42
+ ):
43
+
44
+ model = MT5ForConditionalGeneration.from_pretrained(model_name)
45
+ tokenizer = T5Tokenizer.from_pretrained(model_name)
46
+
47
+ discaded = 0
48
+ text = prefix + sentence + " </s>"
49
+ encoding = tokenizer.encode_plus(
50
+ text, pad_to_max_length=True, return_tensors="pt"
51
+ )
52
+ input_ids, attention_masks = encoding["input_ids"].to(device), encoding[
53
+ "attention_mask"
54
+ ].to(device)
55
+
56
+ do_sample = True if temperature > 0 else False
57
+ print(f"do_sample: {do_sample}")
58
+ print(f"temperature: {temperature}")
59
+ # https://huggingface.co/blog/how-to-generate
60
+ # https://huggingface.co/transformers/v3.2.0/_modules/transformers/generation_utils.html
61
+ model_output = model.generate(
62
+ input_ids=input_ids,
63
+ attention_mask=attention_masks,
64
+ do_sample=do_sample,
65
+ max_length=max_length,
66
+ top_k=top_k,
67
+ num_beams=n_predictions * 2, ## ask for twice since some will be discarted
68
+ top_p=0.98,
69
+ temperature=temperature,
70
+ early_stopping=True,
71
+ num_return_sequences=n_predictions * 2,
72
+ )
73
+ logging.debug(f"{len(model_output)} predictions for {sentence}")
74
+ outputs = []
75
+ for output in model_output:
76
+ generated_sent = tokenizer.decode(
77
+ output, skip_special_tokens=True, clean_up_tokenization_spaces=True
78
+ )
79
+ if (
80
+ self._discard_recommendations(sentence, generated_sent) is False
81
+ and generated_sent not in outputs
82
+ ):
83
+ generated_sent = generated_sent.replace("’", "'")
84
+ outputs.append(generated_sent)
85
+ else:
86
+ logging.debug(f"Discarded: {generated_sent} - source:{sentence}")
87
+ discaded = +1
88
+
89
+ if len(outputs) == n_predictions:
90
+ break
91
+
92
+ return outputs
93
+
94
+
95
+ def main():
96
+ i = Inference()
97
+ sentence = "Aquesta és una associació sense ànim de lucre amb la missió de fomentar la presència i l'ús del català."
98
+ #sentence = "Softcatalà és una associació sense ànim de lucre amb la missió de fomentar la presència i l'ús del català en tots els àmbits de les noves tecnologies."
99
+ model = os.getcwd()
100
+ options = i.get_paraphrases(
101
+ model,
102
+ sentence,
103
+ 1.0
104
+ )
105
+ print(f"original: {sentence}")
106
+ for option in options:
107
+ print(f" {option}")
108
+
109
+
110
+ if __name__ == "__main__":
111
+ main()