Spaces:
Build error
Build error
import os | |
import torch | |
from transformers import AutoModelForCausalLM, AutoTokenizer | |
import gradio as gr | |
# π¨ DEBUG: Print ALL environment variables again | |
print("π DEBUG: Listing all environment variables:") | |
for key, value in os.environ.items(): | |
if "HF" in key or "TOKEN" in key or "SECRET" in key: # Only show relevant secrets | |
print(f"{key} = {value[:5]}...{value[-5:]} (Masked for security)") | |
for key, value in os.environ.items(): | |
print(f"{key} = {value}") | |
# β Get the HF_TOKEN | |
HF_TOKEN = os.getenv("HF_TOKEN") | |
if not HF_TOKEN: | |
raise ValueError("β HF_TOKEN is STILL not set! Hugging Face Spaces is NOT detecting it.") | |
else: | |
print(f"β HF_TOKEN detected: {HF_TOKEN[:5]}...{HF_TOKEN[-5:]} (Masked for security)") | |
# β Get Hugging Face token from environment | |
HF_TOKEN = os.getenv("HF_TOKEN") | |
# β Ensure token is properly set | |
if not HF_TOKEN: | |
raise ValueError("β HF_TOKEN is not set! Go to Hugging Face Spaces β Settings β Secrets and add your token.") | |
# β Define model | |
model_name = "meta-llama/Llama-3.2-1B-Instruct" | |
# β Load tokenizer & model with authentication | |
print(f"π Loading model: {model_name} ...") | |
tokenizer = AutoTokenizer.from_pretrained(model_name, token=HF_TOKEN) # β Remove `use_auth_token=True` | |
model = AutoModelForCausalLM.from_pretrained( | |
model_name, | |
device_map="auto", | |
torch_dtype=torch.float16, | |
token=HF_TOKEN | |
) # β Remove `use_auth_token=True` | |
print(f"β Model '{model_name}' loaded successfully!") | |
# β Define chatbot function | |
def chatbot(prompt): | |
inputs = tokenizer(prompt, return_tensors="pt").to("cuda") | |
output = model.generate(**inputs, max_length=200) | |
return tokenizer.decode(output[0], skip_special_tokens=True) | |
# β Launch Gradio | |
print("π Launching chatbot...") | |
gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Llama Chatbot").launch() | |