IAMTFRMZA commited on
Commit
74c6fff
Β·
verified Β·
1 Parent(s): eeb4027

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -24
app.py CHANGED
@@ -41,58 +41,58 @@ if st.sidebar.button("πŸ”„ Clear Chat"):
41
 
42
  show_image = st.sidebar.checkbox("πŸ“– Show Document Image", value=True)
43
 
44
- # ------------------ Split Layout ------------------
45
  col1, col2 = st.columns([1, 2])
46
 
47
- # ------------------ Image Panel (Left) ------------------
48
  with col1:
49
  if show_image and st.session_state.image_url:
50
  st.image(st.session_state.image_url, caption="πŸ“‘ Extracted Page", use_container_width=True)
51
  st.session_state.image_updated = False
52
 
53
- # ------------------ Chat Panel (Right) ------------------
54
  with col2:
55
- # Pair user + assistant messages
 
 
 
56
  paired_messages = []
57
  buffer = []
58
-
59
  for msg in st.session_state.messages:
60
  buffer.append(msg)
61
  if msg["role"] == "assistant" and len(buffer) == 2:
62
  paired_messages.append(buffer.copy())
63
  buffer.clear()
64
-
65
- # If last message is from user and no assistant reply yet
66
  if buffer:
67
  paired_messages.append(buffer.copy())
68
 
69
- # Show pairs in reverse order (latest on top)
70
- for pair in reversed(paired_messages):
71
- for msg in pair:
72
- st.chat_message(msg["role"]).write(msg["content"])
 
 
73
 
74
- # ------------------ Chat Input Handling ------------------
75
- if prompt := st.chat_input("Type your question about the document..."):
76
  st.session_state.messages.append({"role": "user", "content": prompt})
77
 
78
  try:
79
- # Create thread if not exists
80
  if st.session_state.thread_id is None:
81
  thread = client.beta.threads.create()
82
  st.session_state.thread_id = thread.id
83
 
84
- thread_id = st.session_state.thread_id
85
-
86
  # Send user message to assistant
87
  client.beta.threads.messages.create(
88
- thread_id=thread_id,
89
  role="user",
90
  content=prompt
91
  )
92
 
93
- # Run assistant
94
  run = client.beta.threads.runs.create(
95
- thread_id=thread_id,
96
  assistant_id=ASSISTANT_ID
97
  )
98
 
@@ -100,15 +100,15 @@ with col2:
100
  with st.spinner("Assistant is thinking..."):
101
  while True:
102
  run_status = client.beta.threads.runs.retrieve(
103
- thread_id=thread_id,
104
  run_id=run.id
105
  )
106
  if run_status.status == "completed":
107
  break
108
  time.sleep(1)
109
 
110
- # Retrieve assistant response
111
- messages = client.beta.threads.messages.list(thread_id=thread_id)
112
  assistant_message = None
113
  for message in reversed(messages.data):
114
  if message.role == "assistant":
@@ -117,7 +117,7 @@ with col2:
117
 
118
  st.session_state.messages.append({"role": "assistant", "content": assistant_message})
119
 
120
- # Extract image URL if present
121
  image_match = re.search(
122
  r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
123
  assistant_message
@@ -126,7 +126,7 @@ with col2:
126
  st.session_state.image_url = image_match.group(0)
127
  st.session_state.image_updated = True
128
 
129
- st.rerun()
130
 
131
  except Exception as e:
132
  st.error(f"❌ Error: {str(e)}")
 
41
 
42
  show_image = st.sidebar.checkbox("πŸ“– Show Document Image", value=True)
43
 
44
+ # ------------------ Layout: Image (Left) and Chat (Right) ------------------
45
  col1, col2 = st.columns([1, 2])
46
 
47
+ # ------------------ Left Panel: Document Image ------------------
48
  with col1:
49
  if show_image and st.session_state.image_url:
50
  st.image(st.session_state.image_url, caption="πŸ“‘ Extracted Page", use_container_width=True)
51
  st.session_state.image_updated = False
52
 
53
+ # ------------------ Right Panel: Chat Interface ------------------
54
  with col2:
55
+ # -- Chat Input (Always at the Top)
56
+ prompt = st.chat_input("Type your question about the document...")
57
+
58
+ # -- Pair messages (user + assistant) for display
59
  paired_messages = []
60
  buffer = []
 
61
  for msg in st.session_state.messages:
62
  buffer.append(msg)
63
  if msg["role"] == "assistant" and len(buffer) == 2:
64
  paired_messages.append(buffer.copy())
65
  buffer.clear()
 
 
66
  if buffer:
67
  paired_messages.append(buffer.copy())
68
 
69
+ # -- Display chat history (latest first, older scroll down)
70
+ with st.container():
71
+ for pair in reversed(paired_messages):
72
+ for msg in pair:
73
+ with st.chat_message(msg["role"]):
74
+ st.write(msg["content"])
75
 
76
+ # -- Process new prompt if entered
77
+ if prompt:
78
  st.session_state.messages.append({"role": "user", "content": prompt})
79
 
80
  try:
81
+ # Initialize thread if not already done
82
  if st.session_state.thread_id is None:
83
  thread = client.beta.threads.create()
84
  st.session_state.thread_id = thread.id
85
 
 
 
86
  # Send user message to assistant
87
  client.beta.threads.messages.create(
88
+ thread_id=st.session_state.thread_id,
89
  role="user",
90
  content=prompt
91
  )
92
 
93
+ # Trigger assistant run
94
  run = client.beta.threads.runs.create(
95
+ thread_id=st.session_state.thread_id,
96
  assistant_id=ASSISTANT_ID
97
  )
98
 
 
100
  with st.spinner("Assistant is thinking..."):
101
  while True:
102
  run_status = client.beta.threads.runs.retrieve(
103
+ thread_id=st.session_state.thread_id,
104
  run_id=run.id
105
  )
106
  if run_status.status == "completed":
107
  break
108
  time.sleep(1)
109
 
110
+ # Retrieve latest assistant message
111
+ messages = client.beta.threads.messages.list(thread_id=st.session_state.thread_id)
112
  assistant_message = None
113
  for message in reversed(messages.data):
114
  if message.role == "assistant":
 
117
 
118
  st.session_state.messages.append({"role": "assistant", "content": assistant_message})
119
 
120
+ # -- Extract GitHub-hosted image URL if present
121
  image_match = re.search(
122
  r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
123
  assistant_message
 
126
  st.session_state.image_url = image_match.group(0)
127
  st.session_state.image_updated = True
128
 
129
+ st.rerun() # Rerun to show updated image + message at top
130
 
131
  except Exception as e:
132
  st.error(f"❌ Error: {str(e)}")