File size: 1,886 Bytes
b2c5dba
e50b983
f7f2544
 
 
dd21792
dfab01c
 
 
dd21792
2cec9d1
 
 
 
dd21792
 
 
dfab01c
dd21792
 
 
dfab01c
dd21792
 
 
 
4ca5b57
 
 
 
 
 
b2c5dba
e50b983
f7f2544
b2c5dba
4ca5b57
b2c5dba
4ca5b57
f7f2544
 
 
b2c5dba
4ca5b57
 
b2c5dba
f7f2544
 
 
 
 
 
 
b2c5dba
 
f7f2544
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()