-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
260 lines (206 loc) · 10.1 KB
/
Copy pathmain.py
File metadata and controls
260 lines (206 loc) · 10.1 KB
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
from app.ui import render, read_pdf, show_sources
from app.config import MISTRAL_API_KEY
from app.mistral_client import MistralClient
from app.rag import chunk_text, build_index, retrieve
import streamlit as st
import uuid
from datetime import datetime
if not MISTRAL_API_KEY:
raise RuntimeError("MISTRAL_API_KEY not set")
client = MistralClient(MISTRAL_API_KEY)
@st.cache_resource(show_spinner=False)
def process_document(uploaded_file):
"""Process document once and cache results"""
with st.spinner("Processing document for the first time..."):
text = read_pdf(uploaded_file)
chunks = chunk_text(text)
embeddings = client.embed(chunks)
index = build_index(embeddings)
return chunks, index
# Initialize session state for chat
if 'chat_history' not in st.session_state:
st.session_state.chat_history = []
if 'document_processed' not in st.session_state:
st.session_state.document_processed = False
if 'current_document' not in st.session_state:
st.session_state.current_document = None
def add_message(role, content, sources=None):
"""Add a message to chat history"""
st.session_state.chat_history.append({
'id': str(uuid.uuid4()),
'role': role,
'content': content,
'sources': sources or [],
'timestamp': datetime.now().strftime("%H:%M")
})
def clear_chat():
"""Clear chat history but keep document"""
st.session_state.chat_history = []
def answer_question(question, chunks, index):
"""Generate answer for a question"""
with st.spinner("🔍 Searching document..."):
q_embedding = client.embed([question])[0]
context = retrieve(q_embedding, index, chunks)
with st.spinner("💭 Generating answer..."):
# Build prompt with conversation history for context
conversation_context = ""
if st.session_state.chat_history:
recent_messages = st.session_state.chat_history[-4:] # Last 4 messages for context
for msg in recent_messages:
if msg['role'] == 'user':
conversation_context += f"User: {msg['content']}\n"
elif msg['role'] == 'assistant':
conversation_context += f"Assistant: {msg['content']}\n"
prompt = f"""You are a helpful assistant answering questions about a document.
Use ONLY information from the provided context. If the answer is not in the context, say you don't know. Always give answers in paragraph form.
Previous conversation context (for reference only):
{conversation_context}
Current context from document:
{chr(10).join(context)}
Question: {question}
Answer based only on the document context above:"""
answer = client.chat(prompt)
return answer, context
def main():
# Get UI components
uploaded = render()
# Document processing (only once)
if uploaded and not st.session_state.document_processed:
with st.container():
st.subheader("📄 Processing Document")
try:
# Process document with caching
chunks, index = process_document(uploaded)
# Store in session state
st.session_state.chunks = chunks
st.session_state.index = index
st.session_state.document_processed = True
st.session_state.current_document = uploaded.name
# Clear any previous chat
clear_chat()
# Add welcome message
add_message('assistant', f"I've loaded the document **'{uploaded.name}'**. Ask me anything about it!")
st.success(f"✅ Document loaded successfully!")
# Show document stats
with st.expander("📊 Document Details", expanded=False):
col1, col2 = st.columns(2)
with col1:
st.metric("Total Sections", len(chunks))
with col2:
st.metric("Processing Status", "Ready")
st.caption(f"*Document processed and ready for questions*")
st.rerun()
except Exception as e:
st.error(f"❌ Failed to process document: {str(e)}")
return
# Main chat interface
if st.session_state.document_processed:
# Sidebar with controls
with st.sidebar:
st.header("💬 Chat Controls")
st.subheader(f"📄 {st.session_state.current_document}")
# Chat management
if st.button("🗑️ Clear Chat", use_container_width=True, type="secondary"):
clear_chat()
add_message('assistant', "Chat cleared. How can I help you with the document?")
st.rerun()
if st.button("🔄 New Document", use_container_width=True):
for key in ['document_processed', 'chunks', 'index', 'current_document']:
if key in st.session_state:
del st.session_state[key]
st.cache_resource.clear()
st.session_state.chat_history = []
st.rerun()
st.divider()
# Chat statistics
st.caption("**Chat Statistics**")
user_msgs = sum(1 for msg in st.session_state.chat_history if msg['role'] == 'user')
assistant_msgs = sum(1 for msg in st.session_state.chat_history if msg['role'] == 'assistant')
st.caption(f"👤 User: {user_msgs} messages")
st.caption(f"🤖 Assistant: {assistant_msgs} messages")
st.divider()
# Suggested questions
st.caption("**💡 Try asking:**")
suggestions = [
"Summarize the main points",
"What are the key findings?",
"Explain the methodology",
"List the recommendations"
]
for suggestion in suggestions:
if st.button(suggestion, use_container_width=True, type="secondary"):
st.session_state.suggested_question = suggestion
st.rerun()
# Main chat area
st.subheader(f"💬 Chat about: **{st.session_state.current_document}**")
# Display chat history
chat_container = st.container()
with chat_container:
for message in st.session_state.chat_history:
with st.chat_message(message['role']):
# Display message content
st.markdown(message['content'])
# Show timestamp
st.caption(f"*{message['timestamp']}*")
# Show sources for assistant messages if available
if message['role'] == 'assistant' and message.get('sources'):
with st.expander("📚 View sources", expanded=False):
show_sources(message['sources'])
# Chat input at bottom
st.divider()
# Initialize chat input
chat_input_key = f"chat_input_{len(st.session_state.chat_history)}"
# Handle suggested questions
if 'suggested_question' in st.session_state:
question = st.session_state.suggested_question
del st.session_state.suggested_question
else:
# Regular chat input
question = st.chat_input(
"Ask a question about the document...",
key=chat_input_key
)
# Process question
if question:
# Add user message to chat
add_message('user', question)
# Display user message immediately
with st.chat_message("user"):
st.markdown(question)
# Generate assistant response
with st.chat_message("assistant"):
# Create a placeholder for the streaming effect
message_placeholder = st.empty()
# Show initial thinking indicator
message_placeholder.markdown("💭 Thinking...")
# Generate answer
answer, sources = answer_question(
question,
st.session_state.chunks,
st.session_state.index
)
# Update with actual answer
message_placeholder.markdown(answer)
# Add timestamp
st.caption(f"*{datetime.now().strftime('%H:%M')}*")
# Show sources
with st.expander("📚 View sources", expanded=False):
show_sources(sources)
st.caption(f"*Retrieved {len(sources)} relevant sections*")
# Add assistant message to history
add_message('assistant', answer, sources)
# Scroll to bottom (using JavaScript via components)
st.rerun()
else:
# Initial state - no document uploaded
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
st.markdown("""
<div style='text-align: center; padding: 40px;'>
<h1>📚 Document Q&A Assistant</h1>
<p>Upload a PDF document to start chatting with AI about its content</p>
</div>
""", unsafe_allow_html=True)
st.info("👆 Use the sidebar to upload a PDF document")
if __name__ == "__main__":
main()