Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,22 +17,21 @@ if not HUGGINGFACE_API_TOKEN:
|
|
| 17 |
# Title of the app
|
| 18 |
st.title("Optimized LLaMA 2 Chatbot")
|
| 19 |
|
| 20 |
-
# Load the
|
| 21 |
@st.cache_resource
|
| 22 |
def load_model_and_tokenizer():
|
| 23 |
-
"""Load the tokenizer and model
|
| 24 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 25 |
-
"meta-llama/Llama-2-
|
| 26 |
use_auth_token=HUGGINGFACE_API_TOKEN
|
| 27 |
)
|
| 28 |
|
| 29 |
-
# Load the model with quantization and device_map auto
|
| 30 |
model = LlamaForCausalLM.from_pretrained(
|
| 31 |
-
"meta-llama/Llama-2-
|
| 32 |
use_auth_token=HUGGINGFACE_API_TOKEN,
|
| 33 |
-
torch_dtype=torch.float16,
|
| 34 |
-
device_map="auto",
|
| 35 |
-
low_cpu_mem_usage=True
|
| 36 |
)
|
| 37 |
|
| 38 |
return tokenizer, model
|
|
@@ -46,12 +45,12 @@ def generate_text(prompt, tokenizer, model, max_length=256, temperature=0.6):
|
|
| 46 |
with torch.no_grad():
|
| 47 |
generate_ids = model.generate(
|
| 48 |
inputs.input_ids,
|
| 49 |
-
max_length=max_length,
|
| 50 |
-
temperature=temperature,
|
| 51 |
do_sample=True,
|
| 52 |
-
top_k=30,
|
| 53 |
-
top_p=0.85,
|
| 54 |
-
repetition_penalty=1.1,
|
| 55 |
)
|
| 56 |
|
| 57 |
return tokenizer.decode(generate_ids[0], skip_special_tokens=True)
|
|
@@ -70,8 +69,8 @@ if st.button("Generate Response"):
|
|
| 70 |
|
| 71 |
# Optional settings in sidebar
|
| 72 |
st.sidebar.header("Settings")
|
| 73 |
-
max_length = st.sidebar.slider("Max Length", min_value=50, max_value=512, value=256)
|
| 74 |
-
temperature = st.sidebar.slider("Temperature", min_value=0.1, max_value=1.0, value=0.6)
|
| 75 |
|
| 76 |
# Regenerate response with updated settings
|
| 77 |
if st.sidebar.button("Regenerate"):
|
|
|
|
| 17 |
# Title of the app
|
| 18 |
st.title("Optimized LLaMA 2 Chatbot")
|
| 19 |
|
| 20 |
+
# Load the LLaMA model and tokenizer from Hugging Face
|
| 21 |
@st.cache_resource
|
| 22 |
def load_model_and_tokenizer():
|
| 23 |
+
"""Load the tokenizer and model."""
|
| 24 |
tokenizer = AutoTokenizer.from_pretrained(
|
| 25 |
+
"meta-llama/Llama-2-7b-hf", # Correct model identifier
|
| 26 |
use_auth_token=HUGGINGFACE_API_TOKEN
|
| 27 |
)
|
| 28 |
|
|
|
|
| 29 |
model = LlamaForCausalLM.from_pretrained(
|
| 30 |
+
"meta-llama/Llama-2-7b-hf", # Correct model identifier
|
| 31 |
use_auth_token=HUGGINGFACE_API_TOKEN,
|
| 32 |
+
torch_dtype=torch.float16,
|
| 33 |
+
device_map="auto",
|
| 34 |
+
low_cpu_mem_usage=True
|
| 35 |
)
|
| 36 |
|
| 37 |
return tokenizer, model
|
|
|
|
| 45 |
with torch.no_grad():
|
| 46 |
generate_ids = model.generate(
|
| 47 |
inputs.input_ids,
|
| 48 |
+
max_length=max_length,
|
| 49 |
+
temperature=temperature,
|
| 50 |
do_sample=True,
|
| 51 |
+
top_k=30,
|
| 52 |
+
top_p=0.85,
|
| 53 |
+
repetition_penalty=1.1,
|
| 54 |
)
|
| 55 |
|
| 56 |
return tokenizer.decode(generate_ids[0], skip_special_tokens=True)
|
|
|
|
| 69 |
|
| 70 |
# Optional settings in sidebar
|
| 71 |
st.sidebar.header("Settings")
|
| 72 |
+
max_length = st.sidebar.slider("Max Length", min_value=50, max_value=512, value=256)
|
| 73 |
+
temperature = st.sidebar.slider("Temperature", min_value=0.1, max_value=1.0, value=0.6)
|
| 74 |
|
| 75 |
# Regenerate response with updated settings
|
| 76 |
if st.sidebar.button("Regenerate"):
|