Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -46,45 +46,43 @@ if "last_audio_path" not in st.session_state:
|
|
46 |
if "selected_voice" not in st.session_state:
|
47 |
st.session_state["selected_voice"] = "Jenny (US, Female)"
|
48 |
|
49 |
-
# ---
|
50 |
st.markdown("""
|
51 |
<style>
|
52 |
-
.block-container {padding-top:
|
53 |
header {visibility: hidden;}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; margin-bottom: 10px; }
|
55 |
.stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
|
56 |
.stChatMessage[data-testid="stChatMessage-assistant"] { background: #e3f2fd; color: #000000; }
|
57 |
-
.
|
58 |
-
|
59 |
-
width: 100vw;
|
60 |
-
margin: 0 auto;
|
61 |
-
text-align: center;
|
62 |
-
padding-top: 2.5em;
|
63 |
-
padding-bottom: 1em;
|
64 |
-
background: none;
|
65 |
-
z-index: 1001;
|
66 |
-
overflow: visible;
|
67 |
}
|
68 |
-
.
|
69 |
-
position:
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
box-shadow: 0 -2px 8px rgba(0,0,0,0.10);
|
74 |
-
padding: 1.2em 0.5em 0.8em 0.5em;
|
75 |
-
z-index: 9999;
|
76 |
}
|
77 |
-
.
|
78 |
-
.
|
79 |
-
.
|
80 |
</style>
|
81 |
""", unsafe_allow_html=True)
|
82 |
|
83 |
-
# ---
|
84 |
st.markdown("""
|
85 |
-
<div class="
|
86 |
-
<img src="https://lortechnologies.com/wp-content/uploads/2023/03/LOR-Online-Logo.svg"
|
87 |
-
<div style="font-size:
|
88 |
</div>
|
89 |
""", unsafe_allow_html=True)
|
90 |
|
@@ -112,6 +110,7 @@ with st.sidebar:
|
|
112 |
st.session_state["mute_voice"] = False
|
113 |
st.rerun()
|
114 |
|
|
|
115 |
def get_or_create_thread_id():
|
116 |
doc_ref = db.collection("users").document(user_id)
|
117 |
doc = doc_ref.get()
|
@@ -139,9 +138,9 @@ def clear_chat_history():
|
|
139 |
|
140 |
def display_chat_history():
|
141 |
messages = db.collection("users").document(user_id).collection("messages").order_by("timestamp").stream()
|
142 |
-
assistant_icon_html = "<img src='https://raw.githubusercontent.com/AndrewLORTech/lortechwebsite/main/lorain.jpg' width='
|
143 |
chat_msgs = []
|
144 |
-
for msg in list(messages)[::-1]:
|
145 |
data = msg.to_dict()
|
146 |
if data["role"] == "user":
|
147 |
chat_msgs.append(
|
@@ -151,8 +150,8 @@ def display_chat_history():
|
|
151 |
chat_msgs.append(
|
152 |
f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>LORAIN:</strong> {data['content']}</div>"
|
153 |
)
|
154 |
-
|
155 |
-
st.markdown(
|
156 |
|
157 |
# --- Edge TTS synth ---
|
158 |
async def edge_tts_synthesize(text, voice, user_id):
|
@@ -172,33 +171,32 @@ def synthesize_voice(text, voice_key, user_id):
|
|
172 |
st.session_state["last_voice"] = voice
|
173 |
return out_path
|
174 |
|
175 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
176 |
display_chat_history()
|
177 |
-
st.markdown('<div class="footer-placeholder"></div>', unsafe_allow_html=True)
|
178 |
|
179 |
-
# --- JS: auto-scroll to
|
180 |
st.markdown("""
|
181 |
<script>
|
182 |
window.onload = function() {
|
183 |
-
var anchor = document.getElementById("chat-
|
184 |
-
if(anchor){ anchor.scrollIntoView({ behavior: "smooth", block: "
|
185 |
};
|
186 |
window.setTimeout(function(){
|
187 |
-
var anchor = document.getElementById("chat-
|
188 |
-
if(anchor){ anchor.scrollIntoView({ behavior: "smooth", block: "
|
189 |
}, 200);
|
190 |
</script>
|
191 |
""", unsafe_allow_html=True)
|
192 |
|
193 |
-
# --- "Floating" Chat Input and Clear Chat button ---
|
194 |
-
with st.container():
|
195 |
-
st.markdown('<div class="footer-fakebar">', unsafe_allow_html=True)
|
196 |
-
col1, col2 = st.columns([10, 1])
|
197 |
-
user_input = col1.chat_input("Type your message here...")
|
198 |
-
if col2.button("🗑️", help="Clear Chat", key="clear-chat-bottom"):
|
199 |
-
clear_chat_history()
|
200 |
-
st.markdown('</div>', unsafe_allow_html=True)
|
201 |
-
|
202 |
if user_input:
|
203 |
# --- OpenAI Assistant Response ---
|
204 |
thread_id = get_or_create_thread_id()
|
|
|
46 |
if "selected_voice" not in st.session_state:
|
47 |
st.session_state["selected_voice"] = "Jenny (US, Female)"
|
48 |
|
49 |
+
# --- CSS for logo, chat, and input bar at top ---
|
50 |
st.markdown("""
|
51 |
<style>
|
52 |
+
.block-container {padding-top: 1rem;}
|
53 |
header {visibility: hidden;}
|
54 |
+
.logo-mini {
|
55 |
+
width: 75px !important;
|
56 |
+
margin: 0 auto 0.25em auto;
|
57 |
+
display: block;
|
58 |
+
}
|
59 |
+
.lor-brand-bar {
|
60 |
+
width: 100vw; text-align: center; background: none;
|
61 |
+
margin-bottom: 0.5em; margin-top: 0.1em;
|
62 |
+
}
|
63 |
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; margin-bottom: 10px; }
|
64 |
.stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
|
65 |
.stChatMessage[data-testid="stChatMessage-assistant"] { background: #e3f2fd; color: #000000; }
|
66 |
+
.chat-history-wrapper {
|
67 |
+
margin-top: 0.5em; padding-bottom: 2em; min-height: 60vh;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
}
|
69 |
+
.input-top-bar {
|
70 |
+
position: sticky; top: 0; z-index: 20;
|
71 |
+
background: #191b22; padding: 0.5em 0 0.25em 0;
|
72 |
+
display: flex; align-items: center; gap: 0.4em;
|
73 |
+
border-bottom: 1px solid #22232c;
|
|
|
|
|
|
|
74 |
}
|
75 |
+
.input-top-bar .element-container { flex: 1 1 auto; }
|
76 |
+
.input-top-bar input { font-size: 1.12em !important; }
|
77 |
+
.clear-chat-btn { background: none; border: none; font-size: 1.4em; color: #666; cursor: pointer; }
|
78 |
</style>
|
79 |
""", unsafe_allow_html=True)
|
80 |
|
81 |
+
# --- Top Branding, Mini Logo ---
|
82 |
st.markdown("""
|
83 |
+
<div class="lor-brand-bar">
|
84 |
+
<img src="https://lortechnologies.com/wp-content/uploads/2023/03/LOR-Online-Logo.svg" class="logo-mini" />
|
85 |
+
<div style="font-size: 13px; color: #888;">Powered by LOR Technologies</div>
|
86 |
</div>
|
87 |
""", unsafe_allow_html=True)
|
88 |
|
|
|
110 |
st.session_state["mute_voice"] = False
|
111 |
st.rerun()
|
112 |
|
113 |
+
# --- Firestore helpers ---
|
114 |
def get_or_create_thread_id():
|
115 |
doc_ref = db.collection("users").document(user_id)
|
116 |
doc = doc_ref.get()
|
|
|
138 |
|
139 |
def display_chat_history():
|
140 |
messages = db.collection("users").document(user_id).collection("messages").order_by("timestamp").stream()
|
141 |
+
assistant_icon_html = "<img src='https://raw.githubusercontent.com/AndrewLORTech/lortechwebsite/main/lorain.jpg' width='22' style='vertical-align:middle; border-radius:50%;'/>"
|
142 |
chat_msgs = []
|
143 |
+
for msg in list(messages)[::-1]: # Most recent first!
|
144 |
data = msg.to_dict()
|
145 |
if data["role"] == "user":
|
146 |
chat_msgs.append(
|
|
|
150 |
chat_msgs.append(
|
151 |
f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>LORAIN:</strong> {data['content']}</div>"
|
152 |
)
|
153 |
+
st.markdown('<div class="chat-history-wrapper">' + "".join(chat_msgs) + '</div>', unsafe_allow_html=True)
|
154 |
+
st.markdown('<div id="chat-top-anchor"></div>', unsafe_allow_html=True)
|
155 |
|
156 |
# --- Edge TTS synth ---
|
157 |
async def edge_tts_synthesize(text, voice, user_id):
|
|
|
171 |
st.session_state["last_voice"] = voice
|
172 |
return out_path
|
173 |
|
174 |
+
# --- INPUT AT THE TOP ---
|
175 |
+
with st.container():
|
176 |
+
st.markdown('<div class="input-top-bar">', unsafe_allow_html=True)
|
177 |
+
col1, col2 = st.columns([10, 1])
|
178 |
+
user_input = col1.chat_input("Type your message here...")
|
179 |
+
if col2.button("🗑���", help="Clear Chat", key="clear-chat-top"):
|
180 |
+
clear_chat_history()
|
181 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
182 |
+
|
183 |
+
# --- CHAT: display under input, latest on top ---
|
184 |
display_chat_history()
|
|
|
185 |
|
186 |
+
# --- JS: auto-scroll to top on new message ---
|
187 |
st.markdown("""
|
188 |
<script>
|
189 |
window.onload = function() {
|
190 |
+
var anchor = document.getElementById("chat-top-anchor");
|
191 |
+
if(anchor){ anchor.scrollIntoView({ behavior: "smooth", block: "start" }); }
|
192 |
};
|
193 |
window.setTimeout(function(){
|
194 |
+
var anchor = document.getElementById("chat-top-anchor");
|
195 |
+
if(anchor){ anchor.scrollIntoView({ behavior: "smooth", block: "start" }); }
|
196 |
}, 200);
|
197 |
</script>
|
198 |
""", unsafe_allow_html=True)
|
199 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
200 |
if user_input:
|
201 |
# --- OpenAI Assistant Response ---
|
202 |
thread_id = get_or_create_thread_id()
|