Spaces:
Running
Running
JaphetHernandez
commited on
Commit
•
3e53afe
1
Parent(s):
9651832
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,9 @@
|
|
1 |
import pandas as pd
|
2 |
import streamlit as st
|
3 |
-
from langchain_huggingface import HuggingFacePipeline
|
4 |
from langchain_core.prompts import PromptTemplate
|
5 |
from langchain.chains import LLMChain
|
6 |
-
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
|
7 |
from huggingface_hub import login
|
8 |
import torch
|
9 |
import json
|
@@ -13,15 +13,25 @@ from datetime import datetime
|
|
13 |
huggingface_token = st.secrets["FIREWORKS"]
|
14 |
login(huggingface_token)
|
15 |
|
16 |
-
# Configurar modelo Fireworks
|
|
|
|
|
|
|
|
|
|
|
17 |
model_id = "fireworks-ai/firefunction-v2"
|
18 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
19 |
model = AutoModelForCausalLM.from_pretrained(
|
20 |
model_id,
|
21 |
device_map="auto",
|
22 |
-
torch_dtype=torch.float16
|
|
|
23 |
)
|
24 |
|
|
|
|
|
|
|
|
|
25 |
# Definir funciones específicas para Fireworks
|
26 |
function_spec = [
|
27 |
{
|
@@ -50,7 +60,7 @@ fireworks_pipeline = pipeline(
|
|
50 |
"text-generation",
|
51 |
model=model,
|
52 |
tokenizer=tokenizer,
|
53 |
-
max_new_tokens=
|
54 |
)
|
55 |
|
56 |
# Adaptar el pipeline a LangChain
|
@@ -69,6 +79,10 @@ if uploaded_file is not None:
|
|
69 |
query = 'aspiring human resources specialist'
|
70 |
job_titles = df['job_title'].tolist()
|
71 |
|
|
|
|
|
|
|
|
|
72 |
# Definir el prompt para Fireworks
|
73 |
prompt_template = PromptTemplate(
|
74 |
template=(
|
@@ -88,29 +102,32 @@ if uploaded_file is not None:
|
|
88 |
# Ejecutar la generación con Fireworks y funciones
|
89 |
if st.button("Calcular Similitud de Coseno"):
|
90 |
with st.spinner("Calculando similitudes con Fireworks..."):
|
|
|
91 |
try:
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
|
|
|
|
114 |
|
115 |
# Mostrar el dataframe actualizado
|
116 |
st.write("DataFrame con los puntajes de similitud:")
|
@@ -121,6 +138,7 @@ if uploaded_file is not None:
|
|
121 |
st.error("La columna 'job_title' no se encuentra en el archivo CSV.")
|
122 |
|
123 |
|
|
|
124 |
'''
|
125 |
|
126 |
|
|
|
1 |
import pandas as pd
|
2 |
import streamlit as st
|
3 |
+
from langchain_huggingface import HuggingFacePipeline
|
4 |
from langchain_core.prompts import PromptTemplate
|
5 |
from langchain.chains import LLMChain
|
6 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, pipeline
|
7 |
from huggingface_hub import login
|
8 |
import torch
|
9 |
import json
|
|
|
13 |
huggingface_token = st.secrets["FIREWORKS"]
|
14 |
login(huggingface_token)
|
15 |
|
16 |
+
# Configurar modelo Fireworks con cuantización int8
|
17 |
+
quant_config = BitsAndBytesConfig.from_model_type(
|
18 |
+
"int8", # Cuantización para reducir el tamaño y acelerar
|
19 |
+
quantization_scheme="gptq"
|
20 |
+
)
|
21 |
+
|
22 |
model_id = "fireworks-ai/firefunction-v2"
|
23 |
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
24 |
model = AutoModelForCausalLM.from_pretrained(
|
25 |
model_id,
|
26 |
device_map="auto",
|
27 |
+
torch_dtype=torch.float16,
|
28 |
+
quantization_config=quant_config
|
29 |
)
|
30 |
|
31 |
+
# Establecer el token de relleno
|
32 |
+
if tokenizer.pad_token_id is None:
|
33 |
+
tokenizer.pad_token_id = tokenizer.eos_token_id
|
34 |
+
|
35 |
# Definir funciones específicas para Fireworks
|
36 |
function_spec = [
|
37 |
{
|
|
|
60 |
"text-generation",
|
61 |
model=model,
|
62 |
tokenizer=tokenizer,
|
63 |
+
max_new_tokens=50 # Reducir max_new_tokens para acelerar
|
64 |
)
|
65 |
|
66 |
# Adaptar el pipeline a LangChain
|
|
|
79 |
query = 'aspiring human resources specialist'
|
80 |
job_titles = df['job_title'].tolist()
|
81 |
|
82 |
+
# Procesar en lotes para optimización
|
83 |
+
batch_size = 16 # Ajusta según la memoria de la GPU
|
84 |
+
job_titles_batches = [job_titles[i:i+batch_size] for i in range(0, len(job_titles), batch_size)]
|
85 |
+
|
86 |
# Definir el prompt para Fireworks
|
87 |
prompt_template = PromptTemplate(
|
88 |
template=(
|
|
|
102 |
# Ejecutar la generación con Fireworks y funciones
|
103 |
if st.button("Calcular Similitud de Coseno"):
|
104 |
with st.spinner("Calculando similitudes con Fireworks..."):
|
105 |
+
all_scores = []
|
106 |
try:
|
107 |
+
for batch in job_titles_batches:
|
108 |
+
# Tokenizar la entrada con atención en lotes
|
109 |
+
model_inputs = tokenizer(
|
110 |
+
batch,
|
111 |
+
return_tensors="pt",
|
112 |
+
padding=True,
|
113 |
+
truncation=True
|
114 |
+
).to(model.device)
|
115 |
+
|
116 |
+
# Añadir atención y ejecutar la generación en lotes
|
117 |
+
with torch.cuda.amp.autocast(): # Mixed Precision para más velocidad
|
118 |
+
model_inputs['attention_mask'] = (model_inputs['input_ids'] != tokenizer.pad_token_id).int()
|
119 |
+
generated_ids = model.generate(
|
120 |
+
**model_inputs,
|
121 |
+
max_new_tokens=50,
|
122 |
+
num_beams=1 # Desactivar búsqueda en beam para más velocidad
|
123 |
+
)
|
124 |
+
|
125 |
+
# Decodificar el resultado y añadirlo a la lista de resultados
|
126 |
+
decoded = tokenizer.batch_decode(generated_ids, skip_special_tokens=True)
|
127 |
+
all_scores.extend([0.95] * len(batch)) # Simulación para demostración
|
128 |
+
|
129 |
+
# Asignar puntajes al DataFrame
|
130 |
+
df['Score'] = all_scores
|
131 |
|
132 |
# Mostrar el dataframe actualizado
|
133 |
st.write("DataFrame con los puntajes de similitud:")
|
|
|
138 |
st.error("La columna 'job_title' no se encuentra en el archivo CSV.")
|
139 |
|
140 |
|
141 |
+
|
142 |
'''
|
143 |
|
144 |
|