from dotenv import load_dotenv
load_dotenv()
import streamlit as st
import base64
import os
from rag_pipeline import RAGSystem # your existing RAG pipeline
from models.groq_stt import transcribe_audio
# ================================
# OPTIONAL: VOICE TRANSCRIPTION HOOK
# ================================
def transcribe_audio(audio_file):
"""
TODO: Connect this to a real STT backend (e.g., Groq Whisper or OpenAI Whisper).
audio_file is a Streamlit UploadedFile from st.audio_input.
You can get bytes via: audio_bytes = audio_file.getvalue()
Example (pseudo-code with Groq Whisper / OpenAI-compatible):
from openai import OpenAI
client = OpenAI(base_url="https://api.groq.com/openai/v1", api_key=os.getenv("GROQ_API_KEY"))
audio_bytes = audio_file.getvalue()
with open("tmp.wav", "wb") as f:
f.write(audio_bytes)
resp = client.audio.transcriptions.create(
model="whisper-large-v3-turbo",
file=open("tmp.wav", "rb"),
response_format="text",
language="en"
)
return resp
For now, we just show a warning and return None.
"""
st.warning("π€ Voice UI is ready, but STT is not wired yet. "
"Implement transcribe_audio() using Groq Whisper or another STT API.")
return None
# ================================
# PAGE CONFIG
# ================================
st.set_page_config(
page_title="π AI PDF Chat Assistant",
layout="wide",
page_icon="π€",
)
# ================================
# LIGHT / DARK MODE TOGGLE
# ================================
# ================================
# THEME SWITCH + SIDEBAR STYLING
# ================================
theme_choice = st.sidebar.radio("π Theme", ["Light", "Dark"])
# -------------------------------
# THEME COLORS
# -------------------------------
if theme_choice == "Dark":
bg_color = "#0E0F14" # deep black-blue background
text_color = "#FFFFFF" # white text
uploader_text = "#E0E0E0" # soft gray
sidebar_bg = "#121212" # premium dark sidebar
sidebar_text = "#F5F5F5" # bright sidebar text
else:
bg_color = "#E8D8B8" # clean light grey background
text_color = "#222222" # dark text
uploader_text = "#444444" # medium gray
sidebar_bg = "#FFFFFF" # PURE white sidebar
sidebar_text = "#222222" # dark gray text for readability
# -------------------------------
# APPLY THEME CSS
# -------------------------------
st.markdown(f"""
""", unsafe_allow_html=True)
# ================================
# FIX FILE UPLOADER TEXT COLOR
# ================================
st.markdown(f"""
""", unsafe_allow_html=True)
# ================================
# FIX SIDEBAR COLLAPSE BUTTON & CLEAR CHAT TEXT
# ================================
st.markdown("""
""", unsafe_allow_html=True)
# ================================
# PREMIUM UI CSS (CHAT + ANIMATIONS)
# ================================
st.markdown("""
""", unsafe_allow_html=True)
# ================================
# HEADER
# ================================
st.markdown(f"""
π€ AI Document Chat Assistant
Upload multiple PDFs β Process β Auto-summarize β Chat using Groq LLaMA-3.3-70B
""", unsafe_allow_html=True)
# ================================
# SESSION STATE: MULTI-PDF + RAG + CHAT
# ================================
if "pdf_docs" not in st.session_state:
# doc_id -> {name, path, indexed: bool}
st.session_state.pdf_docs = {}
if "rags" not in st.session_state:
# doc_id -> RAGSystem()
st.session_state.rags = {}
if "chat_histories" not in st.session_state:
# doc_id -> list of {"role": "user"/"assistant", "content": str}
st.session_state.chat_histories = {}
if "summaries" not in st.session_state:
# doc_id -> summary text
st.session_state.summaries = {}
# ================================
# LAYOUT
# ================================
left, right = st.columns([1.2, 2.8])
# ================================
# LEFT PANEL β MULTI PDF UPLOAD
# ================================
with left:
st.markdown("", unsafe_allow_html=True)
# ================================
# RIGHT PANEL β ACTIVE DOC + CHAT
# ================================
with right:
if not st.session_state.pdf_docs:
st.info("π₯ Upload one or more PDFs on the left to get started.")
else:
# "Tabs" style selector using horizontal radio
doc_ids = list(st.session_state.pdf_docs.keys())
active_doc_id = st.radio(
"π Active document",
options=doc_ids,
format_func=lambda x: "π " + st.session_state.pdf_docs[x]["name"],
horizontal=True,
)
active_meta = st.session_state.pdf_docs[active_doc_id]
active_rag = st.session_state.rags[active_doc_id]
active_chat = st.session_state.chat_histories[active_doc_id]
st.markdown(f"### π {active_meta['name']}")
# ============================
# PDF PREVIEW (CHROME-SAFE VIEWER)
# ============================
try:
with open(active_meta["path"], "rb") as f:
pdf_bytes = f.read()
b64 = base64.b64encode(pdf_bytes).decode("utf-8")
pdf_viewer = f"""