import streamlit as st import asyncio import edge_tts import time import os import uuid import re import html import firebase_admin from firebase_admin import credentials, firestore from openai import OpenAI # ---- Firebase setup ---- if not firebase_admin._apps: cred = credentials.Certificate("firebase-service-account.json") firebase_admin.initialize_app(cred) db = firestore.client() # ---- OpenAI setup ---- openai_key = os.getenv("openai_key") assistant_id = os.getenv("assistant_id") client = OpenAI(api_key=openai_key) VOICE_OPTIONS = { "Jenny (US, Female)": "en-US-JennyNeural", "Aria (US, Female)": "en-US-AriaNeural", "Ryan (UK, Male)": "en-GB-RyanNeural", "Natasha (AU, Female)": "en-AU-NatashaNeural", "William (AU, Male)": "en-AU-WilliamNeural", "Libby (UK, Female)": "en-GB-LibbyNeural", "Leah (SA, Female)": "en-ZA-LeahNeural", "Luke (SA, Male)": "en-ZA-LukeNeural" } st.set_page_config(page_title="C2 Group AI Assistant", layout="wide") # --- State setup if "user_id" not in st.session_state: st.session_state["user_id"] = str(uuid.uuid4()) user_id = st.session_state["user_id"] if "mute_voice" not in st.session_state: st.session_state["mute_voice"] = False if "last_tts_text" not in st.session_state: st.session_state["last_tts_text"] = "" if "last_audio_path" not in st.session_state: st.session_state["last_audio_path"] = "" if "selected_voice" not in st.session_state: st.session_state["selected_voice"] = "Jenny (US, Female)" # --- CSS --- st.markdown(""" """, unsafe_allow_html=True) # --- Top Branding + clear button --- st.markdown("""
""", unsafe_allow_html=True) # --- Sidebar: voice settings --- with st.sidebar: st.markdown("### Voice Settings & Controls") selected_voice = st.selectbox( "Select assistant voice", list(VOICE_OPTIONS.keys()), index=list(VOICE_OPTIONS.keys()).index(st.session_state["selected_voice"]) ) st.session_state["selected_voice"] = selected_voice last_audio = st.session_state.get("last_audio_path") mute_voice = st.session_state.get("mute_voice", False) if last_audio and os.path.exists(last_audio): st.audio(last_audio, format="audio/mp3", autoplay=not mute_voice) if st.button("🔁 Replay Voice"): st.audio(last_audio, format="audio/mp3", autoplay=True) if not mute_voice: if st.button("🔇 Mute Voice"): st.session_state["mute_voice"] = True st.rerun() else: if st.button("🔊 Unmute Voice"): st.session_state["mute_voice"] = False st.rerun() # --- Firestore helpers --- def get_or_create_thread_id(): doc_ref = db.collection("users").document(user_id) doc = doc_ref.get() if doc.exists: return doc.to_dict()["thread_id"] else: thread = client.beta.threads.create() doc_ref.set({"thread_id": thread.id, "created_at": firestore.SERVER_TIMESTAMP}) return thread.id def save_message(role, content): db.collection("users").document(user_id).collection("messages").add({ "role": role, "content": content, "timestamp": firestore.SERVER_TIMESTAMP }) def clear_chat_history(): user_doc_ref = db.collection("users").document(user_id) for msg in user_doc_ref.collection("messages").stream(): msg.reference.delete() user_doc_ref.delete() st.session_state.clear() st.rerun() def display_chat_history(): messages = db.collection("users").document(user_id).collection("messages").order_by("timestamp").stream() assistant_icon_html = "