Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +39 -23
src/streamlit_app.py
CHANGED
@@ -1,17 +1,18 @@
|
|
|
|
|
|
1 |
import os
|
2 |
import sys
|
3 |
import asyncio
|
4 |
|
5 |
-
#
|
6 |
# 0) Crear y setear un event loop en este hilo (ScriptRunner.scriptThread)
|
7 |
-
#
|
8 |
loop = asyncio.new_event_loop()
|
9 |
asyncio.set_event_loop(loop)
|
10 |
|
11 |
-
#
|
12 |
-
# 1) Redirigir
|
13 |
-
#
|
14 |
-
# ───────────────────────────────────────────────────
|
15 |
os.environ["HOME"] = "/tmp"
|
16 |
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
|
17 |
try:
|
@@ -19,57 +20,71 @@ try:
|
|
19 |
except Exception:
|
20 |
pass
|
21 |
|
22 |
-
#
|
23 |
-
# 2)
|
24 |
-
#
|
25 |
-
HERE = os.path.dirname(__file__)
|
26 |
if HERE not in sys.path:
|
27 |
sys.path.insert(0, HERE)
|
28 |
|
29 |
-
#
|
30 |
-
# 3) Ahora sí importamos Streamlit y
|
31 |
-
#
|
32 |
import streamlit as st
|
33 |
from agent.linkedin_agent import create_agent
|
34 |
-
from tools.web_search_tool import get_web_search_tool
|
35 |
from agents import Runner
|
36 |
|
37 |
-
#
|
38 |
-
# 4)
|
39 |
-
#
|
40 |
st.set_page_config(page_title="Buscador de Perfiles LinkedIn", layout="centered")
|
41 |
|
42 |
if not os.getenv("OPENAI_API_KEY"):
|
43 |
-
st.error("❌
|
44 |
st.stop()
|
45 |
|
|
|
|
|
|
|
46 |
agent = create_agent()
|
47 |
|
|
|
|
|
|
|
48 |
st.title("🔍 Buscador de Perfiles LinkedIn")
|
49 |
-
st.write("Ingresa la descripción de la oferta y la cantidad de perfiles que deseas encontrar.")
|
50 |
|
|
|
51 |
if "history" not in st.session_state:
|
52 |
st.session_state.history = []
|
53 |
|
|
|
54 |
with st.form(key="oferta_form", clear_on_submit=False):
|
55 |
oferta = st.text_area("📰 Descripción de la oferta de empleo:", height=150)
|
56 |
num_perfiles = st.number_input(
|
57 |
"🔢 Cantidad de perfiles a buscar:",
|
58 |
-
min_value=1,
|
|
|
|
|
59 |
)
|
60 |
enviar = st.form_submit_button(label="Enviar al agente")
|
61 |
|
|
|
62 |
if enviar and oferta:
|
|
|
63 |
st.session_state.history.append(("usuario", oferta))
|
|
|
|
|
64 |
prompt = f"Oferta: {oferta}\nNúmero perfiles: {num_perfiles}"
|
65 |
|
66 |
-
#
|
67 |
-
# Aquí ya existe un event loop, así que run_sync no fallará
|
68 |
-
# ───────────────────────────────────────────────────
|
69 |
resultado = Runner.run_sync(agent, prompt)
|
70 |
respuesta = resultado.final_output
|
|
|
|
|
71 |
st.session_state.history.append(("agente", respuesta))
|
72 |
|
|
|
73 |
st.markdown("---")
|
74 |
st.markdown("### 💬 Historial de Chat")
|
75 |
for quien, texto in st.session_state.history:
|
@@ -78,5 +93,6 @@ for quien, texto in st.session_state.history:
|
|
78 |
else:
|
79 |
st.markdown(f"**Agente:** {texto}")
|
80 |
|
|
|
81 |
if st.button("🔄 Limpiar historial"):
|
82 |
st.session_state.history = []
|
|
|
1 |
+
# src/streamlit_app.py
|
2 |
+
|
3 |
import os
|
4 |
import sys
|
5 |
import asyncio
|
6 |
|
7 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
8 |
# 0) Crear y setear un event loop en este hilo (ScriptRunner.scriptThread)
|
9 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
10 |
loop = asyncio.new_event_loop()
|
11 |
asyncio.set_event_loop(loop)
|
12 |
|
13 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
14 |
+
# 1) Redirigir HOME y StreamlitConfig a /tmp/.streamlit (es escribible en HF Spaces)
|
15 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
|
|
16 |
os.environ["HOME"] = "/tmp"
|
17 |
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
|
18 |
try:
|
|
|
20 |
except Exception:
|
21 |
pass
|
22 |
|
23 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
24 |
+
# 2) Añadir src/ a sys.path para que Python encuentre agent/ y tools/
|
25 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
26 |
+
HERE = os.path.dirname(__file__) # debería apuntar a /app/src
|
27 |
if HERE not in sys.path:
|
28 |
sys.path.insert(0, HERE)
|
29 |
|
30 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
31 |
+
# 3) Ahora sí importamos Streamlit, el agente y Runner
|
32 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
33 |
import streamlit as st
|
34 |
from agent.linkedin_agent import create_agent
|
|
|
35 |
from agents import Runner
|
36 |
|
37 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
38 |
+
# 4) Configuración de la página y verificación de la clave de OpenAI
|
39 |
+
# ─────────────────────────────────���─────────────────────────────────────────────
|
40 |
st.set_page_config(page_title="Buscador de Perfiles LinkedIn", layout="centered")
|
41 |
|
42 |
if not os.getenv("OPENAI_API_KEY"):
|
43 |
+
st.error("❌ Por favor define el secret OPENAI_API_KEY en Settings → Secrets de tu Space.")
|
44 |
st.stop()
|
45 |
|
46 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
47 |
+
# 5) Instanciar el agente (solo una vez al inicio)
|
48 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
49 |
agent = create_agent()
|
50 |
|
51 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
52 |
+
# 6) Interfaz de usuario (chat) en Streamlit
|
53 |
+
# ───────────────────────────────────────────────────────────────────────────────
|
54 |
st.title("🔍 Buscador de Perfiles LinkedIn")
|
55 |
+
st.write("Ingresa la descripción de la oferta de empleo y la cantidad de perfiles que deseas encontrar.")
|
56 |
|
57 |
+
# Inicializar historial en la sesión si aún no existe
|
58 |
if "history" not in st.session_state:
|
59 |
st.session_state.history = []
|
60 |
|
61 |
+
# Formulario para recibir la oferta y el número de perfiles
|
62 |
with st.form(key="oferta_form", clear_on_submit=False):
|
63 |
oferta = st.text_area("📰 Descripción de la oferta de empleo:", height=150)
|
64 |
num_perfiles = st.number_input(
|
65 |
"🔢 Cantidad de perfiles a buscar:",
|
66 |
+
min_value=1,
|
67 |
+
max_value=20,
|
68 |
+
value=5
|
69 |
)
|
70 |
enviar = st.form_submit_button(label="Enviar al agente")
|
71 |
|
72 |
+
# Cuando el usuario pulsa "Enviar al agente"
|
73 |
if enviar and oferta:
|
74 |
+
# Agregar el mensaje del usuario al historial
|
75 |
st.session_state.history.append(("usuario", oferta))
|
76 |
+
|
77 |
+
# Construir el prompt en el formato que espera el agente
|
78 |
prompt = f"Oferta: {oferta}\nNúmero perfiles: {num_perfiles}"
|
79 |
|
80 |
+
# Ejecutar el agente de forma síncrona (ahora existe un event loop)
|
|
|
|
|
81 |
resultado = Runner.run_sync(agent, prompt)
|
82 |
respuesta = resultado.final_output
|
83 |
+
|
84 |
+
# Agregar la respuesta del agente al historial
|
85 |
st.session_state.history.append(("agente", respuesta))
|
86 |
|
87 |
+
# Mostrar el historial tipo “chat”
|
88 |
st.markdown("---")
|
89 |
st.markdown("### 💬 Historial de Chat")
|
90 |
for quien, texto in st.session_state.history:
|
|
|
93 |
else:
|
94 |
st.markdown(f"**Agente:** {texto}")
|
95 |
|
96 |
+
# Botón para limpiar el historial
|
97 |
if st.button("🔄 Limpiar historial"):
|
98 |
st.session_state.history = []
|