Spaces:
Sleeping
Sleeping
File size: 3,598 Bytes
2a0f243 cf47d83 69e3a41 3d477e1 ea3c34e 69e3a41 2a0f243 85ec4d4 2a0f243 763be08 ea3c34e a77f2b3 bfb224a 70ed6f0 927a0dd a3bc7ec 927a0dd 763be08 927a0dd ea3c34e 927a0dd ea3c34e a3bc7ec 927a0dd 5d3dc3e ea3c34e b99eeda ea3c34e 763be08 2dbd9ac 763be08 105c4c8 006e69f 2a93782 006e69f 2a93782 006e69f 2a93782 006e69f 2dbd9ac 006e69f 763be08 338b938 763be08 2b5a681 ea3c34e b99eeda ea3c34e 763be08 2b5a681 ea3c34e 006e69f |
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
import streamlit as st
from huggingface_hub import login
import pandas as pd
from threading import Thread
# Token Secret de Hugging Face
huggingface_token = st.secrets["HUGGINGFACEHUB_API_TOKEN"]
login(huggingface_token)
# Cargar el tokenizador y el modelo
model_id = "meta-llama/Llama-3.2-1B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
tokenizer.pad_token = tokenizer.eos_token
MAX_INPUT_TOKEN_LENGTH = 10000
# Asegurar que el token de padding est茅 configurado
if tokenizer.pad_token is None:
tokenizer.pad_token = tokenizer.eos_token # Asignar el token de padding al token de fin de oraci贸n
def generate_response(input_text, max_new_tokens=50, temperature=0.5):
"""
Genera una respuesta usando el modelo de lenguaje con m谩scara de atenci贸n.
"""
# Tokenizar la entrada y crear la m谩scara de atenci贸n
inputs = tokenizer(
input_text,
return_tensors='pt',
padding=True,
truncation=True,
max_length=512 # Ajustar seg煤n sea necesario
)
input_ids = inputs['input_ids'].to(model.device)
attention_mask = inputs['attention_mask'].to(model.device)
# Generar texto con la m谩scara de atenci贸n y el token de padding
outputs = model.generate(
input_ids,
attention_mask=attention_mask,
max_new_tokens=max_new_tokens,
temperature=temperature,
pad_token_id=tokenizer.pad_token_id # Usar el token de padding configurado
)
# Decodificar la respuesta generada
response = tokenizer.decode(outputs[0], skip_special_tokens=True)
return response
def main():
st.title("Chat con Meta Llama 3.2 1B")
uploaded_file = st.file_uploader("Por favor, sube un archivo CSV para iniciar:", type=["csv"])
if uploaded_file is not None:
df = pd.read_csv(uploaded_file)
if 'job_title' in df.columns:
query = "aspiring human resources specialist"
job_titles = df['job_title'].tolist()
# Definir el prompt con in-context learning
initial_prompt = (
"You are an AI assistant specialized in job title extraction and similarity calculation.\n"
"Your task is to extract the first job title from the following list and calculate the cosine similarity with the given query.\n"
f"List: {job_titles}\n"
f"Query: '{query}'\n"
"Extracted first job title: \n"
"Cosine similarity score: "
)
st.write("Prompt inicial con In-context Learning:")
st.write(query)
st.write(initial_prompt)
if st.button("Generar respuesta"):
with st.spinner("Generando respuesta..."):
response = generate_response(initial_prompt, temperature=0.5)
if response:
st.write(f"Respuesta del modelo: {response}")
else:
st.warning("No se pudo generar una respuesta.")
st.success("La conversaci贸n ha terminado.")
if st.button("Iniciar nueva conversaci贸n"):
st.experimental_rerun()
elif st.button("Terminar"):
st.stop()
else:
st.error("La columna 'job_title' no se encuentra en el archivo CSV.")
if __name__ == "__main__":
main()
|