File size: 3,949 Bytes
2a0f243
cf47d83
69e3a41
3d477e1
275dee5
69e3a41
2a0f243
85ec4d4
 
 
2a0f243
763be08
ea3c34e
275dee5
a77f2b3
 
fde1d1c
70ed6f0
612500f
275dee5
a3bc7ec
58a4111
 
 
763be08
fde1d1c
58a4111
 
 
ea3c34e
58a4111
fde1d1c
58a4111
ea3c34e
612500f
275dee5
ea3c34e
a3bc7ec
58a4111
275dee5
 
 
 
 
 
 
 
 
 
 
 
58a4111
 
 
5d3dc3e
ea3c34e
b99eeda
ea3c34e
 
 
 
 
c2b4dad
275dee5
763be08
275dee5
105c4c8
006e69f
 
275dee5
 
 
 
 
 
 
 
 
 
006e69f
ee5951e
275dee5
612500f
006e69f
275dee5
763be08
 
 
338b938
763be08
 
 
 
2b5a681
ea3c34e
 
 
b99eeda
ea3c34e
 
763be08
 
2b5a681
ea3c34e
275dee5
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
101
102
103
104
105
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

def generate_response(input_text, temperature=0.7, max_new_tokens=20):
    input_ids = tokenizer.encode(input_text, return_tensors='pt').to(model.device)

    if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
        input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
        st.warning(f"Se recort贸 la entrada porque excedi贸 el l铆mite de {MAX_INPUT_TOKEN_LENGTH} tokens.")

    streamer = TextIteratorStreamer(tokenizer, timeout=120.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=True,
        top_k=20,
        top_p=0.9,
        temperature=temperature,
        num_return_sequences=3,
        eos_token_id=[tokenizer.eos_token_id]
    )

    try:
        t = Thread(target=model.generate, kwargs=generate_kwargs)
        t.start()
        t.join()  # Asegura que la generaci贸n haya terminado

        outputs = []
        for text in streamer:
            outputs.append(text)
        if not outputs:
            raise ValueError("No se gener贸 ninguna respuesta.")
        
        response = "".join(outputs).strip().split("\n")[0]
        return response
    except Exception as e:
        st.error(f"Error durante la generaci贸n: {e}")
        return "Error en la generaci贸n de texto."

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)
        query = 'aspiring human resources specialist'
        value = 0.00
        if 'job_title' in df.columns:
            job_titles = df['job_title']

            # Definir el prompt con in-context learning
            initial_prompt = (
                "Step 1: Extract the first record from the dataframe df.\n"
                f" {df.iloc[0]['job_title']}\n"
                #f"List: {job_titles}\n"
                #"First job title: \n"
                #"\n"
                "Step 2: Calculate the cosine similarity score between the job_title of the extracted record {df.iloc[0]['job_title']} and the given {query} and assign it to {value}.\n"
                f"Query: '{query}'\n"
                "Cosine similarity score: \n"
                "Step 3: Print the value of the calculated cosine similarity"
                f"Result: {value}"
            )

            
            st.write("Prompt inicial con In-context Learning:\n")
            st.write(initial_prompt)
            st.write(query)

            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()