Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import torch
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import os
|
| 6 |
|
|
|
|
| 7 |
def setup_llama3_auth():
|
| 8 |
"""Configurar autenticaci贸n para Llama 3"""
|
| 9 |
if 'HUGGING_FACE_TOKEN_3' in st.secrets:
|
|
@@ -17,14 +18,17 @@ def setup_llama3_auth():
|
|
| 17 |
|
| 18 |
class Llama3Demo:
|
| 19 |
def __init__(self):
|
| 20 |
-
# Verificar autenticaci贸n antes de cargar el modelo
|
| 21 |
setup_llama3_auth()
|
| 22 |
-
|
| 23 |
-
# Usando el modelo de 3B con instrucciones
|
| 24 |
self.model_name = "meta-llama/Llama-3.2-3B-Instruct"
|
| 25 |
self._model = None
|
| 26 |
self._tokenizer = None
|
| 27 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
@property
|
| 29 |
def model(self):
|
| 30 |
if self._model is None:
|
|
@@ -33,12 +37,11 @@ class Llama3Demo:
|
|
| 33 |
self.model_name,
|
| 34 |
torch_dtype=torch.float16,
|
| 35 |
device_map="auto",
|
| 36 |
-
|
| 37 |
-
|
| 38 |
)
|
| 39 |
except Exception as e:
|
| 40 |
st.error(f"Error cargando el modelo: {str(e)}")
|
| 41 |
-
st.error("Verifica tu acceso a Llama 3.2 en https://huggingface.co/meta-llama")
|
| 42 |
raise e
|
| 43 |
return self._model
|
| 44 |
|
|
@@ -48,21 +51,26 @@ class Llama3Demo:
|
|
| 48 |
try:
|
| 49 |
self._tokenizer = AutoTokenizer.from_pretrained(
|
| 50 |
self.model_name,
|
| 51 |
-
|
| 52 |
)
|
| 53 |
except Exception as e:
|
| 54 |
st.error(f"Error cargando el tokenizer: {str(e)}")
|
| 55 |
raise e
|
| 56 |
return self._tokenizer
|
| 57 |
|
|
|
|
|
|
|
| 58 |
def generate_response(self, prompt: str, max_new_tokens: int = 512) -> str:
|
| 59 |
-
# Formato espec铆fico para Llama 3.2
|
| 60 |
formatted_prompt = f"""<|system|>You are a helpful AI assistant.</s>
|
| 61 |
<|user|>{prompt}</s>
|
| 62 |
<|assistant|>"""
|
| 63 |
|
| 64 |
inputs = self.tokenizer(formatted_prompt, return_tensors="pt").to(self.model.device)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
with torch.no_grad():
|
| 67 |
outputs = self.model.generate(
|
| 68 |
**inputs,
|
|
@@ -70,16 +78,16 @@ class Llama3Demo:
|
|
| 70 |
num_return_sequences=1,
|
| 71 |
temperature=0.7,
|
| 72 |
do_sample=True,
|
| 73 |
-
top_p=0.9
|
|
|
|
| 74 |
)
|
| 75 |
|
| 76 |
-
# Limpiar memoria GPU
|
| 77 |
torch.cuda.empty_cache()
|
| 78 |
|
| 79 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 80 |
-
# Extraer solo la respuesta del asistente
|
| 81 |
return response.split("<|assistant|>")[-1].strip()
|
| 82 |
|
|
|
|
| 83 |
def main():
|
| 84 |
st.set_page_config(page_title="Llama 3.2 Chat", page_icon="馃")
|
| 85 |
|
|
|
|
| 4 |
from huggingface_hub import login
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
##################################################################
|
| 8 |
def setup_llama3_auth():
|
| 9 |
"""Configurar autenticaci贸n para Llama 3"""
|
| 10 |
if 'HUGGING_FACE_TOKEN_3' in st.secrets:
|
|
|
|
| 18 |
|
| 19 |
class Llama3Demo:
|
| 20 |
def __init__(self):
|
|
|
|
| 21 |
setup_llama3_auth()
|
|
|
|
|
|
|
| 22 |
self.model_name = "meta-llama/Llama-3.2-3B-Instruct"
|
| 23 |
self._model = None
|
| 24 |
self._tokenizer = None
|
| 25 |
|
| 26 |
+
# Configuraci贸n de cuantizaci贸n
|
| 27 |
+
self.quantization_config = BitsAndBytesConfig(
|
| 28 |
+
load_in_8bit=True,
|
| 29 |
+
bnb_4bit_compute_dtype=torch.float16
|
| 30 |
+
)
|
| 31 |
+
|
| 32 |
@property
|
| 33 |
def model(self):
|
| 34 |
if self._model is None:
|
|
|
|
| 37 |
self.model_name,
|
| 38 |
torch_dtype=torch.float16,
|
| 39 |
device_map="auto",
|
| 40 |
+
quantization_config=self.quantization_config, # Nueva forma de configurar cuantizaci贸n
|
| 41 |
+
token=st.secrets['HUGGING_FACE_TOKEN_3'] # Actualizado de use_auth_token a token
|
| 42 |
)
|
| 43 |
except Exception as e:
|
| 44 |
st.error(f"Error cargando el modelo: {str(e)}")
|
|
|
|
| 45 |
raise e
|
| 46 |
return self._model
|
| 47 |
|
|
|
|
| 51 |
try:
|
| 52 |
self._tokenizer = AutoTokenizer.from_pretrained(
|
| 53 |
self.model_name,
|
| 54 |
+
token=st.secrets['HUGGING_FACE_TOKEN_3'] # Actualizado de use_auth_token a token
|
| 55 |
)
|
| 56 |
except Exception as e:
|
| 57 |
st.error(f"Error cargando el tokenizer: {str(e)}")
|
| 58 |
raise e
|
| 59 |
return self._tokenizer
|
| 60 |
|
| 61 |
+
|
| 62 |
+
##################################################################
|
| 63 |
def generate_response(self, prompt: str, max_new_tokens: int = 512) -> str:
|
|
|
|
| 64 |
formatted_prompt = f"""<|system|>You are a helpful AI assistant.</s>
|
| 65 |
<|user|>{prompt}</s>
|
| 66 |
<|assistant|>"""
|
| 67 |
|
| 68 |
inputs = self.tokenizer(formatted_prompt, return_tensors="pt").to(self.model.device)
|
| 69 |
|
| 70 |
+
# Asegurar que tenemos un pad_token_id v谩lido
|
| 71 |
+
if self.tokenizer.pad_token_id is None:
|
| 72 |
+
self.tokenizer.pad_token_id = self.tokenizer.eos_token_id
|
| 73 |
+
|
| 74 |
with torch.no_grad():
|
| 75 |
outputs = self.model.generate(
|
| 76 |
**inputs,
|
|
|
|
| 78 |
num_return_sequences=1,
|
| 79 |
temperature=0.7,
|
| 80 |
do_sample=True,
|
| 81 |
+
top_p=0.9,
|
| 82 |
+
pad_token_id=self.tokenizer.pad_token_id # Expl铆citamente establecer pad_token_id
|
| 83 |
)
|
| 84 |
|
|
|
|
| 85 |
torch.cuda.empty_cache()
|
| 86 |
|
| 87 |
response = self.tokenizer.decode(outputs[0], skip_special_tokens=True)
|
|
|
|
| 88 |
return response.split("<|assistant|>")[-1].strip()
|
| 89 |
|
| 90 |
+
##################################################################
|
| 91 |
def main():
|
| 92 |
st.set_page_config(page_title="Llama 3.2 Chat", page_icon="馃")
|
| 93 |
|