IAMTFRMZA commited on
Commit
033ddfe
·
verified ·
1 Parent(s): a4a3203

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -9
app.py CHANGED
@@ -12,10 +12,12 @@ st.caption("Chat with an Ai Assistant on your Documents")
12
  with st.sidebar:
13
  OPENAI_API_KEY = st.text_input("Enter your C2 Group of Technologies Access Key", type="password")
14
 
 
15
  if not OPENAI_API_KEY:
16
  st.error("Please enter your C2 Group of Technologies Access Key to continue.")
17
  st.stop()
18
 
 
19
  client = OpenAI(api_key=OPENAI_API_KEY)
20
  ASSISTANT_ID = "asst_PJjxQftfz2IJUUMvnldK58lB"
21
 
@@ -31,37 +33,38 @@ if st.button("Clear Chat", use_container_width=True):
31
  st.session_state.thread_id = None
32
  st.rerun()
33
 
34
- # Display history
35
  for message in st.session_state.messages:
36
  role, content = message["role"], message["content"]
37
  st.chat_message(role).write(content)
38
 
39
- # Handle new input
40
  if prompt := st.chat_input():
41
  st.session_state.messages.append({"role": "user", "content": prompt})
42
  st.chat_message("user").write(prompt)
43
 
44
  try:
 
45
  if st.session_state.thread_id is None:
46
  thread = client.beta.threads.create()
47
  st.session_state.thread_id = thread.id
48
 
49
  thread_id = st.session_state.thread_id
50
 
51
- # Send user input to OpenAI
52
  client.beta.threads.messages.create(
53
  thread_id=thread_id,
54
  role="user",
55
  content=prompt
56
  )
57
 
58
- # Start assistant run
59
  run = client.beta.threads.runs.create(
60
  thread_id=thread_id,
61
  assistant_id=ASSISTANT_ID
62
  )
63
 
64
- # Wait until complete
65
  while True:
66
  run_status = client.beta.threads.runs.retrieve(
67
  thread_id=thread_id,
@@ -71,7 +74,7 @@ if prompt := st.chat_input():
71
  break
72
  time.sleep(1)
73
 
74
- # Get the latest assistant message
75
  messages = client.beta.threads.messages.list(thread_id=thread_id)
76
  assistant_message = None
77
  for message in reversed(messages.data):
@@ -79,13 +82,13 @@ if prompt := st.chat_input():
79
  assistant_message = message.content[0].text.value
80
  break
81
 
82
- # Display response text
83
  st.chat_message("assistant").write(assistant_message)
84
  st.session_state.messages.append({"role": "assistant", "content": assistant_message})
85
 
86
- # Check if assistant already gave a full image URL
87
  if "![Page Image](" not in assistant_message:
88
- # Try to extract the referenced page number
89
  page_match = re.search(r'Page (\d+)', assistant_message)
90
  if page_match:
91
  page_number = page_match.group(1).zfill(3)
 
12
  with st.sidebar:
13
  OPENAI_API_KEY = st.text_input("Enter your C2 Group of Technologies Access Key", type="password")
14
 
15
+ # Exit if no key provided
16
  if not OPENAI_API_KEY:
17
  st.error("Please enter your C2 Group of Technologies Access Key to continue.")
18
  st.stop()
19
 
20
+ # Initialize OpenAI client
21
  client = OpenAI(api_key=OPENAI_API_KEY)
22
  ASSISTANT_ID = "asst_PJjxQftfz2IJUUMvnldK58lB"
23
 
 
33
  st.session_state.thread_id = None
34
  st.rerun()
35
 
36
+ # Show message history
37
  for message in st.session_state.messages:
38
  role, content = message["role"], message["content"]
39
  st.chat_message(role).write(content)
40
 
41
+ # Handle new user message
42
  if prompt := st.chat_input():
43
  st.session_state.messages.append({"role": "user", "content": prompt})
44
  st.chat_message("user").write(prompt)
45
 
46
  try:
47
+ # Create a new thread if none exists
48
  if st.session_state.thread_id is None:
49
  thread = client.beta.threads.create()
50
  st.session_state.thread_id = thread.id
51
 
52
  thread_id = st.session_state.thread_id
53
 
54
+ # Send user prompt to assistant
55
  client.beta.threads.messages.create(
56
  thread_id=thread_id,
57
  role="user",
58
  content=prompt
59
  )
60
 
61
+ # Run assistant
62
  run = client.beta.threads.runs.create(
63
  thread_id=thread_id,
64
  assistant_id=ASSISTANT_ID
65
  )
66
 
67
+ # Wait until completion
68
  while True:
69
  run_status = client.beta.threads.runs.retrieve(
70
  thread_id=thread_id,
 
74
  break
75
  time.sleep(1)
76
 
77
+ # Retrieve assistant message
78
  messages = client.beta.threads.messages.list(thread_id=thread_id)
79
  assistant_message = None
80
  for message in reversed(messages.data):
 
82
  assistant_message = message.content[0].text.value
83
  break
84
 
85
+ # Display assistant message
86
  st.chat_message("assistant").write(assistant_message)
87
  st.session_state.messages.append({"role": "assistant", "content": assistant_message})
88
 
89
+ # Inject page image if assistant didn't include it
90
  if "![Page Image](" not in assistant_message:
91
+ # Find the page number
92
  page_match = re.search(r'Page (\d+)', assistant_message)
93
  if page_match:
94
  page_number = page_match.group(1).zfill(3)