Views
No views yet
notebook.
- Prediction Loss: 0.0463
- Prediction ROUGE-1: 0.8396
- Prediction ROUGE-2: 0.8200
- Prediction ROUGE-L: 0.8396
- Prediction BLEU: 0.4729
In general, this is a promising result, showing that the model is performing well on the task, with room for improvement on exact token matching (reflected by the BLEU score).
1from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
2import torch
3
4tokenizer = AutoTokenizer.from_pretrained("zeyadusf/text2pandas-T5")
5model = AutoModelForSeq2SeqLM.from_pretrained("zeyadusf/text2pandas-T5")
6device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
7
8def generate_pandas(question, context, model, tokenizer, max_length=512, num_beams=4, early_stopping=True):
9 """
10 Generates text based on the provided question and context using a pre-trained model and tokenizer.
11
12 Args:
13 question (str): The question part of the input.
14 context (str): The context (e.g., DataFrame description) related to the question.
15 model (torch.nn.Module): The pre-trained language model (e.g., T5).
16 tokenizer (PreTrainedTokenizer): The tokenizer corresponding to the model.
17 max_length (int): Maximum length of the generated text.
18 num_beams (int): The number of beams for beam search.
19 early_stopping (bool): Whether to stop the beam search when enough hypotheses have reached the end.
20
21 Returns:
22 str: The generated text decoded by the tokenizer.
23 """
24 # Prepare the input text by combining the question and context
25 input_text = f"<question> {question} <context> {context}"
26
27 # Tokenize the input text, convert to tensor, and truncate if needed
28 inputs = tokenizer.encode(input_text, return_tensors="pt", truncation=True, max_length=max_length)
29
30 # Move inputs and model to the appropriate device
31 inputs = inputs.to(device)
32 model = model.to(device)
33
34 # Generate predictions without calculating gradients
35 with torch.no_grad():
36 outputs = model.generate(inputs, max_length=max_length, num_beams=num_beams, early_stopping=early_stopping)
37
38 # Decode the generated tokens into text, skipping special tokens
39 predicted_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
40
41 return predicted_text
42
43# Example usage
44question = "what is the total amount of players for the rockets in 1998 only?"
45context = "df = pd.DataFrame(columns=['player', 'years_for_rockets'])"
46
47# Generate and print the predicted text
48predicted_text = generate_pandas(question, context, model, tokenizer)
49print(predicted_text)1df[df['years_for_rockets'] == '1998']['player'].count()
2