Downloads · 30 days
30
3% of all-time downloads
zeyadusf/text2pandas-T5
text2pandas-T5 is a text generation model from zeyadusf. Use it when you need the model to write or continue text. It is set up for transformers. The card lists the license as apache-2.0.
Downloads · 30 days
30
3% of all-time downloads
All-time downloads
1.1K
Public
Parameters
223M
77.6 GB on disk
Likes
0
Public
Click a slice to open those files.
.safetensors892 MB · 100%
From the Hugging Face model README
Convert Text with context about your dataframe to code Pandas by py
<!-- related links (notebook - huggingface model - huggingface data - huggingface space)--> <a href="https://www.kaggle.com/code/zeyadusf/text-2-pandas-t5"> <img src="https://img.shields.io/badge/-kaggle notebook-20bee1?style=flat&logo=kaggle&logoColor=black" alt="Kaggle" /> </a> <a href="https://www.kaggle.com/datasets/zeyadusf/convert-text-to-pandas"> <img src="https://img.shields.io/badge/-kaggle Dataset-20bee1?style=flat&logo=kaggle&logoColor=black" alt="Kaggle" /> </a> <a href="https://github.com/zeyadusf/Text2Pandas"> <img src="https://img.shields.io/badge/-Github Repo-1f2328?style=flat&logo=github&logoColor=black" alt="GitHub" /> </a> </center>I fine tuned T5, T5 is an encoder-decoder model pre-trained on a multi-task mixture of unsupervised and supervised tasks and for which each task is converted into a text-to-text format.
Using Transformers library and trained on 5 epochs and learning rate was 3e-5 and scheduler type was cosine. You can see the rest of the hyperparameters in the notebook.<br>
As for the results on test dataset:
- 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). <br>
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
import torch
tokenizer = AutoTokenizer.from_pretrained("zeyadusf/text2pandas-T5")
model = AutoModelForSeq2SeqLM.from_pretrained("zeyadusf/text2pandas-T5")
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def generate_pandas(question, context, model, tokenizer, max_length=512, num_beams=4, early_stopping=True):
"""
Generates text based on the provided question and context using a pre-trained model and tokenizer.
Args:
question (str): The question part of the input.
context (str): The context (e.g., DataFrame description) related to the question.
model (torch.nn.Module): The pre-trained language model (e.g., T5).
tokenizer (PreTrainedTokenizer): The tokenizer corresponding to the model.
max_length (int): Maximum length of the generated text.
num_beams (int): The number of beams for beam search.
early_stopping (bool): Whether to stop the beam search when enough hypotheses have reached the end.
Returns:
str: The generated text decoded by the tokenizer.
"""
# Prepare the input text by combining the question and context
input_text = f"<question> {question} <context> {context}"
# Tokenize the input text, convert to tensor, and truncate if needed
inputs = tokenizer.encode(input_text, return_tensors="pt", truncation=True, max_length=max_length)
# Move inputs and model to the appropriate device
inputs = inputs.to(device)
model = model.to(device)
# Generate predictions without calculating gradients
with torch.no_grad():
outputs = model.generate(inputs, max_length=max_length, num_beams=num_beams, early_stopping=early_stopping)
# Decode the generated tokens into text, skipping special tokens
predicted_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return predicted_text
# Example usage
question = "what is the total amount of players for the rockets in 1998 only?"
context = "df = pd.DataFrame(columns=['player', 'years_for_rockets'])"
# Generate and print the predicted text
predicted_text = generate_pandas(question, context, model, tokenizer)
print(predicted_text)
output
df[df['years_for_rockets'] == '1998']['player'].count()