Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2import torch
3model = AutoModelForSeq2SeqLM.from_pretrained("Chirayu/nl2kql")
4tokenizer = AutoTokenizer.from_pretrained("Chirayu/nl2kql")
5device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6model = model.to(device)
7
8textual_query = '''kusto: find the session ids which have duration greater than 10 and having Manoj Raheja as the owner | conferencesessions : conference, sessionid, session_title, session_type, owner, participants, URL, level, session_location, starttime, duration, time_and_duration, kusto_affinity'''
9
10def generate_query(
11 textual_query: str,
12 num_beams: int = 10,
13 max_length: int = 128,
14 repetition_penalty: int = 2.5,
15 length_penalty: int = 1,
16 early_stopping: bool = True,
17 top_p: int = 0.95,
18 top_k: int = 50,
19 num_return_sequences: int = 1,
20 ) -> str:
21 input_ids = tokenizer.encode(
22 textual_query, return_tensors="pt", add_special_tokens=True
23 )
24 device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
25 input_ids = input_ids.to(device)
26 generated_ids = model.generate(
27 input_ids=input_ids,
28 num_beams=num_beams,
29 max_length=max_length,
30 repetition_penalty=repetition_penalty,
31 length_penalty=length_penalty,
32 early_stopping=early_stopping,
33 top_p=top_p,
34 top_k=top_k,
35 num_return_sequences=num_return_sequences,
36 )
37 query = [
38 tokenizer.decode(
39 generated_id,
40 skip_special_tokens=True,
41 clean_up_tokenization_spaces=True,
42 )
43 for generated_id in generated_ids
44 ][0]
45 return query