File size: 3,599 Bytes
2a0f243
cf47d83
69e3a41
3d477e1
ea3c34e
69e3a41
2a0f243
85ec4d4
 
 
2a0f243
b1382eb
251623f
2a0f243
ea3c34e
 
a77f2b3
 
ea3c34e
 
70ed6f0
47ec4fb
ea3c34e
 
 
a3bc7ec
ea3c34e
 
 
 
 
 
3cbbebc
 
ea3c34e
 
 
a3bc7ec
ea3c34e
 
 
904cf6c
ea3c34e
 
 
5d3dc3e
 
 
 
904cf6c
ea3c34e
2a0f243
ea3c34e
 
 
 
 
 
4fd169c
ea3c34e
 
69302d4
ea3c34e
81e6d96
2b17ca4
219f9d3
 
 
81e6d96
 
4fd169c
ea3c34e
2b5a681
ea3c34e
 
 
5d3dc3e
ea3c34e
2b5a681
ea3c34e
 
 
 
 
 
 
 
2b5a681
ea3c34e
 
2b5a681
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
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)

# Cambiar a la versi贸n Meta Llama 3.1 3B
model_id = "meta-llama/Llama-3.2-1B"

# Cargar el tokenizador y el modelo
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
tokenizer.pad_token = tokenizer.eos_token

# Definir longitud m谩xima de tokens
MAX_INPUT_TOKEN_LENGTH = 4096

def generate_response(input_text, temperature=0.7, max_new_tokens=50):
    """Funci贸n de generaci贸n de texto con el modelo."""
    input_ids = tokenizer.encode(input_text, return_tensors='pt')
    input_ids = input_ids.to(model.device)

    streamer = TextIteratorStreamer(tokenizer, timeout=10.0, skip_prompt=True, skip_special_tokens=True)
    generate_kwargs = dict(
        input_ids=input_ids,
        streamer=streamer,
        max_new_tokens=max_new_tokens,
        do_sample=temperature != 0,
        top_k=50,  # Limita las palabras m谩s probables a 50
        top_p=0.9,  # Considera solo el 90% de la probabilidad acumulada
        temperature=temperature,
        eos_token_id=[tokenizer.eos_token_id]
    )

    # Generaci贸n de texto en un hilo separado
    t = Thread(target=model.generate, kwargs=generate_kwargs)
    t.start()

    outputs = []
    for text in streamer:
        outputs.append(text)

    # Devolver la respuesta completa
    return "".join(outputs)


def main():
    st.title("Chat con Meta Llama 3.1 3B")
    
    # Paso 1: Subir el archivo CSV
    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)
        query = "aspiring human resources specialist"
        st.write("Archivo CSV cargado exitosamente:")
        st.write(df.head())  # Mostrar las primeras filas del dataframe
        job_titles = df['job_title'].tolist()
        # Prompt inicial
        initial_prompt = (""""
            f"You are an AI assistant. You have a list of job titles in {job_titles} and a search query {query}.\n"
            f"Your task is to rank these job titles {job_titles} by their semantic similarity to the given query {query} using the cosine similarity score.\n "
            f"Create a dataframe called df_2 that includes a column 'titles' with the values of {job_titles} and a new column called 'score'  in which you will record the cosine similarity score that you calculated previously.\n"
            f"Finally, print df_2 in the screen"
            """
        )
        st.write(f"Query: {query}")
        st.write(f"Prompt inicial: {initial_prompt}")

        # Generar la respuesta del modelo
        if st.button("Generar respuesta"):
            with st.spinner("Generando respuesta..."):
                response = generate_response(initial_prompt)  # Obtener la primera respuesta completa
                st.write(f"Respuesta del modelo: {response}")

                # Terminar la conversaci贸n
                st.success("La conversaci贸n ha terminado.")
                
                # Opci贸n para reiniciar o finalizar
                if st.button("Iniciar nueva conversaci贸n"):
                    st.experimental_rerun()  # Reinicia la aplicaci贸n
                elif st.button("Terminar"):
                    st.stop()

if __name__ == "__main__":
    main()