Update src/app_updated.py
Browse files- src/app_updated.py +209 -201
src/app_updated.py
CHANGED
@@ -1,202 +1,210 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import os
|
3 |
-
import dotenv
|
4 |
-
import uuid
|
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 |
-
text-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
border-
|
64 |
-
}
|
65 |
-
.
|
66 |
-
background-color: rgba(67, 97, 238, 0.
|
67 |
-
border-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
border:
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
}
|
85 |
-
.status-
|
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 |
-
st.
|
148 |
-
st.
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
st.
|
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 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
202 |
st.write_stream(stream_llm_rag_response(llm_stream, messages))
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import dotenv
|
4 |
+
import uuid
|
5 |
+
import logging
|
6 |
+
|
7 |
+
# Configure environment for Hugging Face Spaces
|
8 |
+
os.environ["HF_HOME"] = "/tmp/.cache/huggingface"
|
9 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/.cache/huggingface"
|
10 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/.cache/huggingface"
|
11 |
+
|
12 |
+
# Create necessary directories
|
13 |
+
os.makedirs("/tmp/.cache/huggingface", exist_ok=True)
|
14 |
+
os.makedirs("/tmp/chroma_persistent_db", exist_ok=True)
|
15 |
+
os.makedirs("/tmp/source_files", exist_ok=True)
|
16 |
+
|
17 |
+
# Configure logging
|
18 |
+
logging.basicConfig(level=logging.INFO)
|
19 |
+
logger = logging.getLogger(__name__)
|
20 |
+
|
21 |
+
from langchain.schema import HumanMessage, AIMessage
|
22 |
+
from langchain_groq import ChatGroq
|
23 |
+
from rag_methods import (
|
24 |
+
load_doc_to_db,
|
25 |
+
load_url_to_db,
|
26 |
+
stream_llm_response,
|
27 |
+
stream_llm_rag_response,
|
28 |
+
)
|
29 |
+
|
30 |
+
dotenv.load_dotenv()
|
31 |
+
|
32 |
+
# --- Custom CSS Styling ---
|
33 |
+
def apply_custom_css():
|
34 |
+
st.markdown("""
|
35 |
+
<style>
|
36 |
+
.main .block-container {
|
37 |
+
padding-top: 2rem;
|
38 |
+
padding-bottom: 2rem;
|
39 |
+
}
|
40 |
+
h1, h2, h3, h4 {
|
41 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
42 |
+
font-weight: 600;
|
43 |
+
}
|
44 |
+
.app-title {
|
45 |
+
text-align: center;
|
46 |
+
color: #4361ee;
|
47 |
+
font-size: 2.2rem;
|
48 |
+
font-weight: 700;
|
49 |
+
margin-bottom: 1.5rem;
|
50 |
+
padding: 1rem;
|
51 |
+
border-radius: 10px;
|
52 |
+
background: linear-gradient(90deg, rgba(67, 97, 238, 0.1), rgba(58, 12, 163, 0.1));
|
53 |
+
text-shadow: 0px 0px 2px rgba(0,0,0,0.1);
|
54 |
+
}
|
55 |
+
.chat-container {
|
56 |
+
border-radius: 10px;
|
57 |
+
padding: 10px;
|
58 |
+
margin-bottom: 1rem;
|
59 |
+
}
|
60 |
+
.message-container {
|
61 |
+
padding: 0.8rem;
|
62 |
+
margin-bottom: 0.8rem;
|
63 |
+
border-radius: 8px;
|
64 |
+
}
|
65 |
+
.user-message {
|
66 |
+
background-color: rgba(67, 97, 238, 0.15);
|
67 |
+
border-left: 4px solid #4361ee;
|
68 |
+
}
|
69 |
+
.assistant-message {
|
70 |
+
background-color: rgba(58, 12, 163, 0.1);
|
71 |
+
border-left: 4px solid #3a0ca3;
|
72 |
+
}
|
73 |
+
.document-list {
|
74 |
+
background-color: rgba(67, 97, 238, 0.05);
|
75 |
+
border-radius: 8px;
|
76 |
+
padding: 0.7rem;
|
77 |
+
}
|
78 |
+
.upload-container {
|
79 |
+
border: 2px dashed rgba(67, 97, 238, 0.5);
|
80 |
+
border-radius: 10px;
|
81 |
+
padding: 1rem;
|
82 |
+
margin-bottom: 1rem;
|
83 |
+
text-align: center;
|
84 |
+
}
|
85 |
+
.status-indicator {
|
86 |
+
font-size: 0.85rem;
|
87 |
+
font-weight: 600;
|
88 |
+
padding: 0.3rem 0.7rem;
|
89 |
+
border-radius: 20px;
|
90 |
+
display: inline-block;
|
91 |
+
margin-bottom: 0.5rem;
|
92 |
+
}
|
93 |
+
.status-active {
|
94 |
+
background-color: rgba(46, 196, 182, 0.2);
|
95 |
+
color: #2EC4B6;
|
96 |
+
}
|
97 |
+
.status-inactive {
|
98 |
+
background-color: rgba(231, 111, 81, 0.2);
|
99 |
+
color: #E76F51;
|
100 |
+
}
|
101 |
+
@media screen and (max-width: 768px) {
|
102 |
+
.app-title {
|
103 |
+
font-size: 1.8rem;
|
104 |
+
padding: 0.7rem;
|
105 |
+
}
|
106 |
+
}
|
107 |
+
</style>
|
108 |
+
""", unsafe_allow_html=True)
|
109 |
+
|
110 |
+
# --- Page Setup ---
|
111 |
+
st.set_page_config(
|
112 |
+
page_title="RAG-Xpert: An Enhanced RAG Framework",
|
113 |
+
page_icon="π",
|
114 |
+
layout="centered",
|
115 |
+
initial_sidebar_state="expanded"
|
116 |
+
)
|
117 |
+
|
118 |
+
apply_custom_css()
|
119 |
+
|
120 |
+
st.markdown('<h1 class="app-title">π RAG-Xpert: An Enhanced Retrieval-Augmented Generation Framework π€</h1>', unsafe_allow_html=True)
|
121 |
+
|
122 |
+
# --- Session Initialization ---
|
123 |
+
if "session_id" not in st.session_state:
|
124 |
+
st.session_state.session_id = str(uuid.uuid4())
|
125 |
+
if "rag_sources" not in st.session_state:
|
126 |
+
st.session_state.rag_sources = []
|
127 |
+
if "messages" not in st.session_state:
|
128 |
+
st.session_state.messages = [
|
129 |
+
{"role": "user", "content": "Hello"},
|
130 |
+
{"role": "assistant", "content": "Hi there! How can I assist you today?"}
|
131 |
+
]
|
132 |
+
|
133 |
+
# --- Sidebar ---
|
134 |
+
with st.sidebar:
|
135 |
+
st.markdown("""
|
136 |
+
<div style="
|
137 |
+
text-align: center;
|
138 |
+
padding: 1rem 0;
|
139 |
+
margin-bottom: 1.5rem;
|
140 |
+
background: linear-gradient(to right, #4361ee22, #3a0ca322);
|
141 |
+
border-radius: 10px;">
|
142 |
+
<div style="font-size: 0.85rem; color: #888;">Developed By</div>
|
143 |
+
<div style="font-size: 1.2rem; font-weight: 700; color: #4361ee;">Uditanshu Pandey</div>
|
144 |
+
</div>
|
145 |
+
""", unsafe_allow_html=True)
|
146 |
+
|
147 |
+
is_vector_db_loaded = "vector_db" in st.session_state and st.session_state.vector_db is not None
|
148 |
+
rag_status = st.toggle("Enable Knowledge Enhancement (RAG)", value=is_vector_db_loaded, key="use_rag", disabled=not is_vector_db_loaded)
|
149 |
+
|
150 |
+
if rag_status:
|
151 |
+
st.markdown('<div class="status-indicator status-active">RAG Mode: Active β</div>', unsafe_allow_html=True)
|
152 |
+
else:
|
153 |
+
st.markdown('<div class="status-indicator status-inactive">RAG Mode: Inactive β</div>', unsafe_allow_html=True)
|
154 |
+
|
155 |
+
st.toggle("Show Retrieved Context", key="debug_mode", value=False)
|
156 |
+
st.button("π§Ή Clear Chat History", on_click=lambda: st.session_state.messages.clear(), type="primary")
|
157 |
+
|
158 |
+
st.markdown("<h3 style='text-align: center; color: #4361ee; margin-top: 1.5rem;'>π Knowledge Sources</h3>", unsafe_allow_html=True)
|
159 |
+
st.markdown('<div class="upload-container">', unsafe_allow_html=True)
|
160 |
+
st.file_uploader("π Upload Documents", type=["pdf", "txt", "docx", "md"], accept_multiple_files=True, on_change=load_doc_to_db, key="rag_docs")
|
161 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
162 |
+
|
163 |
+
st.text_input("π Add Webpage URL", placeholder="https://example.com", on_change=load_url_to_db, key="rag_url")
|
164 |
+
|
165 |
+
doc_count = len(st.session_state.rag_sources) if is_vector_db_loaded else 0
|
166 |
+
with st.expander(f"π Knowledge Base ({doc_count} sources)"):
|
167 |
+
if doc_count:
|
168 |
+
st.markdown('<div class="document-list">', unsafe_allow_html=True)
|
169 |
+
for i, source in enumerate(st.session_state.rag_sources):
|
170 |
+
st.markdown(f"**{i+1}.** {source}")
|
171 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
172 |
+
else:
|
173 |
+
st.info("No documents added yet. Upload files or add URLs to enhance the assistant's knowledge.")
|
174 |
+
|
175 |
+
# --- Initialize LLM ---
|
176 |
+
llm_stream = ChatGroq(
|
177 |
+
model_name="meta-llama/llama-4-scout-17b-16e-instruct",
|
178 |
+
api_key=os.getenv("GROQ_API_KEY"),
|
179 |
+
temperature=0.4,
|
180 |
+
max_tokens=1024,
|
181 |
+
)
|
182 |
+
|
183 |
+
# --- Chat Display ---
|
184 |
+
st.markdown('<div class="chat-container">', unsafe_allow_html=True)
|
185 |
+
for message in st.session_state.messages:
|
186 |
+
avatar = "π€" if message["role"] == "user" else "π€"
|
187 |
+
css_class = "user-message" if message["role"] == "user" else "assistant-message"
|
188 |
+
with st.chat_message(message["role"], avatar=avatar):
|
189 |
+
st.markdown(f'<div class="message-container {css_class}">{message["content"]}</div>', unsafe_allow_html=True)
|
190 |
+
st.markdown('</div>', unsafe_allow_html=True)
|
191 |
+
|
192 |
+
# --- User Input Handling ---
|
193 |
+
if prompt := st.chat_input("Ask me anything..."):
|
194 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
195 |
+
with st.chat_message("user", avatar="π€"):
|
196 |
+
st.markdown(f'<div class="message-container user-message">{prompt}</div>', unsafe_allow_html=True)
|
197 |
+
|
198 |
+
with st.chat_message("assistant", avatar="π€"):
|
199 |
+
thinking_placeholder = st.empty()
|
200 |
+
thinking_placeholder.info("Thinking... Please wait a moment.")
|
201 |
+
messages = [
|
202 |
+
HumanMessage(content=m["content"]) if m["role"] == "user" else AIMessage(content=m["content"])
|
203 |
+
for m in st.session_state.messages
|
204 |
+
]
|
205 |
+
if not st.session_state.use_rag:
|
206 |
+
thinking_placeholder.empty()
|
207 |
+
st.write_stream(stream_llm_response(llm_stream, messages))
|
208 |
+
else:
|
209 |
+
thinking_placeholder.info("Searching knowledge base... Please wait.")
|
210 |
st.write_stream(stream_llm_rag_response(llm_stream, messages))
|