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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -15
app.py CHANGED
@@ -12,7 +12,6 @@ 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
- # Stop execution if no API 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()
@@ -32,38 +31,37 @@ if st.button("Clear Chat", use_container_width=True):
32
  st.session_state.thread_id = None
33
  st.rerun()
34
 
35
- # Display message history
36
  for message in st.session_state.messages:
37
  role, content = message["role"], message["content"]
38
  st.chat_message(role).write(content)
39
 
40
- # Handle new user input
41
  if prompt := st.chat_input():
42
  st.session_state.messages.append({"role": "user", "content": prompt})
43
  st.chat_message("user").write(prompt)
44
 
45
  try:
46
- # Create thread if not yet created
47
  if st.session_state.thread_id is None:
48
  thread = client.beta.threads.create()
49
  st.session_state.thread_id = thread.id
50
 
51
  thread_id = st.session_state.thread_id
52
 
53
- # Send message
54
  client.beta.threads.messages.create(
55
  thread_id=thread_id,
56
  role="user",
57
  content=prompt
58
  )
59
 
60
- # Run assistant
61
  run = client.beta.threads.runs.create(
62
  thread_id=thread_id,
63
  assistant_id=ASSISTANT_ID
64
  )
65
 
66
- # Poll until complete
67
  while True:
68
  run_status = client.beta.threads.runs.retrieve(
69
  thread_id=thread_id,
@@ -73,7 +71,7 @@ if prompt := st.chat_input():
73
  break
74
  time.sleep(1)
75
 
76
- # Get assistant response
77
  messages = client.beta.threads.messages.list(thread_id=thread_id)
78
  assistant_message = None
79
  for message in reversed(messages.data):
@@ -81,16 +79,18 @@ if prompt := st.chat_input():
81
  assistant_message = message.content[0].text.value
82
  break
83
 
84
- # Display assistant message
85
  st.chat_message("assistant").write(assistant_message)
86
  st.session_state.messages.append({"role": "assistant", "content": assistant_message})
87
 
88
- # 🔍 Detect and display GitHub-hosted image
89
- page_match = re.search(r'Page (\d+)', assistant_message)
90
- if page_match:
91
- page_number = page_match.group(1).zfill(3)
92
- image_url = f"https://raw.githubusercontent.com/AndrewLORTech/surgical-pathology-manual/main/51940670-Manual-of-Surgical-Pathology-Third-Edition_1_page_{page_number}.png"
93
- st.image(image_url, caption=f"Page {int(page_number)}", use_container_width=True)
 
 
94
 
95
  except Exception as e:
96
  st.error(f"Error: {str(e)}")
 
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()
 
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
  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
  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)
92
+ image_url = f"https://raw.githubusercontent.com/AndrewLORTech/surgical-pathology-manual/main/51940670-Manual-of-Surgical-Pathology-Third-Edition_1_page_{page_number}.png"
93
+ st.image(image_url, caption=f"Page {int(page_number)}", use_container_width=True)
94
 
95
  except Exception as e:
96
  st.error(f"Error: {str(e)}")