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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -21
app.py CHANGED
@@ -3,55 +3,51 @@ from openai import OpenAI
3
  import time
4
  import re
5
 
6
- # Streamlit setup
7
  st.set_page_config(page_title="documentaitest")
8
  st.title("documentaitest")
9
  st.caption("Chat with an Ai Assistant on your Documents")
10
 
11
- # Sidebar for API key input
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
 
24
- # Initialize session state
25
  if "messages" not in st.session_state:
26
  st.session_state.messages = []
27
  if "thread_id" not in st.session_state:
28
  st.session_state.thread_id = None
29
 
30
- # Button to clear chat
31
  if st.button("Clear Chat", use_container_width=True):
32
  st.session_state.messages = []
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",
@@ -64,7 +60,7 @@ if prompt := st.chat_input():
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,7 +70,7 @@ if prompt := st.chat_input():
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,18 +78,15 @@ if prompt := st.chat_input():
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)
95
- image_url = f"https://raw.githubusercontent.com/AndrewLORTech/surgical-pathology-manual/main/51940670-Manual-of-Surgical-Pathology-Third-Edition_1_page_{page_number}.png"
96
- st.image(image_url, caption=f"Page {int(page_number)}", use_container_width=True)
97
 
98
  except Exception as e:
99
  st.error(f"Error: {str(e)}")
 
3
  import time
4
  import re
5
 
 
6
  st.set_page_config(page_title="documentaitest")
7
  st.title("documentaitest")
8
  st.caption("Chat with an Ai Assistant on your Documents")
9
 
10
+ # Sidebar key entry
11
  with st.sidebar:
12
  OPENAI_API_KEY = st.text_input("Enter your C2 Group of Technologies Access Key", type="password")
13
 
 
14
  if not OPENAI_API_KEY:
15
  st.error("Please enter your C2 Group of Technologies Access Key to continue.")
16
  st.stop()
17
 
 
18
  client = OpenAI(api_key=OPENAI_API_KEY)
19
  ASSISTANT_ID = "asst_PJjxQftfz2IJUUMvnldK58lB"
20
 
21
+ # Session state setup
22
  if "messages" not in st.session_state:
23
  st.session_state.messages = []
24
  if "thread_id" not in st.session_state:
25
  st.session_state.thread_id = None
26
 
27
+ # Clear button
28
  if st.button("Clear Chat", use_container_width=True):
29
  st.session_state.messages = []
30
  st.session_state.thread_id = None
31
  st.rerun()
32
 
33
+ # Display prior messages
34
  for message in st.session_state.messages:
35
  role, content = message["role"], message["content"]
36
  st.chat_message(role).write(content)
37
 
38
+ # Handle input
39
  if prompt := st.chat_input():
40
  st.session_state.messages.append({"role": "user", "content": prompt})
41
  st.chat_message("user").write(prompt)
42
 
43
  try:
 
44
  if st.session_state.thread_id is None:
45
  thread = client.beta.threads.create()
46
  st.session_state.thread_id = thread.id
47
 
48
  thread_id = st.session_state.thread_id
49
 
50
+ # Send prompt to assistant
51
  client.beta.threads.messages.create(
52
  thread_id=thread_id,
53
  role="user",
 
60
  assistant_id=ASSISTANT_ID
61
  )
62
 
63
+ # Wait for assistant to finish
64
  while True:
65
  run_status = client.beta.threads.runs.retrieve(
66
  thread_id=thread_id,
 
70
  break
71
  time.sleep(1)
72
 
73
+ # Get assistant response
74
  messages = client.beta.threads.messages.list(thread_id=thread_id)
75
  assistant_message = None
76
  for message in reversed(messages.data):
 
78
  assistant_message = message.content[0].text.value
79
  break
80
 
81
+ # Display message
82
  st.chat_message("assistant").write(assistant_message)
83
  st.session_state.messages.append({"role": "assistant", "content": assistant_message})
84
 
85
+ # 🔍 Extract and display GitHub image if present
86
+ image_match = re.search(r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png', assistant_message)
87
+ if image_match:
88
+ image_url = image_match.group(0)
89
+ st.image(image_url, caption="Page Image", use_container_width=True)
 
 
 
90
 
91
  except Exception as e:
92
  st.error(f"Error: {str(e)}")