[Python] Getting StopIteration error in HuggingFace

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    Please help me to resolve the issue. I am using Colab and HuggingFace Token is added in Colab secrets.

    from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint
    from dotenv import load_dotenv
    from google.colab import userdata
    import os

    print("Before try block:", 'hf_token' in locals())
    # Initialize hf_token with a default value (None)
    hf_token = None
    try:
    hf_token = userdata.get('HF_TOKEN')
    print("Inside try block, after userdata.get:", 'hf_token' in locals())
    if hf_token:
    print("Hugging Face token successfully retrieved from Colab Secrets.")
    else:
    print("Warning: Hugging Face token not found in Colab Secrets. Ensure you've added it.")
    except Exception as e:
    print(f"An error occurred while accessing Colab Secrets: {e}")

    print("After try block:", 'hf_token' in locals())


    Here, output is:

    Before try block: False

    Inside try block, after userdata.get: True

    Hugging Face token successfully retrieved from Colab Secrets.

    After try block: True


    Now,

    if hf_token:
    print("Inside the 'if hf_token' block, hf_token:", hf_token)
    llm = HuggingFaceEndpoint(
    repo_id="TinyLlama/TinyLlama-1.1B-Chat-v1.0",
    task="text-generation",
    huggingfacehub_api_token=hf_token,
    max_new_tokens=150 # Increased max_new_tokens
    )
    # Initialize ChatHuggingFace directly with the HuggingFaceEndpoint instance
    model = ChatHuggingFace(llm=llm)
    try:
    result = model.invoke('Who is Lincoln')
    print(result)
    except Exception as e:
    print(f"Error during model invocation: Type: {type(e)}, Message: {e}")
    else:
    print("Hugging Face model could not be initialized due to missing token.")


    Now, getting the output:

    Hugging Face token found.

    Error with HuggingFaceHub: Type: <class 'StopIteration'>, Message:


    Tried different prompts also but no luck. Any help is appreciated.

    Continue reading...

Compartilhe esta Página