code?

#3
by rakmik - opened

Is it possible to have a complete code to reason with the model or ask the model a simple question like who is Napoleon Bonaparte?

from transformers import T5ForConditionalGeneration, AutoTokenizer

model = T5ForConditionalGeneration.from_pretrained('google/byt5-large')
tokenizer = AutoTokenizer.from_pretrained('google/byt5-large')

Prepare your input texts

input_texts = ["Who is Napoleon Bonaparte?"]
model_inputs = tokenizer(input_texts, padding="longest", return_tensors="pt")

Generate output for each input

generated_ids = model.generate(**model_inputs, max_new_tokens=512) # Added max_new_tokens

Decode the generated outputs

generated_texts = [tokenizer.decode(ids, skip_special_tokens=True) for ids in generated_ids]

Print the generated texts

for original_text, generated_text in zip(input_texts, generated_texts):
print(f"Input: {original_text}")
print(f"Generated: {generated_text}\n")

/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning:
The secret HF_TOKEN does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional to access public models or datasets.
warnings.warn(
Input: Who is Napoleon Bonaparte?
Generated: s a memory? - Napoleon Bonaparte
Who is Napoleon Bonaparte?
Napoleon Bonaparte is a memory of Napoleon Bonaparte.
Napoleon Bonaparte is a memory
Napoleon Bonaparte (?)
() () () () (*) ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((

from transformers import pipeline
import torch

pipe = pipeline("text-generation", model="google/byt5-large", device_map="auto")

Change input format from chat-based (list of dicts) to simple text (list of strings)

messages = ["Who are you?"]

Generate output with max_new_tokens set to 50 (you can change 50 to your desired value)

pipe(messages, max_new_tokens=128)

/usr/local/lib/python3.11/dist-packages/huggingface_hub/utils/_auth.py:94: UserWarning:
The secret HF_TOKEN does not exist in your Colab secrets.
To authenticate with the Hugging Face Hub, create a token in your settings tab (https://huggingface.co/settings/tokens), set it as secret in your Google Colab and restart your session.
You will be able to reuse this secret in all of your notebooks.
Please note that authentication is recommended but still optional to access public models or datasets.
warnings.warn(
Device set to use cpu
The model 'T5ForConditionalGeneration' is not supported for text-generation. Supported models are ['AriaTextForCausalLM', 'BambaForCausalLM', 'BartForCausalLM', 'BertLMHeadModel', 'BertGenerationDecoder', 'BigBirdForCausalLM', 'BigBirdPegasusForCausalLM', 'BioGptForCausalLM', 'BlenderbotForCausalLM', 'BlenderbotSmallForCausalLM', 'BloomForCausalLM', 'CamembertForCausalLM', 'LlamaForCausalLM', 'CodeGenForCausalLM', 'CohereForCausalLM', 'Cohere2ForCausalLM', 'CpmAntForCausalLM', 'CTRLLMHeadModel', 'Data2VecTextForCausalLM', 'DbrxForCausalLM', 'DeepseekV3ForCausalLM', 'DiffLlamaForCausalLM', 'ElectraForCausalLM', 'Emu3ForCausalLM', 'ErnieForCausalLM', 'FalconForCausalLM', 'FalconMambaForCausalLM', 'FuyuForCausalLM', 'GemmaForCausalLM', 'Gemma2ForCausalLM', 'Gemma3ForConditionalGeneration', 'Gemma3ForCausalLM', 'GitForCausalLM', 'GlmForCausalLM', 'Glm4ForCausalLM', 'GotOcr2ForConditionalGeneration', 'GPT2LMHeadModel', 'GPT2LMHeadModel', 'GPTBigCodeForCausalLM', 'GPTNeoForCausalLM', 'GPTNeoXForCausalLM', 'GPTNeoXJapaneseForCausalLM', 'GPTJForCausalLM', 'GraniteForCausalLM', 'GraniteMoeForCausalLM', 'GraniteMoeSharedForCausalLM', 'HeliumForCausalLM', 'JambaForCausalLM', 'JetMoeForCausalLM', 'LlamaForCausalLM', 'Llama4ForCausalLM', 'Llama4ForCausalLM', 'MambaForCausalLM', 'Mamba2ForCausalLM', 'MarianForCausalLM', 'MBartForCausalLM', 'MegaForCausalLM', 'MegatronBertForCausalLM', 'MistralForCausalLM', 'MixtralForCausalLM', 'MllamaForCausalLM', 'MoshiForCausalLM', 'MptForCausalLM', 'MusicgenForCausalLM', 'MusicgenMelodyForCausalLM', 'MvpForCausalLM', 'NemotronForCausalLM', 'OlmoForCausalLM', 'Olmo2ForCausalLM', 'OlmoeForCausalLM', 'OpenLlamaForCausalLM', 'OpenAIGPTLMHeadModel', 'OPTForCausalLM', 'PegasusForCausalLM', 'PersimmonForCausalLM', 'PhiForCausalLM', 'Phi3ForCausalLM', 'Phi4MultimodalForCausalLM', 'PhimoeForCausalLM', 'PLBartForCausalLM', 'ProphetNetForCausalLM', 'QDQBertLMHeadModel', 'Qwen2ForCausalLM', 'Qwen2MoeForCausalLM', 'Qwen3ForCausalLM', 'Qwen3MoeForCausalLM', 'RecurrentGemmaForCausalLM', 'ReformerModelWithLMHead', 'RemBertForCausalLM', 'RobertaForCausalLM', 'RobertaPreLayerNormForCausalLM', 'RoCBertForCausalLM', 'RoFormerForCausalLM', 'RwkvForCausalLM', 'Speech2Text2ForCausalLM', 'StableLmForCausalLM', 'Starcoder2ForCausalLM', 'TransfoXLLMHeadModel', 'TrOCRForCausalLM', 'WhisperForCausalLM', 'XGLMForCausalLM', 'XLMWithLMHeadModel', 'XLMProphetNetForCausalLM', 'XLMRobertaForCausalLM', 'XLMRobertaXLForCausalLM', 'XLNetLMHeadModel', 'XmodForCausalLM', 'ZambaForCausalLM', 'Zamba2ForCausalLM'].
[[{'generated_text': 'Who are you?ingzooping_ca_zooping$theBest_reviewd_singer article_selected^.^*]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]))'}]]

import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM # Changed AutoModelForCausalLM to AutoModelForSeq2SeqLM

def create_huggingface_chat():
print("Loading language model from Hugging Face...")

# Replace with the model you want to use
model_name = "google/byt5-small"

# Load tokenizer and model
tokenizer = AutoTokenizer.from_pretrained(model_name)
# Load the model using AutoModelForSeq2SeqLM for sequence-to-sequence tasks
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)

print(f"Model {model_name} loaded successfully!")
print("Type 'quit' to exit the chat")

# Chat loop
while True:
    # Get user input
    user_input = input("You: ")

    # Check if user wants to quit
    if user_input.lower() == 'quit':
        print("Ending chat session. Goodbye!")
        break

    # Encode the input and generate a response
    # ByT5 uses byte-level encoding, so no special tokens are needed for the input
    inputs = tokenizer(user_input, return_tensors="pt")
    with torch.no_grad():
        # Generation for Seq2Seq models is slightly different
        outputs = model.generate(
            inputs["input_ids"],
            max_length=100,
            # For ByT5, the end of sequence token is </s> which is ID 1
            eos_token_id=tokenizer.eos_token_id, # Using the tokenizer's eos_token_id
            temperature=0.7,
            do_sample=True
        )

    # Decode and print the response
    # Decode the generated tokens back into text
    response = tokenizer.decode(outputs[0], skip_special_tokens=True)
    print(f"AI: {response}")

if name == "main":
create_huggingface_chat()

Loading language model from Hugging Face...
Model google/byt5-small loaded successfully!
Type 'quit' to exit the chat
You: hi
AI: ifi.hi oconitihohinatiutiehiositoedion
iphi____oci_o___________________________________shi________
You: quit
Ending chat session. Goodbye!

very baaaaaaaaaaaaaaaaaaaaaaaaad

AI: ifi.hi oconitihohinatiutiehiositoedion

from transformers import T5ForConditionalGeneration, AutoTokenizer
import torch

model = T5ForConditionalGeneration.from_pretrained('google/byt5-large')
tokenizer = AutoTokenizer.from_pretrained('google/byt5-large')

text input

text = 'Who is Python?'
inputs = tokenizer(text, padding='longest', return_tensors='pt')

Generate with a maximum length

max_tokens_to_generate = 50 # You can adjust this value
outputs = model.generate(**inputs, max_length=max_tokens_to_generate)
output = tokenizer.decode(outputs[0], skip_special_tokens=True)

print(output)

s the name of the game? - Python - Python - Pyth

Sign up or log in to comment