Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,13 +8,13 @@ import firebase_admin
|
|
| 8 |
from firebase_admin import credentials, firestore
|
| 9 |
from openai import OpenAI
|
| 10 |
|
| 11 |
-
# Firebase setup
|
| 12 |
if not firebase_admin._apps:
|
| 13 |
cred = credentials.Certificate("firebase-service-account.json")
|
| 14 |
firebase_admin.initialize_app(cred)
|
| 15 |
db = firestore.client()
|
| 16 |
|
| 17 |
-
# OpenAI setup
|
| 18 |
openai_key = os.getenv("openai_key")
|
| 19 |
assistant_id = os.getenv("assistant_id")
|
| 20 |
client = OpenAI(api_key=openai_key)
|
|
@@ -32,7 +32,7 @@ VOICE_OPTIONS = {
|
|
| 32 |
|
| 33 |
st.set_page_config(page_title="LOR Technologies AI Assistant", layout="wide")
|
| 34 |
|
| 35 |
-
# State
|
| 36 |
if "user_id" not in st.session_state:
|
| 37 |
st.session_state["user_id"] = str(uuid.uuid4())
|
| 38 |
user_id = st.session_state["user_id"]
|
|
@@ -46,14 +46,25 @@ 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 |
.stChatMessage { max-width: 85%; border-radius: 12px; padding: 8px; margin-bottom: 10px; }
|
| 54 |
.stChatMessage[data-testid="stChatMessage-user"] { background: #f0f0f0; color: #000000; }
|
| 55 |
.stChatMessage[data-testid="stChatMessage-assistant"] { background: #e3f2fd; color: #000000; }
|
| 56 |
.lt-logo { vertical-align: middle; }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
.footer-fakebar {
|
| 58 |
position: fixed;
|
| 59 |
left: 0; bottom: 0;
|
|
@@ -65,19 +76,19 @@ st.markdown("""
|
|
| 65 |
}
|
| 66 |
.footer-fakebar .element-container { flex: 1 1 auto; }
|
| 67 |
.footer-fakebar input { font-size: 1.15em !important; }
|
| 68 |
-
.footer-placeholder { height:
|
| 69 |
</style>
|
| 70 |
""", unsafe_allow_html=True)
|
|
|
|
|
|
|
| 71 |
st.markdown("""
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
</span>
|
| 77 |
-
</div>
|
| 78 |
""", unsafe_allow_html=True)
|
| 79 |
|
| 80 |
-
# Sidebar: audio/voice controls
|
| 81 |
with st.sidebar:
|
| 82 |
st.markdown("### Voice Settings & Controls")
|
| 83 |
selected_voice = st.selectbox(
|
|
@@ -140,9 +151,10 @@ def display_chat_history():
|
|
| 140 |
chat_msgs.append(
|
| 141 |
f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>LORAIN:</strong> {data['content']}</div>"
|
| 142 |
)
|
| 143 |
-
|
|
|
|
| 144 |
|
| 145 |
-
# TTS
|
| 146 |
async def edge_tts_synthesize(text, voice, user_id):
|
| 147 |
out_path = f"output_{user_id}.mp3"
|
| 148 |
communicate = edge_tts.Communicate(text, voice)
|
|
@@ -160,10 +172,24 @@ def synthesize_voice(text, voice_key, user_id):
|
|
| 160 |
st.session_state["last_voice"] = voice
|
| 161 |
return out_path
|
| 162 |
|
| 163 |
-
# --- Chat history and
|
| 164 |
display_chat_history()
|
| 165 |
st.markdown('<div class="footer-placeholder"></div>', unsafe_allow_html=True)
|
| 166 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 167 |
# --- "Floating" Chat Input and Clear Chat button ---
|
| 168 |
with st.container():
|
| 169 |
st.markdown('<div class="footer-fakebar">', unsafe_allow_html=True)
|
|
@@ -175,16 +201,17 @@ with st.container():
|
|
| 175 |
|
| 176 |
if user_input:
|
| 177 |
# --- OpenAI Assistant Response ---
|
| 178 |
-
|
|
|
|
| 179 |
save_message("user", user_input)
|
| 180 |
with st.spinner("Thinking and typing... 💭"):
|
| 181 |
-
run = client.beta.threads.runs.create(thread_id=
|
| 182 |
while True:
|
| 183 |
-
run_status = client.beta.threads.runs.retrieve(thread_id=
|
| 184 |
if run_status.status == "completed":
|
| 185 |
break
|
| 186 |
time.sleep(1)
|
| 187 |
-
messages_response = client.beta.threads.messages.list(thread_id=
|
| 188 |
latest_response = sorted(messages_response.data, key=lambda x: x.created_at)[-1]
|
| 189 |
assistant_message = latest_response.content[0].text.value
|
| 190 |
save_message("assistant", assistant_message)
|
|
|
|
| 8 |
from firebase_admin import credentials, firestore
|
| 9 |
from openai import OpenAI
|
| 10 |
|
| 11 |
+
# ---- Firebase setup ----
|
| 12 |
if not firebase_admin._apps:
|
| 13 |
cred = credentials.Certificate("firebase-service-account.json")
|
| 14 |
firebase_admin.initialize_app(cred)
|
| 15 |
db = firestore.client()
|
| 16 |
|
| 17 |
+
# ---- OpenAI setup ----
|
| 18 |
openai_key = os.getenv("openai_key")
|
| 19 |
assistant_id = os.getenv("assistant_id")
|
| 20 |
client = OpenAI(api_key=openai_key)
|
|
|
|
| 32 |
|
| 33 |
st.set_page_config(page_title="LOR Technologies AI Assistant", layout="wide")
|
| 34 |
|
| 35 |
+
# --- State setup
|
| 36 |
if "user_id" not in st.session_state:
|
| 37 |
st.session_state["user_id"] = str(uuid.uuid4())
|
| 38 |
user_id = st.session_state["user_id"]
|
|
|
|
| 46 |
if "selected_voice" not in st.session_state:
|
| 47 |
st.session_state["selected_voice"] = "Jenny (US, Female)"
|
| 48 |
|
| 49 |
+
# --- Logo and Branding Styling ---
|
| 50 |
st.markdown("""
|
| 51 |
<style>
|
| 52 |
+
.block-container {padding-top: 0.5rem; padding-bottom: 0rem;}
|
| 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 |
.lt-logo { vertical-align: middle; }
|
| 58 |
+
.logo-container {
|
| 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 |
.footer-fakebar {
|
| 69 |
position: fixed;
|
| 70 |
left: 0; bottom: 0;
|
|
|
|
| 76 |
}
|
| 77 |
.footer-fakebar .element-container { flex: 1 1 auto; }
|
| 78 |
.footer-fakebar input { font-size: 1.15em !important; }
|
| 79 |
+
.footer-placeholder { height: 98px; }
|
| 80 |
</style>
|
| 81 |
""", unsafe_allow_html=True)
|
| 82 |
+
|
| 83 |
+
# --- LOGO AREA (fixes cropping, ensures visibility) ---
|
| 84 |
st.markdown("""
|
| 85 |
+
<div class="logo-container">
|
| 86 |
+
<img src="https://lortechnologies.com/wp-content/uploads/2023/03/LOR-Online-Logo.svg" width="140" style="display: inline-block; margin-bottom: 0.2em;" class="lt-logo"/>
|
| 87 |
+
<div style="font-size: 14px; color: gray; margin-top: 0.25em;">Powered by LOR Technologies</div>
|
| 88 |
+
</div>
|
|
|
|
|
|
|
| 89 |
""", unsafe_allow_html=True)
|
| 90 |
|
| 91 |
+
# --- Sidebar: audio/voice controls
|
| 92 |
with st.sidebar:
|
| 93 |
st.markdown("### Voice Settings & Controls")
|
| 94 |
selected_voice = st.selectbox(
|
|
|
|
| 151 |
chat_msgs.append(
|
| 152 |
f"<div class='stChatMessage' data-testid='stChatMessage-assistant'>{assistant_icon_html} <strong>LORAIN:</strong> {data['content']}</div>"
|
| 153 |
)
|
| 154 |
+
# Attach chat, plus the scroll anchor
|
| 155 |
+
st.markdown("".join(chat_msgs[::-1]) + '<div id="chat-bottom-anchor"></div>', unsafe_allow_html=True)
|
| 156 |
|
| 157 |
+
# --- Edge TTS synth ---
|
| 158 |
async def edge_tts_synthesize(text, voice, user_id):
|
| 159 |
out_path = f"output_{user_id}.mp3"
|
| 160 |
communicate = edge_tts.Communicate(text, voice)
|
|
|
|
| 172 |
st.session_state["last_voice"] = voice
|
| 173 |
return out_path
|
| 174 |
|
| 175 |
+
# --- Chat history, scroll anchor, and fake footer for space ---
|
| 176 |
display_chat_history()
|
| 177 |
st.markdown('<div class="footer-placeholder"></div>', unsafe_allow_html=True)
|
| 178 |
|
| 179 |
+
# --- JS: auto-scroll to latest message ---
|
| 180 |
+
st.markdown("""
|
| 181 |
+
<script>
|
| 182 |
+
window.onload = function() {
|
| 183 |
+
var anchor = document.getElementById("chat-bottom-anchor");
|
| 184 |
+
if(anchor){ anchor.scrollIntoView({ behavior: "smooth", block: "end" }); }
|
| 185 |
+
};
|
| 186 |
+
window.setTimeout(function(){
|
| 187 |
+
var anchor = document.getElementById("chat-bottom-anchor");
|
| 188 |
+
if(anchor){ anchor.scrollIntoView({ behavior: "smooth", block: "end" }); }
|
| 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)
|
|
|
|
| 201 |
|
| 202 |
if user_input:
|
| 203 |
# --- OpenAI Assistant Response ---
|
| 204 |
+
thread_id = get_or_create_thread_id()
|
| 205 |
+
client.beta.threads.messages.create(thread_id=thread_id, role="user", content=user_input)
|
| 206 |
save_message("user", user_input)
|
| 207 |
with st.spinner("Thinking and typing... 💭"):
|
| 208 |
+
run = client.beta.threads.runs.create(thread_id=thread_id, assistant_id=assistant_id)
|
| 209 |
while True:
|
| 210 |
+
run_status = client.beta.threads.runs.retrieve(thread_id=thread_id, run_id=run.id)
|
| 211 |
if run_status.status == "completed":
|
| 212 |
break
|
| 213 |
time.sleep(1)
|
| 214 |
+
messages_response = client.beta.threads.messages.list(thread_id=thread_id)
|
| 215 |
latest_response = sorted(messages_response.data, key=lambda x: x.created_at)[-1]
|
| 216 |
assistant_message = latest_response.content[0].text.value
|
| 217 |
save_message("assistant", assistant_message)
|