Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,126 +6,123 @@ import os
|
|
| 6 |
|
| 7 |
from openai import OpenAI
|
| 8 |
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
st.set_page_config(page_title="Manuales de Procedimientos de SCHT")
|
| 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 |
-
st.sidebar.markdown(st.session_state.session_id)
|
| 44 |
-
st.sidebar.divider()
|
| 45 |
-
|
| 46 |
-
if "assistant" not in st.session_state:
|
| 47 |
-
|
| 48 |
-
st.session_state.assistant = openai.beta.assistants.retrieve(assistant_id='asst_ZNKKXJLxzvwX7zVo0cVR1USv')
|
| 49 |
-
|
| 50 |
-
# Create a new thread for this session
|
| 51 |
-
st.session_state.thread = client.beta.threads.create(
|
| 52 |
-
metadata={
|
| 53 |
-
'session_id': st.session_state.session_id,
|
| 54 |
-
}
|
| 55 |
-
)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
st.rerun()
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
# Display messages
|
| 74 |
-
for message in reversed(st.session_state.messages.data):
|
| 75 |
-
if message.role in ["user", "assistant"]:
|
| 76 |
-
with st.chat_message(message.role):
|
| 77 |
-
for content_part in message.content:
|
| 78 |
-
message_text = content_part.text.value
|
| 79 |
-
st.markdown(message_text)
|
| 80 |
-
|
| 81 |
-
if prompt := st.chat_input("Escribe tu consulta sobre los procedimientos de SCHT"):
|
| 82 |
-
with st.chat_message('user'):
|
| 83 |
-
st.write(prompt)
|
| 84 |
-
|
| 85 |
-
# Add message to the thread
|
| 86 |
-
st.session_state.messages = client.beta.threads.messages.create(
|
| 87 |
-
thread_id=st.session_state.thread.id,
|
| 88 |
-
role="user",
|
| 89 |
-
content=prompt
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
# Do a run to process the messages in the thread
|
| 93 |
-
st.session_state.run = client.beta.threads.runs.create(
|
| 94 |
-
thread_id=st.session_state.thread.id,
|
| 95 |
-
assistant_id=st.session_state.assistant.id,
|
| 96 |
-
)
|
| 97 |
if st.session_state.retry_error < 3:
|
| 98 |
-
time.sleep(
|
| 99 |
st.rerun()
|
| 100 |
-
|
| 101 |
-
#
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
with st.chat_message('assistant'):
|
| 106 |
-
st.write("Pensando ......")
|
| 107 |
if st.session_state.retry_error < 3:
|
| 108 |
-
|
|
|
|
| 109 |
st.rerun()
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
elif st.session_state.run.status != "completed":
|
| 124 |
-
# Attempt to retrieve the run again, possibly redundant if there's no other status but 'running' or 'failed'
|
| 125 |
-
st.session_state.run = client.beta.threads.runs.retrieve(
|
| 126 |
-
thread_id=st.session_state.thread.id,
|
| 127 |
-
run_id=st.session_state.run.id,
|
| 128 |
-
)
|
| 129 |
-
if st.session_state.retry_error < 3:
|
| 130 |
-
time.sleep(3)
|
| 131 |
-
st.rerun()
|
|
|
|
| 6 |
|
| 7 |
from openai import OpenAI
|
| 8 |
|
| 9 |
+
openai.api_key = os.environ['OPENAI_API_KEY']
|
| 10 |
|
| 11 |
+
client = OpenAI()
|
|
|
|
| 12 |
|
| 13 |
+
MODEL = "gpt-4-1106-preview"
|
| 14 |
|
| 15 |
+
if "session_id" not in st.session_state: # Used to identify each session
|
| 16 |
+
st.session_state.session_id = str(uuid.uuid4())
|
| 17 |
|
| 18 |
+
if "run" not in st.session_state: # Stores the run state of the assistant
|
| 19 |
+
st.session_state.run = {"status": None}
|
| 20 |
|
| 21 |
+
if "messages" not in st.session_state: # Stores the messages of the assistant
|
| 22 |
+
st.session_state.messages = []
|
| 23 |
+
|
| 24 |
+
if "retry_error" not in st.session_state: # Used for error handling
|
| 25 |
+
st.session_state.retry_error = 0
|
| 26 |
+
|
| 27 |
+
st.set_page_config(page_title="Manuales de Procedimientos de SCHT")
|
| 28 |
+
st.sidebar.title("Manuales de Procedimientos de SCHT")
|
| 29 |
+
st.sidebar.divider()
|
| 30 |
+
st.sidebar.markdown("Versi贸n actual: 0.0.1")
|
| 31 |
+
st.sidebar.markdown("Usando el API de gpt-4-1106-preview")
|
| 32 |
+
st.sidebar.markdown("### Incluye los siguientes manuales:")
|
| 33 |
+
st.sidebar.markdown("GAF-PROC-001 Procedimiento de Compras y Evaluacion de Proveedores")
|
| 34 |
+
st.sidebar.markdown("GAF-PROC-002 Procedimiento Reclutamiento y Seleccion de Personal")
|
| 35 |
+
st.sidebar.markdown("GAF-PROC-003 Procedimiento de Contratacion de Personal")
|
| 36 |
+
st.sidebar.markdown("GAF-PROC-004 Procedimiento Facturaci贸n y Cobranzas")
|
| 37 |
+
st.sidebar.markdown("GPD-PROC-001 Promocion y Desarrollo")
|
| 38 |
+
st.sidebar.markdown("GPR-PROC-001 Inicio del Proyecto")
|
| 39 |
+
|
| 40 |
+
st.sidebar.markdown(st.session_state.session_id)
|
| 41 |
+
st.sidebar.divider()
|
| 42 |
+
|
| 43 |
+
if "assistant" not in st.session_state:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
st.session_state.assistant = openai.beta.assistants.retrieve(assistant_id='asst_ZNKKXJLxzvwX7zVo0cVR1USv')
|
| 46 |
+
|
| 47 |
+
# Create a new thread for this session
|
| 48 |
+
st.session_state.thread = client.beta.threads.create(
|
| 49 |
+
metadata={
|
| 50 |
+
'session_id': st.session_state.session_id,
|
| 51 |
+
}
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# Refresh Session Button
|
| 55 |
+
if st.button("Reiniciar (Se borrar谩n los mensajes anteriores)"):
|
| 56 |
+
st.session_state.session_id = str(uuid.uuid4()) # Reset session ID
|
| 57 |
+
st.session_state.messages = [] # Reset messages
|
| 58 |
+
st.session_state.run = {"status": None} # Reset run state
|
| 59 |
+
st.session_state.retry_error = 0 # Reset error count
|
| 60 |
+
# You might also need to reset or recreate the assistant and thread here
|
| 61 |
+
st.rerun()
|
| 62 |
+
|
| 63 |
+
# If the run is completed, display the messages
|
| 64 |
+
elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
|
| 65 |
+
# Retrieve the list of messages
|
| 66 |
+
st.session_state.messages = client.beta.threads.messages.list(
|
| 67 |
+
thread_id=st.session_state.thread.id
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# Display messages
|
| 71 |
+
for message in reversed(st.session_state.messages.data):
|
| 72 |
+
if message.role in ["user", "assistant"]:
|
| 73 |
+
with st.chat_message(message.role):
|
| 74 |
+
for content_part in message.content:
|
| 75 |
+
message_text = content_part.text.value
|
| 76 |
+
st.markdown(message_text)
|
| 77 |
+
|
| 78 |
+
if prompt := st.chat_input("Escribe tu consulta sobre los procedimientos de SCHT"):
|
| 79 |
+
with st.chat_message('user'):
|
| 80 |
+
st.write(prompt)
|
| 81 |
+
|
| 82 |
+
# Add message to the thread
|
| 83 |
+
st.session_state.messages = client.beta.threads.messages.create(
|
| 84 |
+
thread_id=st.session_state.thread.id,
|
| 85 |
+
role="user",
|
| 86 |
+
content=prompt
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
# Do a run to process the messages in the thread
|
| 90 |
+
st.session_state.run = client.beta.threads.runs.create(
|
| 91 |
+
thread_id=st.session_state.thread.id,
|
| 92 |
+
assistant_id=st.session_state.assistant.id,
|
| 93 |
+
)
|
| 94 |
+
if st.session_state.retry_error < 3:
|
| 95 |
+
time.sleep(3) # Wait 1 second before checking run status
|
| 96 |
st.rerun()
|
| 97 |
+
|
| 98 |
+
# Check if 'run' object has 'status' attribute
|
| 99 |
+
if hasattr(st.session_state.run, 'status'):
|
| 100 |
+
# Handle the 'running' status
|
| 101 |
+
if st.session_state.run.status == "running":
|
| 102 |
+
with st.chat_message('assistant'):
|
| 103 |
+
st.write("Pensando ......")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
if st.session_state.retry_error < 3:
|
| 105 |
+
time.sleep(1) # Short delay to prevent immediate rerun, adjust as needed
|
| 106 |
st.rerun()
|
| 107 |
+
|
| 108 |
+
# Handle the 'failed' status
|
| 109 |
+
elif st.session_state.run.status == "failed":
|
| 110 |
+
st.session_state.retry_error += 1
|
| 111 |
+
with st.chat_message('assistant'):
|
|
|
|
|
|
|
| 112 |
if st.session_state.retry_error < 3:
|
| 113 |
+
st.write("Hubo un error, probando de nuevo ......")
|
| 114 |
+
time.sleep(3) # Longer delay before retrying
|
| 115 |
st.rerun()
|
| 116 |
+
else:
|
| 117 |
+
st.error("ERROR: La p谩gina de Open AI puede estar recibiendo demasiados pedidos, prueba en unos momentos ......")
|
| 118 |
+
|
| 119 |
+
# Handle any status that is not 'completed'
|
| 120 |
+
elif st.session_state.run.status != "completed":
|
| 121 |
+
# Attempt to retrieve the run again, possibly redundant if there's no other status but 'running' or 'failed'
|
| 122 |
+
st.session_state.run = client.beta.threads.runs.retrieve(
|
| 123 |
+
thread_id=st.session_state.thread.id,
|
| 124 |
+
run_id=st.session_state.run.id,
|
| 125 |
+
)
|
| 126 |
+
if st.session_state.retry_error < 3:
|
| 127 |
+
time.sleep(3)
|
| 128 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|