Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -14,40 +14,43 @@ tokenizer = AutoTokenizer.from_pretrained(model_id)
|
|
14 |
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
|
15 |
tokenizer.pad_token = tokenizer.eos_token
|
16 |
|
17 |
-
MAX_INPUT_TOKEN_LENGTH =
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
tokenizer.pad_token = tokenizer.eos_token # Asignar el token de padding al token de fin de oraci贸n
|
22 |
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
"""
|
27 |
-
# Tokenizar la entrada y crear la m谩scara de atenci贸n
|
28 |
-
inputs = tokenizer(
|
29 |
-
input_text,
|
30 |
-
return_tensors='pt',
|
31 |
-
padding=True,
|
32 |
-
truncation=True,
|
33 |
-
max_length=512 # Ajustar seg煤n sea necesario
|
34 |
-
)
|
35 |
-
|
36 |
-
input_ids = inputs['input_ids'].to(model.device)
|
37 |
-
attention_mask = inputs['attention_mask'].to(model.device)
|
38 |
|
39 |
-
|
40 |
-
|
41 |
-
input_ids,
|
42 |
-
|
43 |
max_new_tokens=max_new_tokens,
|
|
|
|
|
|
|
44 |
temperature=temperature,
|
45 |
-
|
46 |
)
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
51 |
|
52 |
def main():
|
53 |
st.title("Chat con Meta Llama 3.2 1B")
|
@@ -58,23 +61,25 @@ def main():
|
|
58 |
df = pd.read_csv(uploaded_file)
|
59 |
|
60 |
if 'job_title' in df.columns:
|
61 |
-
query = "aspiring human resources specialist"
|
62 |
job_titles = df['job_title'].tolist()
|
63 |
|
64 |
# Definir el prompt con in-context learning
|
65 |
initial_prompt = (
|
66 |
-
"
|
67 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
f"List: {job_titles}\n"
|
69 |
-
|
70 |
-
"Extracted first job title: \n"
|
71 |
-
"Cosine similarity score: "
|
72 |
)
|
73 |
-
|
74 |
-
|
75 |
|
76 |
st.write("Prompt inicial con In-context Learning:")
|
77 |
-
st.write(query)
|
78 |
st.write(initial_prompt)
|
79 |
|
80 |
if st.button("Generar respuesta"):
|
@@ -96,4 +101,3 @@ def main():
|
|
96 |
|
97 |
if __name__ == "__main__":
|
98 |
main()
|
99 |
-
|
|
|
14 |
model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto")
|
15 |
tokenizer.pad_token = tokenizer.eos_token
|
16 |
|
17 |
+
MAX_INPUT_TOKEN_LENGTH = 4096
|
18 |
|
19 |
+
def generate_response(input_text, temperature=0.5, max_new_tokens=50):
|
20 |
+
input_ids = tokenizer.encode(input_text, return_tensors='pt').to(model.device)
|
|
|
21 |
|
22 |
+
if input_ids.shape[1] > MAX_INPUT_TOKEN_LENGTH:
|
23 |
+
input_ids = input_ids[:, -MAX_INPUT_TOKEN_LENGTH:]
|
24 |
+
st.warning(f"Se recort贸 la entrada porque excedi贸 el l铆mite de {MAX_INPUT_TOKEN_LENGTH} tokens.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
25 |
|
26 |
+
streamer = TextIteratorStreamer(tokenizer, timeout=30.0, skip_prompt=True, skip_special_tokens=True)
|
27 |
+
generate_kwargs = dict(
|
28 |
+
input_ids=input_ids,
|
29 |
+
streamer=streamer,
|
30 |
max_new_tokens=max_new_tokens,
|
31 |
+
do_sample=True,
|
32 |
+
top_k=40,
|
33 |
+
top_p=0.9,
|
34 |
temperature=temperature,
|
35 |
+
eos_token_id=[tokenizer.eos_token_id]
|
36 |
)
|
37 |
|
38 |
+
try:
|
39 |
+
t = Thread(target=model.generate, kwargs=generate_kwargs)
|
40 |
+
t.start()
|
41 |
+
t.join() # Asegura que la generaci贸n haya terminado
|
42 |
+
|
43 |
+
outputs = []
|
44 |
+
for text in streamer:
|
45 |
+
outputs.append(text)
|
46 |
+
if not outputs:
|
47 |
+
raise ValueError("No se gener贸 ninguna respuesta.")
|
48 |
+
|
49 |
+
response = "".join(outputs).strip().split("\n")[0]
|
50 |
+
return response
|
51 |
+
except Exception as e:
|
52 |
+
st.error(f"Error durante la generaci贸n: {e}")
|
53 |
+
return "Error en la generaci贸n de texto."
|
54 |
|
55 |
def main():
|
56 |
st.title("Chat con Meta Llama 3.2 1B")
|
|
|
61 |
df = pd.read_csv(uploaded_file)
|
62 |
|
63 |
if 'job_title' in df.columns:
|
|
|
64 |
job_titles = df['job_title'].tolist()
|
65 |
|
66 |
# Definir el prompt con in-context learning
|
67 |
initial_prompt = (
|
68 |
+
"Here are some examples of job title extraction:\n"
|
69 |
+
"Example 1:\n"
|
70 |
+
"List: ['Data Scientist', 'Machine Learning Engineer', 'AI Researcher']\n"
|
71 |
+
"First job title: 'Data Scientist'\n"
|
72 |
+
"\n"
|
73 |
+
"Example 2:\n"
|
74 |
+
"List: ['Software Developer', 'Backend Engineer', 'Frontend Developer']\n"
|
75 |
+
"First job title: 'Software Developer'\n"
|
76 |
+
"\n"
|
77 |
+
"Now, extract the first job title from the following list:\n"
|
78 |
f"List: {job_titles}\n"
|
79 |
+
"First job title:"
|
|
|
|
|
80 |
)
|
|
|
|
|
81 |
|
82 |
st.write("Prompt inicial con In-context Learning:")
|
|
|
83 |
st.write(initial_prompt)
|
84 |
|
85 |
if st.button("Generar respuesta"):
|
|
|
101 |
|
102 |
if __name__ == "__main__":
|
103 |
main()
|
|