g-ur commited on
Commit
ef46818
1 Parent(s): 2255517

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -112
app.py CHANGED
@@ -6,126 +6,123 @@ import os
6
 
7
  from openai import OpenAI
8
 
 
9
 
10
- def main():
11
- st.set_page_config(page_title="Manuales de Procedimientos de SCHT")
12
 
13
- openai.api_key = os.environ['OPENAI_API_KEY']
14
 
15
- client = OpenAI()
 
16
 
17
- MODEL = "gpt-4-1106-preview"
 
18
 
19
- if "session_id" not in st.session_state: # Used to identify each session
20
- st.session_state.session_id = str(uuid.uuid4())
21
-
22
- if "run" not in st.session_state: # Stores the run state of the assistant
23
- st.session_state.run = {"status": None}
24
-
25
- if "messages" not in st.session_state: # Stores the messages of the assistant
26
- st.session_state.messages = []
27
-
28
- if "retry_error" not in st.session_state: # Used for error handling
29
- st.session_state.retry_error = 0
30
-
31
- st.sidebar.title("Manuales de Procedimientos de SCHT")
32
- st.sidebar.divider()
33
- st.sidebar.markdown("Versi贸n actual: 0.0.1")
34
- st.sidebar.markdown("Usando el API de gpt-4-1106-preview")
35
- st.sidebar.markdown("### Incluye los siguientes manuales:")
36
- st.sidebar.markdown("GAF-PROC-001 Procedimiento de Compras y Evaluacion de Proveedores")
37
- st.sidebar.markdown("GAF-PROC-002 Procedimiento Reclutamiento y Seleccion de Personal")
38
- st.sidebar.markdown("GAF-PROC-003 Procedimiento de Contratacion de Personal")
39
- st.sidebar.markdown("GAF-PROC-004 Procedimiento Facturaci贸n y Cobranzas")
40
- st.sidebar.markdown("GPD-PROC-001 Promocion y Desarrollo")
41
- st.sidebar.markdown("GPR-PROC-001 Inicio del Proyecto")
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
- # Refresh Session Button
58
- if st.button("Reiniciar (Se borrar谩n los mensajes anteriores)"):
59
- st.session_state.session_id = str(uuid.uuid4()) # Reset session ID
60
- st.session_state.messages = [] # Reset messages
61
- st.session_state.run = {"status": None} # Reset run state
62
- st.session_state.retry_error = 0 # Reset error count
63
- # You might also need to reset or recreate the assistant and thread here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  st.rerun()
65
-
66
- # If the run is completed, display the messages
67
- elif hasattr(st.session_state.run, 'status') and st.session_state.run.status == "completed":
68
- # Retrieve the list of messages
69
- st.session_state.messages = client.beta.threads.messages.list(
70
- thread_id=st.session_state.thread.id
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(3) # Wait 1 second before checking run status
99
  st.rerun()
100
-
101
- # Check if 'run' object has 'status' attribute
102
- if hasattr(st.session_state.run, 'status'):
103
- # Handle the 'running' status
104
- if st.session_state.run.status == "running":
105
- with st.chat_message('assistant'):
106
- st.write("Pensando ......")
107
  if st.session_state.retry_error < 3:
108
- time.sleep(1) # Short delay to prevent immediate rerun, adjust as needed
 
109
  st.rerun()
110
-
111
- # Handle the 'failed' status
112
- elif st.session_state.run.status == "failed":
113
- st.session_state.retry_error += 1
114
- with st.chat_message('assistant'):
115
- if st.session_state.retry_error < 3:
116
- st.write("Hubo un error, probando de nuevo ......")
117
- time.sleep(3) # Longer delay before retrying
118
- st.rerun()
119
- else:
120
- st.error("ERROR: La p谩gina de Open AI puede estar recibiendo demasiados pedidos, prueba en unos momentos ......")
121
-
122
- # Handle any status that is not 'completed'
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()