Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,24 @@
|
|
| 1 |
import torch
|
|
|
|
| 2 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 3 |
import gradio as gr
|
| 4 |
|
| 5 |
-
# β
|
|
|
|
|
|
|
|
|
|
| 6 |
model_name = "meta-llama/Llama-3.2-1B-Instruct"
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
model = AutoModelForCausalLM.from_pretrained(
|
| 9 |
model_name,
|
| 10 |
device_map="auto",
|
| 11 |
-
torch_dtype=torch.float16
|
|
|
|
| 12 |
)
|
|
|
|
| 13 |
|
| 14 |
# β
Define chatbot function
|
| 15 |
def chatbot(prompt):
|
|
@@ -17,5 +26,6 @@ def chatbot(prompt):
|
|
| 17 |
output = model.generate(**inputs, max_length=200)
|
| 18 |
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 19 |
|
| 20 |
-
# β
|
|
|
|
| 21 |
gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Llama Chatbot").launch()
|
|
|
|
| 1 |
import torch
|
| 2 |
+
import os
|
| 3 |
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 4 |
import gradio as gr
|
| 5 |
|
| 6 |
+
# β
Get Hugging Face token from environment variables
|
| 7 |
+
hf_token = os.getenv("HF_TOKEN") # Automatically retrieves the token from Hugging Face Spaces Secrets
|
| 8 |
+
|
| 9 |
+
# β
Define your model
|
| 10 |
model_name = "meta-llama/Llama-3.2-1B-Instruct"
|
| 11 |
+
|
| 12 |
+
# β
Load tokenizer & model with authentication
|
| 13 |
+
print(f"π Loading model: {model_name} ...")
|
| 14 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, token=hf_token)
|
| 15 |
model = AutoModelForCausalLM.from_pretrained(
|
| 16 |
model_name,
|
| 17 |
device_map="auto",
|
| 18 |
+
torch_dtype=torch.float16,
|
| 19 |
+
token=hf_token
|
| 20 |
)
|
| 21 |
+
print(f"β
Model '{model_name}' loaded successfully!")
|
| 22 |
|
| 23 |
# β
Define chatbot function
|
| 24 |
def chatbot(prompt):
|
|
|
|
| 26 |
output = model.generate(**inputs, max_length=200)
|
| 27 |
return tokenizer.decode(output[0], skip_special_tokens=True)
|
| 28 |
|
| 29 |
+
# β
Launch Gradio
|
| 30 |
+
print("π Launching chatbot...")
|
| 31 |
gr.Interface(fn=chatbot, inputs="text", outputs="text", title="Llama Chatbot").launch()
|