Spaces:
Sleeping
Sleeping
File size: 6,228 Bytes
3fa74ff f221d3b bf715da 674f02f 3fa74ff bf715da 3fa74ff bf715da 3fa74ff bf715da 3ac27b6 f221d3b 6b6c0d0 3fa74ff f221d3b 3ac27b6 3fa74ff a721a61 3ac27b6 a721a61 3fa74ff a721a61 3fa74ff a721a61 3fa74ff a721a61 3fa74ff 966cd25 3fa74ff a721a61 3fa74ff a721a61 3fa74ff a721a61 bf715da 3fa74ff a721a61 3fa74ff a721a61 3fa74ff a721a61 3fa74ff a721a61 bf715da 3fa74ff a721a61 3fa74ff a721a61 3fa74ff 966cd25 a721a61 3fa74ff a721a61 |
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 |
# src/streamlit_app.py
import os
import sys
import asyncio
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 0) Crear y setear un event loop en este hilo (ScriptRunner.scriptThread)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 1) Redirigir HOME y StreamlitConfig a /tmp/.streamlit (es escribible en HF Spaces)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
os.environ["HOME"] = "/tmp"
os.environ["STREAMLIT_CONFIG_DIR"] = "/tmp/.streamlit"
try:
os.makedirs("/tmp/.streamlit", exist_ok=True)
except Exception:
pass
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 2) AΓ±adir src/ a sys.path para que Python encuentre agent/ y tools/
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
HERE = os.path.dirname(__file__) # deberΓa apuntar a /app/src
if HERE not in sys.path:
sys.path.insert(0, HERE)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 3) Ahora sΓ importamos Streamlit, el agente y Runner
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
import streamlit as st
from agent.linkedin_agent import create_agent
from agents import Runner
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 4) ConfiguraciΓ³n de la pΓ‘gina y verificaciΓ³n de la clave de OpenAI
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.set_page_config(page_title="Buscador de Perfiles LinkedIn", layout="centered")
if not os.getenv("OPENAI_API_KEY"):
st.error("β Por favor define el secret OPENAI_API_KEY en Settings β Secrets de tu Space.")
st.stop()
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 5) Instanciar el agente (solo una vez al inicio)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
agent = create_agent()
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# 6) Interfaz de usuario (chat) en Streamlit
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
st.title("π Buscador de Perfiles LinkedIn")
st.write("Ingresa la descripciΓ³n de la oferta de empleo y la cantidad de perfiles que deseas encontrar.")
# Inicializar historial en la sesiΓ³n si aΓΊn no existe
if "history" not in st.session_state:
st.session_state.history = []
# Formulario para recibir la oferta y el nΓΊmero de perfiles
with st.form(key="oferta_form", clear_on_submit=False):
oferta = st.text_area("π° DescripciΓ³n de la oferta de empleo:", height=150)
num_perfiles = st.number_input(
"π’ Cantidad de perfiles a buscar:",
min_value=1,
max_value=20,
value=5
)
enviar = st.form_submit_button(label="Enviar al agente")
# Cuando el usuario pulsa "Enviar al agente"
if enviar and oferta:
# Agregar el mensaje del usuario al historial
st.session_state.history.append(("usuario", oferta))
# Construir el prompt en el formato que espera el agente
prompt = f"Oferta: {oferta}\nNΓΊmero perfiles: {num_perfiles}"
# Ejecutar el agente de forma sΓncrona (ahora existe un event loop)
resultado = Runner.run_sync(agent, prompt)
respuesta = resultado.final_output
# Agregar la respuesta del agente al historial
st.session_state.history.append(("agente", respuesta))
# Mostrar el historial tipo βchatβ
st.markdown("---")
st.markdown("### π¬ Historial de Chat")
for quien, texto in st.session_state.history:
if quien == "usuario":
st.markdown(f"**TΓΊ:** {texto}")
else:
st.markdown(f"**Agente:** {texto}")
# BotΓ³n para limpiar el historial
if st.button("π Limpiar historial"):
st.session_state.history = []
|