import os from dotenv import load_dotenv # Load environment variables load_dotenv() import base64 import gc import random import tempfile import time import uuid from IPython.display import Markdown, display from llama_index.core import Settings from llama_index.llms.cerebras import Cerebras from llama_index.core import PromptTemplate from llama_index.embeddings.fastembed import FastEmbedEmbedding from llama_index.core import VectorStoreIndex, ServiceContext, SimpleDirectoryReader import streamlit as st if "id" not in st.session_state: st.session_state.id = uuid.uuid4() st.session_state.file_cache = {} session_id = st.session_state.id client = None @st.cache_resource def load_llm(): llm = Cerebras(model="meta-llama/Llama-4-Maverick-17B-128E-Instruct-FP8", api_key=os.getenv("CEREBRAS_API_KEY")) return llm def reset_chat(): st.session_state.messages = [] st.session_state.context = None gc.collect() def display_pdf(file): st.markdown("### PDF Preview") base64_pdf = base64.b64encode(file.read()).decode("utf-8") pdf_display = f"""""" st.markdown(pdf_display, unsafe_allow_html=True) with st.sidebar: # Add API Key Input col1, col2 = st.columns([1, 3]) with col1: # Add vertical space to align with header st.write("") st.image("./assets/cerebras.png", width=200) # with col2: # st.header("Groq Configuration") # st.write("Groq API Key") # Add hyperlink to get API key st.markdown("[Get your API key](https://www.cerebras.ai/)", unsafe_allow_html=True) api_key_input = st.text_input("Enter your Cerebras API Key:", type="password", key="api_key_input") # Store API Key in session state if provided if api_key_input: st.session_state.groq_api_key = api_key_input st.header(f"Add your documents!") uploaded_file = st.file_uploader("Choose your `.pdf` file", type="pdf") if uploaded_file: try: with tempfile.TemporaryDirectory() as temp_dir: file_path = os.path.join(temp_dir, uploaded_file.name) with open(file_path, "wb") as f: f.write(uploaded_file.getvalue()) file_key = f"{session_id}-{uploaded_file.name}" st.write("Indexing your document...") if file_key not in st.session_state.get('file_cache', {}): if os.path.exists(temp_dir): loader = SimpleDirectoryReader( input_dir=temp_dir, required_exts=[".pdf"], recursive=True ) else: st.error('Could not find the file you uploaded, please check again...') st.stop() docs = loader.load_data() llm = load_llm() embed_model = FastEmbedEmbedding(model_name="BAAI/bge-large-en-v1.5") Settings.embed_model = embed_model index = VectorStoreIndex.from_documents(docs, show_progress=True) Settings.llm = llm query_engine = index.as_query_engine(streaming=True) qa_prompt_tmpl_str = ( "Context information is below.\n" "---------------------\n" "{context_str}\n" "---------------------\n" "Given the context information above I want you to think step by step to answer the query in a crisp manner, in case you don't know the answer say 'I don't know!'.\n" "Query: {query_str}\n" "Answer: " ) qa_prompt_tmpl = PromptTemplate(qa_prompt_tmpl_str) query_engine.update_prompts( {"response_synthesizer:text_qa_template": qa_prompt_tmpl} ) st.session_state.file_cache[file_key] = query_engine else: query_engine = st.session_state.file_cache[file_key] st.success("Ready to Chat!") display_pdf(uploaded_file) except Exception as e: st.error(f"An error occurred: {e}") st.stop() col1, col2 = st.columns([6, 1]) with col1: # Removed the original header st.markdown("