Update app.py
Browse files
app.py
CHANGED
@@ -5,6 +5,7 @@ import os
|
|
5 |
import uuid
|
6 |
from datetime import datetime
|
7 |
import time
|
|
|
8 |
|
9 |
# Load environment variables
|
10 |
load_dotenv()
|
@@ -400,7 +401,7 @@ if prompt:
|
|
400 |
if len(messages) == 0:
|
401 |
# Check if the first prompt is just a greeting
|
402 |
greetings = ["hello", "hi", "hey", "greetings", "good morning", "good afternoon", "good evening", "howdy"]
|
403 |
-
is_greeting = any(greeting in prompt.lower() for greeting in greetings)
|
404 |
|
405 |
# Keep "New chat" if it's a greeting, otherwise change immediately after first response
|
406 |
if not is_greeting:
|
@@ -408,7 +409,7 @@ if prompt:
|
|
408 |
st.session_state.conversations[current_id]["should_update_title"] = True
|
409 |
|
410 |
# Update title after 2nd or 3rd user message if still has default title
|
411 |
-
elif len(messages)
|
412 |
# Create a short 3-4 word summary
|
413 |
words = prompt.split()
|
414 |
if len(words) > 4:
|
@@ -449,7 +450,13 @@ if prompt:
|
|
449 |
|
450 |
# Final display without cursor
|
451 |
if full_response:
|
452 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
453 |
else:
|
454 |
message_placeholder.markdown("No response received from the model.")
|
455 |
|
@@ -458,21 +465,29 @@ if prompt:
|
|
458 |
|
459 |
# Check if we should update the title after first response
|
460 |
if len(messages) == 2 and st.session_state.conversations[current_id].get("should_update_title", False):
|
461 |
-
# Extract a short title from the
|
462 |
-
|
463 |
-
words =
|
464 |
-
|
465 |
-
|
|
|
|
|
|
|
|
|
|
|
466 |
st.session_state.conversations[current_id]["title"] = new_title[:30]
|
467 |
st.session_state.conversations[current_id]["should_update_title"] = False
|
|
|
|
|
|
|
468 |
|
469 |
except Exception as e:
|
470 |
error_msg = f"Error: {str(e)}"
|
471 |
message_placeholder.markdown(error_msg)
|
472 |
messages.append({"role": "assistant", "content": error_msg})
|
473 |
|
474 |
-
#
|
475 |
-
|
476 |
|
477 |
# JavaScript for Auto-Scroll - Improved version
|
478 |
scroll_script = """
|
|
|
5 |
import uuid
|
6 |
from datetime import datetime
|
7 |
import time
|
8 |
+
import re
|
9 |
|
10 |
# Load environment variables
|
11 |
load_dotenv()
|
|
|
401 |
if len(messages) == 0:
|
402 |
# Check if the first prompt is just a greeting
|
403 |
greetings = ["hello", "hi", "hey", "greetings", "good morning", "good afternoon", "good evening", "howdy"]
|
404 |
+
is_greeting = any(greeting.lower() in prompt.lower() for greeting in greetings) and len(prompt.split()) < 5
|
405 |
|
406 |
# Keep "New chat" if it's a greeting, otherwise change immediately after first response
|
407 |
if not is_greeting:
|
|
|
409 |
st.session_state.conversations[current_id]["should_update_title"] = True
|
410 |
|
411 |
# Update title after 2nd or 3rd user message if still has default title
|
412 |
+
elif 1 <= len(messages) <= 5 and st.session_state.conversations[current_id]["title"].startswith("New chat"):
|
413 |
# Create a short 3-4 word summary
|
414 |
words = prompt.split()
|
415 |
if len(words) > 4:
|
|
|
450 |
|
451 |
# Final display without cursor
|
452 |
if full_response:
|
453 |
+
# Clean up any excessive dash lines or formatting issues
|
454 |
+
cleaned_response = full_response
|
455 |
+
# Replace multiple consecutive dash lines with a single line
|
456 |
+
cleaned_response = re.sub(r'β{5,}', 'βββββ', cleaned_response)
|
457 |
+
# Remove excessive newlines
|
458 |
+
cleaned_response = re.sub(r'\n{3,}', '\n\n', cleaned_response)
|
459 |
+
message_placeholder.markdown(cleaned_response)
|
460 |
else:
|
461 |
message_placeholder.markdown("No response received from the model.")
|
462 |
|
|
|
465 |
|
466 |
# Check if we should update the title after first response
|
467 |
if len(messages) == 2 and st.session_state.conversations[current_id].get("should_update_title", False):
|
468 |
+
# Extract a short title from the first user prompt
|
469 |
+
user_prompt = messages[0]["content"]
|
470 |
+
words = user_prompt.split()
|
471 |
+
if len(words) >= 4:
|
472 |
+
# Use first 3-4 meaningful words for title
|
473 |
+
title_words = words[:4]
|
474 |
+
new_title = " ".join(title_words)
|
475 |
+
else:
|
476 |
+
new_title = user_prompt
|
477 |
+
|
478 |
st.session_state.conversations[current_id]["title"] = new_title[:30]
|
479 |
st.session_state.conversations[current_id]["should_update_title"] = False
|
480 |
+
|
481 |
+
# Force a refresh of the UI after each message to ensure title updates
|
482 |
+
st.experimental_rerun()
|
483 |
|
484 |
except Exception as e:
|
485 |
error_msg = f"Error: {str(e)}"
|
486 |
message_placeholder.markdown(error_msg)
|
487 |
messages.append({"role": "assistant", "content": error_msg})
|
488 |
|
489 |
+
# We will force rerun after message processing completes in the assistant block above
|
490 |
+
# This allows us to update the UI after title changes
|
491 |
|
492 |
# JavaScript for Auto-Scroll - Improved version
|
493 |
scroll_script = """
|