File size: 2,808 Bytes
f762e1b
cf47d83
f762e1b
 
69e3a41
 
f762e1b
85ec4d4
 
 
f762e1b
 
ea3c34e
275dee5
70ed6f0
f762e1b
 
 
a3bc7ec
f762e1b
 
763be08
f762e1b
 
a3bc7ec
f762e1b
 
 
ea3c34e
f762e1b
c2b4dad
f762e1b
105c4c8
f762e1b
 
 
 
 
 
 
 
 
 
 
ee5951e
f762e1b
 
 
763be08
f762e1b
 
 
 
 
 
 
2b5a681
f762e1b
 
2b5a681
f762e1b
 
 
 
 
 
 
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
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.")