Spaces:
Runtime error
Runtime error
File size: 6,699 Bytes
6b3e3b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | import streamlit as st
import os
import tempfile
import shutil
from dotenv import load_dotenv
from huggingface_hub import snapshot_download
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.llms.groq import Groq
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
# 1. Load Environment Variables
load_dotenv()
# --- PAGE CONFIGURATION ---
st.set_page_config(
page_title="Parardha's Insight Engine",
page_icon="🧠",
layout="centered",
initial_sidebar_state="expanded"
)
# --- CUSTOM CSS FOR AESTHETICS ---
st.markdown("""
<style>
/* Import Google Font */
@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;600&display=swap');
html, body, [class*="css"] {
font-family: 'Inter', sans-serif;
}
/* Hide default Streamlit elements */
#MainMenu {visibility: hidden;}
footer {visibility: hidden;}
/* Custom Header Styling */
.title-text {
text-align: center;
font-size: 3rem;
font-weight: 800;
background: linear-gradient(45deg, #FF4B4B, #0068C9);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
margin-bottom: 10px;
padding-top: 20px;
}
.subtitle-text {
text-align: center;
font-size: 1.1rem;
font-weight: 400;
opacity: 0.8;
margin-bottom: 40px;
}
</style>
""", unsafe_allow_html=True)
# --- SIDEBAR CONFIGURATION ---
with st.sidebar:
st.image("https://cdn-icons-png.flaticon.com/512/2593/2593491.png", width=60)
st.markdown("### **Insight Engine**")
st.caption("v1.0 • Built by **Parardha**")
st.markdown("---")
# Step 1: API Key
st.markdown("#### 1️⃣ Configuration")
api_key = os.getenv("GROQ_API_KEY")
if api_key:
st.success("✅ System Online")
else:
st.error("⚠️ API Key Not Found")
st.info("Please set `GROQ_API_KEY` in secrets.")
st.stop()
st.markdown("---")
# Step 2: Upload
st.markdown("#### 2️⃣ Knowledge Base")
uploaded_file = st.file_uploader("Upload a PDF to analyze", type=["pdf"])
if uploaded_file:
st.caption(f"📄 Loaded: {uploaded_file.name}")
st.markdown("---")
st.markdown("""
<div style="text-align: center; opacity: 0.6; font-size: 0.8em;">
Powered by <b>Llama 3.3</b> & <b>Parardha's AI</b>
</div>
""", unsafe_allow_html=True)
# --- MAIN HEADER ---
st.markdown('<div class="title-text">Parardha\'s Insight Engine</div>', unsafe_allow_html=True)
st.markdown('<div class="subtitle-text">Your personal AI research assistant. Upload a document to unlock insights.</div>', unsafe_allow_html=True)
# --- INITIALIZE SESSION STATE ---
if "messages" not in st.session_state:
st.session_state.messages = [
{"role": "assistant", "content": "Greetings! I am ready to analyze your documents. Please upload a PDF in the sidebar to begin."}
]
# --- DISPLAY CHAT HISTORY ---
for message in st.session_state.messages:
# Custom avatars for a polished look
avatar = "https://cdn-icons-png.flaticon.com/512/4712/4712027.png" if message["role"] == "assistant" else "https://cdn-icons-png.flaticon.com/512/9131/9131529.png"
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message["content"])
# --- MODEL LOADING LOGIC ---
@st.cache_resource(show_spinner=False)
def load_models(_api_key):
try:
# LLM Setup
llm = Groq(model="llama-3.3-70b-versatile", api_key=_api_key)
# Embedding Setup (Stable)
repo_id = "sentence-transformers/all-MiniLM-L6-v2"
current_dir = os.path.dirname(os.path.abspath(__file__))
model_path = os.path.join(current_dir, "ai_model")
if not os.path.exists(model_path):
snapshot_download(repo_id=repo_id, local_dir=model_path, local_dir_use_symlinks=False)
try:
embed_model = HuggingFaceEmbedding(model_name=model_path)
except Exception:
if os.path.exists(model_path):
shutil.rmtree(model_path)
snapshot_download(repo_id=repo_id, local_dir=model_path, local_dir_use_symlinks=False)
embed_model = HuggingFaceEmbedding(model_name=model_path)
return llm, embed_model, None
except Exception as e:
return None, None, str(e)
def index_document(file):
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = os.path.join(temp_dir, file.name)
with open(temp_path, "wb") as f:
f.write(file.getbuffer())
reader = SimpleDirectoryReader(input_dir=temp_dir)
documents = reader.load_data()
index = VectorStoreIndex.from_documents(documents)
return index
# --- CHAT INTERACTION ---
if prompt := st.chat_input("Ask a question about your document..."):
# User Message
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar="https://cdn-icons-png.flaticon.com/512/9131/9131529.png"):
st.markdown(prompt)
# Validation
if not uploaded_file:
st.warning("⚠️ Please upload a PDF document in the sidebar to proceed.")
st.stop()
# AI Processing
with st.chat_message("assistant", avatar="https://cdn-icons-png.flaticon.com/512/4712/4712027.png"):
with st.spinner("Processing inquiry..."):
llm, embed_model, error_msg = load_models(api_key)
if error_msg:
st.error(f"System Error: {error_msg}")
st.stop()
Settings.llm = llm
Settings.embed_model = embed_model
try:
# Index only if new session or file changed (basic check)
if "vector_index" not in st.session_state:
with st.spinner("Indexing knowledge base..."):
st.session_state.vector_index = index_document(uploaded_file)
query_engine = st.session_state.vector_index.as_query_engine()
response = query_engine.query(prompt)
st.markdown(response.response)
st.session_state.messages.append({"role": "assistant", "content": response.response})
except Exception as e:
st.error(f"Processing Error: {str(e)}") |