IAMTFRMZA commited on
Commit
809b532
Β·
verified Β·
1 Parent(s): 9c9251a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -8
app.py CHANGED
@@ -15,7 +15,7 @@ ASSISTANT_ID = os.environ.get("ASSISTANT_ID")
15
 
16
  # ------------------ Error Handling for Missing Secrets ------------------
17
  if not OPENAI_API_KEY or not ASSISTANT_ID:
18
- st.error("Missing secrets. Please ensure both OPENAI_API_KEY and ASSISTANT_ID are set in your Hugging Face Space secrets.")
19
  st.stop()
20
 
21
  client = OpenAI(api_key=OPENAI_API_KEY)
@@ -52,10 +52,7 @@ with col1:
52
 
53
  # ------------------ Chat Panel (Right) ------------------
54
  with col2:
55
- for message in st.session_state.messages:
56
- role, content = message["role"], message["content"]
57
- st.chat_message(role).write(content)
58
-
59
  if prompt := st.chat_input("Type your question about the document..."):
60
  st.session_state.messages.append({"role": "user", "content": prompt})
61
  st.chat_message("user").write(prompt)
@@ -82,7 +79,7 @@ with col2:
82
  )
83
 
84
  # Wait for assistant response
85
- with st.spinner("Assistant is thinking..."):
86
  while True:
87
  run_status = client.beta.threads.runs.retrieve(
88
  thread_id=thread_id,
@@ -103,7 +100,7 @@ with col2:
103
  st.chat_message("assistant").write(assistant_message)
104
  st.session_state.messages.append({"role": "assistant", "content": assistant_message})
105
 
106
- # Extract GitHub image from response if available
107
  image_match = re.search(
108
  r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
109
  assistant_message
@@ -111,7 +108,13 @@ with col2:
111
  if image_match:
112
  st.session_state.image_url = image_match.group(0)
113
  st.session_state.image_updated = True
114
- st.rerun() # Trigger rerun to refresh image display
115
 
116
  except Exception as e:
117
  st.error(f"❌ Error: {str(e)}")
 
 
 
 
 
 
 
15
 
16
  # ------------------ Error Handling for Missing Secrets ------------------
17
  if not OPENAI_API_KEY or not ASSISTANT_ID:
18
+ st.error("❌ Missing secrets. Please ensure both OPENAI_API_KEY and ASSISTANT_ID are set in your Hugging Face Space secrets.")
19
  st.stop()
20
 
21
  client = OpenAI(api_key=OPENAI_API_KEY)
 
52
 
53
  # ------------------ Chat Panel (Right) ------------------
54
  with col2:
55
+ # πŸ”Ό Chat input stays at the top
 
 
 
56
  if prompt := st.chat_input("Type your question about the document..."):
57
  st.session_state.messages.append({"role": "user", "content": prompt})
58
  st.chat_message("user").write(prompt)
 
79
  )
80
 
81
  # Wait for assistant response
82
+ with st.spinner("πŸ€– Assistant is thinking..."):
83
  while True:
84
  run_status = client.beta.threads.runs.retrieve(
85
  thread_id=thread_id,
 
100
  st.chat_message("assistant").write(assistant_message)
101
  st.session_state.messages.append({"role": "assistant", "content": assistant_message})
102
 
103
+ # Extract GitHub image URL if present
104
  image_match = re.search(
105
  r'https://raw\.githubusercontent\.com/AndrewLORTech/surgical-pathology-manual/main/[\w\-/]*\.png',
106
  assistant_message
 
108
  if image_match:
109
  st.session_state.image_url = image_match.group(0)
110
  st.session_state.image_updated = True
111
+ st.rerun()
112
 
113
  except Exception as e:
114
  st.error(f"❌ Error: {str(e)}")
115
+
116
+ # πŸ”½ Show previous messages below the input
117
+ for message in reversed(st.session_state.messages):
118
+ role, content = message["role"], message["content"]
119
+ st.chat_message(role).write(content)
120
+