Update README.md
Browse files
README.md
CHANGED
@@ -21,4 +21,51 @@ A user is asking an ambiguous question (where ambiguous question is a question t
|
|
21 |
- are you looking for a suitable ldl to use as a server or a client (Score: 0.3182) <br />
|
22 |
- how would you like to consume the nlp model (Score: 0.2842) <br />
|
23 |
|
24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
- are you looking for a suitable ldl to use as a server or a client (Score: 0.3182) <br />
|
22 |
- how would you like to consume the nlp model (Score: 0.2842) <br />
|
23 |
|
24 |
+
|
25 |
+
```
|
26 |
+
from transformers import AutoTokenizer, AutoModelWithLMHead
|
27 |
+
tokenizer = AutoTokenizer.from_pretrained("salesken/clariq_gpt2")
|
28 |
+
model = AutoModelWithLMHead.from_pretrained("salesken/clariq_gpt2")
|
29 |
+
|
30 |
+
|
31 |
+
|
32 |
+
input_query="Serve your models directly from Hugging Face infrastructure and run large scale NLP models in milliseconds with just a few lines of code"
|
33 |
+
|
34 |
+
query= input_query + " ~~ "
|
35 |
+
|
36 |
+
|
37 |
+
input_ids = tokenizer.encode(query.lower(), return_tensors='pt')
|
38 |
+
sample_outputs = model.generate(input_ids,
|
39 |
+
do_sample=True,
|
40 |
+
num_beams=1,
|
41 |
+
max_length=128,
|
42 |
+
temperature=0.9,
|
43 |
+
top_k = 40,
|
44 |
+
num_return_sequences=10)
|
45 |
+
clarifications_gen = []
|
46 |
+
for i in range(len(sample_outputs)):
|
47 |
+
r = tokenizer.decode(sample_outputs[i], skip_special_tokens=True).split('||')[0]
|
48 |
+
r = r.split(' ~~ ~~')[1]
|
49 |
+
if r not in clarifications_gen:
|
50 |
+
clarifications_gen.append(r)
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
# to select the top n results:
|
55 |
+
|
56 |
+
from sentence_transformers import SentenceTransformer, util
|
57 |
+
import torch
|
58 |
+
embedder = SentenceTransformer('paraphrase-distilroberta-base-v1')
|
59 |
+
|
60 |
+
# Corpus with example sentences
|
61 |
+
corpus = clarifications_gen
|
62 |
+
corpus_embeddings = embedder.encode(corpus, convert_to_tensor=True)
|
63 |
+
# Query sentences:
|
64 |
+
query = input_query.lower()
|
65 |
+
query_embedding = embedder.encode(query, convert_to_tensor=True)
|
66 |
+
cos_scores = util.pytorch_cos_sim(query_embedding, corpus_embeddings)[0]
|
67 |
+
top_results = torch.topk(cos_scores, k=1)
|
68 |
+
for score, idx in zip(top_results[0], top_results[3]):
|
69 |
+
print(corpus[idx], "(Score: {:.4f})".format(score))
|
70 |
+
|
71 |
+
```
|