TheBobBob commited on
Commit
3563063
·
verified ·
1 Parent(s): a831da4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -26
app.py CHANGED
@@ -201,15 +201,23 @@ def generate_response(db, query_text, previous_context):
201
  filename="unsloth.BF16.gguf",
202
  )
203
 
204
- # Directly get the output without streaming
205
- output = llm(
206
  prompt_template,
 
207
  temperature=0.1,
208
  top_p=0.9,
209
  top_k=20
210
  )
211
 
212
- full_response = output["choices"][0]["text"]
 
 
 
 
 
 
 
 
213
 
214
  return full_response
215
 
@@ -219,10 +227,6 @@ def streamlit_app():
219
  # Initialize db in session state if not already present
220
  if "db" not in st.session_state:
221
  st.session_state.db = None
222
- if "messages" not in st.session_state:
223
- st.session_state.messages = []
224
- if "previous_context" not in st.session_state:
225
- st.session_state.previous_context = ""
226
 
227
  # Search query input
228
  search_str = st.text_input("Enter search query:")
@@ -255,30 +259,35 @@ def streamlit_app():
255
 
256
  if final_items:
257
  st.session_state.db = create_vector_db(final_items)
258
- st.success("Database initialized and models analyzed!")
259
  else:
260
- st.warning("No valid models found for the selected IDs.")
261
 
262
- # Chat interface
263
- query = st.text_area("Chat with the model:", value="", height=150)
264
-
265
- if query:
266
- # Update messages with user query
267
- st.session_state.messages.append({"role": "user", "content": query})
268
-
269
- # Generate response based on previous context and new query
270
- response = generate_response(st.session_state.db, query, st.session_state.previous_context)
271
- st.session_state.messages.append({"role": "assistant", "content": response})
272
-
273
- # Update previous context with the latest response
274
- st.session_state.previous_context = response
275
 
276
- # Display conversation history
277
  for message in st.session_state.messages:
278
- if message["role"] == "user":
279
- st.write(f"**You:** {message['content']}")
 
 
 
 
 
 
 
 
280
  else:
281
- st.write(f"**Assistant:** {message['content']}")
 
 
 
282
 
283
  if __name__ == "__main__":
284
  streamlit_app()
 
201
  filename="unsloth.BF16.gguf",
202
  )
203
 
204
+ output_stream = llm(
 
205
  prompt_template,
206
+ stream=True,
207
  temperature=0.1,
208
  top_p=0.9,
209
  top_k=20
210
  )
211
 
212
+ full_response = ""
213
+
214
+ response_placeholder = st.empty()
215
+
216
+ for token in output_stream:
217
+ # Extract the text from the token
218
+ token_text = token.get("choices", [{}])[0].get("text", "")
219
+ full_response += token_text
220
+ response_placeholder.text(full_response) # Print token output in real-time
221
 
222
  return full_response
223
 
 
227
  # Initialize db in session state if not already present
228
  if "db" not in st.session_state:
229
  st.session_state.db = None
 
 
 
 
230
 
231
  # Search query input
232
  search_str = st.text_input("Enter search query:")
 
259
 
260
  if final_items:
261
  st.session_state.db = create_vector_db(final_items)
262
+ st.write("Models have been processed and added to the database.")
263
  else:
264
+ st.error("No items found in the models. Check if the Antimony files were generated correctly.")
265
 
266
+ # Avoid caching the database initialization, or ensure it's properly updated.
267
+ @st.cache_resource
268
+ def get_messages():
269
+ if "messages" not in st.session_state:
270
+ st.session_state.messages = []
271
+ return st.session_state.messages
272
+
273
+ st.session_state.messages = get_messages()
 
 
 
 
 
274
 
 
275
  for message in st.session_state.messages:
276
+ with st.chat_message(message["role"]):
277
+ st.markdown(message["content"])
278
+
279
+ # Chat input section
280
+ if prompt := st.chat_input("Ask a question about the models:"):
281
+ st.chat_message("user").markdown(prompt)
282
+ st.session_state.messages.append({"role": "user", "content": prompt})
283
+
284
+ if st.session_state.db is None:
285
+ st.error("Database is not initialized. Please process the models first.")
286
  else:
287
+ response = generate_response(st.session_state.db, prompt, st.session_state.messages)
288
+
289
+ st.chat_message("assistant").markdown(response) # Directly display the final response
290
+ st.session_state.messages.append({"role": "assistant", "content": response})
291
 
292
  if __name__ == "__main__":
293
  streamlit_app()