Prueba_1 / app.py
JaphetHernandez's picture
Update app.py
f762e1b verified
raw
history blame
2.81 kB
import pandas as pd
import streamlit as st
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
from langchain.llms import HuggingFacePipeline
from huggingface_hub import login
# Token de Hugging Face (Secreto)
huggingface_token = st.secrets["HUGGINGFACEHUB_API_TOKEN"]
login(huggingface_token)
# Cargar el modelo Llama 3.1 y el tokenizador
model_id = "meta-llama/Llama-3.1-1B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
# Crear pipeline de generaci贸n de texto
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer, max_length=1024)
llm_pipeline = HuggingFacePipeline(pipeline=pipe)
# Interfaz de Streamlit
st.title("Cosine Similarity with Llama 3.1")
# Subir archivo CSV
uploaded_file = st.file_uploader("Sube un archivo CSV con la columna 'job_title':", type=["csv"])
if uploaded_file is not None:
# Cargar el CSV en un DataFrame
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 para el LLM
prompt = (
f"You are given a query and a list of job titles. Your task is to calculate the cosine similarity "
f"between the query and each job title. The query is: '{query}'. For each job title, provide the similarity "
f"score as a new column in the dataframe, called 'Score'. Return the dataframe with job titles and scores.\n"
f"Job Titles: {job_titles}\n"
f"Output format:\n"
f"1. Job Title: [Job Title], Score: [Cosine Similarity Score]\n"
f"2. Job Title: [Job Title], Score: [Cosine Similarity Score]\n"
f"..."
)
# Mostrar el prompt inicial
st.write("Prompt enviado al LLM:")
st.write(prompt)
# Generar respuesta del LLM
if st.button("Generar puntajes de similitud"):
with st.spinner("Calculando similitudes con Llama 3.1..."):
try:
response = llm_pipeline(prompt)
st.write("Respuesta del modelo:")
st.write(response)
# Simular la asignaci贸n de puntajes en la columna 'Score' (ya que el modelo no ejecuta c谩lculos reales)
df['Score'] = [0.95] * len(df) # Este paso es solo ilustrativo
# Mostrar el dataframe actualizado
st.write("DataFrame con los puntajes de similitud:")
st.write(df)
except Exception as e:
st.error(f"Error durante la generaci贸n: {e}")
else:
st.error("La columna 'job_title' no se encuentra en el archivo CSV.")