Upload app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,152 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import sys
|
2 |
+
import os
|
3 |
+
import json
|
4 |
+
import gradio as gr
|
5 |
+
sys.path.append('src')
|
6 |
+
from procesador_de_cvs_con_llm import ProcesadorCV
|
7 |
+
|
8 |
+
use_dotenv = False
|
9 |
+
if use_dotenv:
|
10 |
+
from dotenv import load_dotenv
|
11 |
+
load_dotenv("../../../../../../apis/.env")
|
12 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
13 |
+
|
14 |
+
else:
|
15 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
16 |
+
|
17 |
+
unmasked_chars = 8
|
18 |
+
masked_key = api_key[:unmasked_chars] + '*' * (len(api_key) - unmasked_chars*2) + api_key[-unmasked_chars:]
|
19 |
+
print(f"API key: {masked_key}")
|
20 |
+
|
21 |
+
def process_cv(job_text, cv_text, req_experience, positions_cap, dist_threshold_low, dist_threshold_high):
|
22 |
+
if dist_threshold_low >= dist_threshold_high:
|
23 |
+
return {"error": "dist_threshold_low debe ser más bajo que dist_threshold_high."}
|
24 |
+
|
25 |
+
if not isinstance(cv_text, str) or not cv_text.strip():
|
26 |
+
return {"error": "Por favor, introduce el CV o sube un fichero."}
|
27 |
+
|
28 |
+
try:
|
29 |
+
procesador = ProcesadorCV(api_key, cv_text, job_text, ner_pre_prompt,
|
30 |
+
system_prompt, user_prompt, ner_schema, response_schema)
|
31 |
+
dict_respuesta = procesador.procesar_cv_completo(
|
32 |
+
req_experience=req_experience,
|
33 |
+
positions_cap=positions_cap,
|
34 |
+
dist_threshold_low=dist_threshold_low,
|
35 |
+
dist_threshold_high=dist_threshold_high
|
36 |
+
)
|
37 |
+
return dict_respuesta
|
38 |
+
except Exception as e:
|
39 |
+
return {"error": f"Error en el procesamiento: {str(e)}"}
|
40 |
+
|
41 |
+
# Parámetros de ejecución:
|
42 |
+
job_text = "Generative AI engineer"
|
43 |
+
cv_sample_path = 'cv_examples/reddgr_cv.txt' # Ruta al fichero de texto con un currículo de ejemplo
|
44 |
+
with open(cv_sample_path, 'r', encoding='utf-8') as file:
|
45 |
+
cv_text = file.read()
|
46 |
+
# Prompts:
|
47 |
+
with open('prompts/ner_pre_prompt.txt', 'r', encoding='utf-8') as f:
|
48 |
+
ner_pre_prompt = f.read()
|
49 |
+
with open('prompts/system_prompt.txt', 'r', encoding='utf-8') as f:
|
50 |
+
system_prompt = f.read()
|
51 |
+
with open('prompts/user_prompt.txt', 'r', encoding='utf-8') as f:
|
52 |
+
user_prompt = f.read()
|
53 |
+
# Esquemas JSON:
|
54 |
+
with open('json/ner_schema.json', 'r', encoding='utf-8') as f:
|
55 |
+
ner_schema = json.load(f)
|
56 |
+
with open('json/response_schema.json', 'r', encoding='utf-8') as f:
|
57 |
+
response_schema = json.load(f)
|
58 |
+
|
59 |
+
# Fichero de ejemplo para autocompletar (opción que aparece en la parte de abajo de la interfaz de usuario):
|
60 |
+
with open('cv_examples/reddgr_cv.txt', 'r', encoding='utf-8') as file:
|
61 |
+
cv_example = file.read()
|
62 |
+
|
63 |
+
default_parameters = [48, 10, 0.5, 0.7] # Parámetros por defecto para el reinicio de la interfaz y los ejemplos predefinidos
|
64 |
+
|
65 |
+
# Código CSS para truncar el texto de ejemplo en la interfaz (bloque "Examples" en la parte de abajo):
|
66 |
+
css = """
|
67 |
+
table tbody tr {
|
68 |
+
height: 2.5em; /* Set a fixed height for the rows */
|
69 |
+
overflow: hidden; /* Hide overflow content */
|
70 |
+
}
|
71 |
+
|
72 |
+
table tbody tr td {
|
73 |
+
overflow: hidden; /* Ensure content within cells doesn't overflow */
|
74 |
+
text-overflow: ellipsis; /* Add ellipsis for overflowing text */
|
75 |
+
white-space: nowrap; /* Prevent text from wrapping */
|
76 |
+
vertical-align: middle; /* Align text vertically within the fixed height */
|
77 |
+
}
|
78 |
+
"""
|
79 |
+
|
80 |
+
# Interfaz Gradio:
|
81 |
+
with gr.Blocks(css=css) as interface:
|
82 |
+
# Inputs
|
83 |
+
job_text_input = gr.Textbox(label="Título oferta de trabajo", lines=1, placeholder="Introduce el título de la oferta de trabajo")
|
84 |
+
cv_text_input = gr.Textbox(label="CV en formato texto", lines=5, max_lines=5, placeholder="Introduce el texto del CV")
|
85 |
+
|
86 |
+
# Opciones avanzadas ocultas en un objeto "Accordion"
|
87 |
+
with gr.Accordion("Opciones avanzadas", open=False):
|
88 |
+
req_experience_input = gr.Number(label="Experiencia requerida (en meses)", value=default_parameters[0], precision=0)
|
89 |
+
positions_cap_input = gr.Number(label="Número máximo de puestos a extraer", value=default_parameters[1], precision=0)
|
90 |
+
dist_threshold_low_slider = gr.Slider(
|
91 |
+
label="Umbral mínimo de distancia de embeddings (puesto equivalente)",
|
92 |
+
minimum=0, maximum=1, value=default_parameters[2], step=0.05
|
93 |
+
)
|
94 |
+
dist_threshold_high_slider = gr.Slider(
|
95 |
+
label="Umbral máximo de distancia de embeddings (puesto irrelevante)",
|
96 |
+
minimum=0, maximum=1, value=default_parameters[3], step=0.05
|
97 |
+
)
|
98 |
+
|
99 |
+
submit_button = gr.Button("Procesar")
|
100 |
+
clear_button = gr.Button("Limpiar")
|
101 |
+
|
102 |
+
output_json = gr.JSON(label="Resultado")
|
103 |
+
|
104 |
+
# Ejemplos:
|
105 |
+
examples = gr.Examples(
|
106 |
+
examples=[
|
107 |
+
["Cajero de supermercado", "Trabajo de charcutero desde 2021. Antes trabajé 2 meses de camarero en un bar de tapas."] + default_parameters,
|
108 |
+
["Generative AI Engineer", cv_example] + default_parameters
|
109 |
+
],
|
110 |
+
inputs=[job_text_input, cv_text_input, req_experience_input, positions_cap_input, dist_threshold_low_slider, dist_threshold_high_slider]
|
111 |
+
)
|
112 |
+
|
113 |
+
# Botón "Procesar"
|
114 |
+
submit_button.click(
|
115 |
+
fn=process_cv,
|
116 |
+
inputs=[
|
117 |
+
job_text_input,
|
118 |
+
cv_text_input,
|
119 |
+
req_experience_input,
|
120 |
+
positions_cap_input,
|
121 |
+
dist_threshold_low_slider,
|
122 |
+
dist_threshold_high_slider
|
123 |
+
],
|
124 |
+
outputs=output_json
|
125 |
+
)
|
126 |
+
|
127 |
+
# Botón "Limpiar"
|
128 |
+
clear_button.click(
|
129 |
+
fn=lambda: ("","",*default_parameters),
|
130 |
+
inputs=[],
|
131 |
+
outputs=[
|
132 |
+
job_text_input,
|
133 |
+
cv_text_input,
|
134 |
+
req_experience_input,
|
135 |
+
positions_cap_input,
|
136 |
+
dist_threshold_low_slider,
|
137 |
+
dist_threshold_high_slider
|
138 |
+
]
|
139 |
+
)
|
140 |
+
|
141 |
+
# Footer
|
142 |
+
gr.Markdown("""
|
143 |
+
<footer>
|
144 |
+
<p>Puedes consultar el código completo de esta app y los notebooks explicativos en
|
145 |
+
<a href='https://github.com/reddgr' target='_blank'>GitHub</a></p>
|
146 |
+
<p>© 2024 <a href='https://talkingtochatbots.com' target='_blank'>talkingtochatbots.com</a></p>
|
147 |
+
</footer>
|
148 |
+
""")
|
149 |
+
|
150 |
+
# Lanzar la aplicación:
|
151 |
+
if __name__ == "__main__":
|
152 |
+
interface.launch()
|