-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
468 lines (401 loc) · 15.9 KB
/
app.py
File metadata and controls
468 lines (401 loc) · 15.9 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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
import streamlit as st
import os
from dotenv import load_dotenv
import time
# Load environment variables
load_dotenv()
st.set_page_config(
page_title="🎙️ Persona Interview Simulator",
page_icon="🎙️",
layout="centered",
initial_sidebar_state="collapsed"
)
# Custom CSS for chat-like styling
st.markdown("""
<style>
.chat-container {
background-color: #f8f9fa;
border-radius: 10px;
padding: 20px;
margin: 10px 0;
border: 1px solid #e9ecef;
}
.chat-header {
background: linear-gradient(90deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 15px 20px;
border-radius: 10px 10px 0 0;
margin: -20px -20px 20px -20px;
}
.chat-messages {
max-height: 400px;
overflow-y: auto;
padding: 10px;
background: white;
border-radius: 8px;
border: 1px solid #dee2e6;
}
.user-message {
background-color: #007bff;
color: white;
padding: 10px 15px;
border-radius: 18px 18px 4px 18px;
margin: 8px 0;
text-align: right;
max-width: 70%;
margin-left: auto;
}
.bot-message {
background-color: #f8f9fa;
color: #212529;
padding: 10px 15px;
border-radius: 18px 18px 18px 4px;
margin: 8px 0;
text-align: left;
max-width: 70%;
border: 1px solid #dee2e6;
}
.input-container {
background: white;
padding: 15px;
border-radius: 0 0 10px 10px;
border-top: 1px solid #dee2e6;
}
.stButton > button {
border-radius: 20px;
height: 40px;
}
.stTextInput > div > div > input {
border-radius: 20px;
height: 40px;
}
.progress-step {
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 8px;
padding: 15px;
margin: 10px 0;
}
.progress-step.completed {
background: #d4edda;
border-color: #c3e6cb;
}
.progress-step.active {
background: #fff3cd;
border-color: #ffeaa7;
}
</style>
""", unsafe_allow_html=True)
# Check if required environment variables are set
required_vars = ["OPENROUTER_API_KEY", "PINECONE_API_KEY", "COHERE_API_KEY"]
missing_vars = [var for var in required_vars if not os.getenv(var)]
if missing_vars:
st.error(f"❌ Missing required environment variables: {', '.join(missing_vars)}")
st.info("Please create a `.env` file with the following variables:")
st.code(f"""
OPENROUTER_API_KEY=your_openrouter_api_key_here
PINECONE_API_KEY=your_pinecone_api_key_here
COHERE_API_KEY=your_cohere_api_key_here
""")
st.stop()
# Initialize session state
if "qa_chain" not in st.session_state:
st.session_state.qa_chain = None
if "profile" not in st.session_state:
st.session_state.profile = None
if "chat_history" not in st.session_state:
st.session_state.chat_history = []
if "setup_complete" not in st.session_state:
st.session_state.setup_complete = False
if "input_key" not in st.session_state:
st.session_state.input_key = 0
def initialize_rag_system():
"""Initialize the RAG system with simple progress tracking"""
progress_placeholder = st.empty()
# Step 1: Loading and cleaning transcript
with progress_placeholder.container():
st.markdown("""
<div style="text-align: center; padding: 20px;">
<div style="font-size: 24px; margin-bottom: 10px;">⏳</div>
<h4>Loading interview transcript...</h4>
</div>
""", unsafe_allow_html=True)
from langchain_community.document_loaders import PyPDFLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import ChatOpenAI
import json
import textwrap
import re
doc = PyPDFLoader("interview.pdf").load()[0]
clean = doc.page_content.replace("researcher:", "").replace("Interviewee:", "")
time.sleep(0.5) # Small delay for visual feedback
# Step 2: Extracting persona metadata
with progress_placeholder.container():
st.markdown("""
<div style="text-align: center; padding: 20px;">
<div style="font-size: 24px; margin-bottom: 10px;">⏳</div>
<h4>Analyzing interview content...</h4>
</div>
""", unsafe_allow_html=True)
# Configure for OpenRouter
openrouter_key = os.getenv("OPENROUTER_API_KEY")
os.environ["OPENAI_API_KEY"] = openrouter_key
extract_llm = ChatOpenAI(
model_name="gpt-4o-mini",
base_url="https://openrouter.ai/api/v1"
)
EXTRACT_PROMPT = """
You are a data extractor. Read the interview text delimited by <doc>.
Return strict JSON with keys:
name – full name or null
bio – 1-sentence bio (job/age/location if stated) or null
style – 2-3 adjectives describing speaking style or null
<doc>{document}</doc>
"""
meta_json = extract_llm.invoke(EXTRACT_PROMPT.format(document=textwrap.shorten(clean, 12000))).content
# Extract JSON from markdown code blocks if present
json_match = re.search(r'```(?:json)?\s*(\{.*?\})\s*```', meta_json, re.DOTALL)
if json_match:
meta_json = json_match.group(1)
try:
profile = json.loads(meta_json)
except json.JSONDecodeError as e:
print(f"JSON parsing failed: {e}")
profile = {
"name": "Jamie",
"bio": "Interviewee in the provided transcript.",
"style": "neutral"
}
# Fall-backs for missing fields
if not profile.get("name") or profile.get("name") == "null" or profile.get("name") is None:
profile["name"] = "Jamie" # Hardcode Jamie as fallback
profile.setdefault("bio", "Interviewee in the provided transcript.")
profile.setdefault("style", "neutral")
time.sleep(0.5)
# Step 3: Creating text chunks
with progress_placeholder.container():
st.markdown("""
<div style="text-align: center; padding: 20px;">
<div style="font-size: 24px; margin-bottom: 10px;">⏳</div>
<h4>Preparing searchable content...</h4>
</div>
""", unsafe_allow_html=True)
splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=100)
chunks = splitter.create_documents([clean])
time.sleep(0.5)
# Step 4: Creating embeddings with Cohere
with progress_placeholder.container():
st.markdown("""
<div style="text-align: center; padding: 20px;">
<div style="font-size: 24px; margin-bottom: 10px;">⏳</div>
<h4>Creating AI embeddings...</h4>
</div>
""", unsafe_allow_html=True)
from langchain_cohere import CohereEmbeddings
from langchain_pinecone import PineconeVectorStore
from pinecone import Pinecone, ServerlessSpec, CloudProvider, AwsRegion
cohere_api_key = os.getenv("COHERE_API_KEY")
if not cohere_api_key:
raise ValueError("COHERE_API_KEY is required")
embeds = CohereEmbeddings(
model="embed-english-v3.0",
cohere_api_key=cohere_api_key
)
dimension = 1024 # Cohere embed-english-v3.0 dimension
time.sleep(0.5)
# Step 5: Setting up Pinecone database
with progress_placeholder.container():
st.markdown("""
<div style="text-align: center; padding: 20px;">
<div style="font-size: 24px; margin-bottom: 10px;">⏳</div>
<h4>Setting up database...</h4>
</div>
""", unsafe_allow_html=True)
pinecone_api_key = os.getenv("PINECONE_API_KEY")
pc = Pinecone(api_key=pinecone_api_key)
# Create or get Pinecone index
index_name = "persona-pilot"
try:
existing_indexes = pc.list_indexes()
index_exists = index_name in [idx.name for idx in existing_indexes]
if index_exists:
index_description = pc.describe_index(index_name)
existing_dimension = index_description.dimension
if existing_dimension != dimension:
pc.delete_index(index_name)
index_exists = False
if not index_exists:
pc.create_index(
name=index_name,
dimension=dimension,
metric="cosine",
spec=ServerlessSpec(
cloud=CloudProvider.AWS,
region=AwsRegion.US_EAST_1
)
)
# Create vector store
vdb = PineconeVectorStore.from_documents(
documents=chunks,
embedding=embeds,
index_name=index_name
)
except Exception as e:
st.error(f"❌ Pinecone setup failed: {e}")
return False
time.sleep(0.5)
# Step 6: Building the Q&A system
with progress_placeholder.container():
st.markdown("""
<div style="text-align: center; padding: 20px;">
<div style="font-size: 24px; margin-bottom: 10px;">⏳</div>
<h4>Building conversation system...</h4>
</div>
""", unsafe_allow_html=True)
from langchain.prompts import PromptTemplate
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferMemory
retriever = vdb.as_retriever(search_kwargs={"k": 3})
# Create system message with profile information
system_message = f"""
You are {profile['name']}, speaking in first person.
Base every answer on the excerpts provided below. If a topic is missing,
answer with your best reasoning but stay consistent with your past statements;
If unsure, say so. If not found, answer using your best reasoning as {profile['name']}.
Persona bio: {profile['bio']}. Style: {profile['style']}.
Base your answer on the transcript excerpts when relevant.
If the answer is not covered, respond in character as {profile['name']}, using your lived experience and personality.
"""
prompt = PromptTemplate(
input_variables=["context", "chat_history", "question"],
template=system_message + """
Relevant excerpts from the interview:
{context}
Chat History:
{chat_history}
Human: {question}
Assistant:"""
)
memory = ConversationBufferMemory(
memory_key="chat_history",
return_messages=True,
output_key="answer"
)
qa_chain = ConversationalRetrievalChain.from_llm(
llm=ChatOpenAI(
model_name="gpt-4o-mini",
base_url="https://openrouter.ai/api/v1"
),
retriever=retriever,
memory=memory,
combine_docs_chain_kwargs={"prompt": prompt}
)
time.sleep(0.5)
# Final success message
with progress_placeholder.container():
st.markdown("""
<div style="text-align: center; padding: 20px;">
<div style="font-size: 24px; margin-bottom: 10px;">✅</div>
<h4>Ready to chat!</h4>
<p>You can now have a conversation with {name}</p>
</div>
""".format(name=profile['name']), unsafe_allow_html=True)
# Store in session state
st.session_state.qa_chain = qa_chain
st.session_state.profile = profile
st.session_state.setup_complete = True
time.sleep(1)
st.rerun()
# Main app logic
if not st.session_state.setup_complete:
# Show initial setup screen
st.title("🎙️ Persona Interview Simulator")
st.markdown("### Ready to chat with your interview persona?")
st.markdown("Click the button below to initialize the AI system. This will:")
st.markdown("- 📄 Load and process the interview transcript")
st.markdown("- 👤 Extract persona information")
st.markdown("- 🧠 Create embeddings using Cohere AI")
st.markdown("- 🗄️ Set up the vector database")
st.markdown("- 🤖 Build the conversational AI system")
col1, col2, col3 = st.columns([1, 2, 1])
with col2:
if st.button("🚀 Initialize AI System", type="primary", use_container_width=True):
initialize_rag_system()
st.markdown("---")
st.markdown("**Note:** This process may take 30-60 seconds on first run as it sets up the entire RAG system.")
else:
# Show chat interface
profile = st.session_state.profile
qa_chain = st.session_state.qa_chain
# Chat container with header
st.markdown(f"""
<div class="chat-container">
<div class="chat-header">
<h2 style="margin: 0; font-size: 1.5rem;">🎙️ {profile.get('name', 'Unknown')}</h2>
<p style="margin: 5px 0 0 0; opacity: 0.9; font-size: 0.9rem;">{profile.get('bio', 'AI Interview Simulator')}</p>
</div>
""", unsafe_allow_html=True)
# Persona details in a small info box
with st.expander("ℹ️ About this persona", expanded=False):
st.markdown(f"**Name:** {profile.get('name', 'Unknown')}")
st.markdown(f"**Bio:** {profile.get('bio', 'No bio available')}")
st.markdown(f"**Speaking Style:** {profile.get('style', 'Neutral')}")
# Chat messages area
st.markdown('<div class="chat-messages">', unsafe_allow_html=True)
if st.session_state.chat_history:
for i, (role, msg) in enumerate(st.session_state.chat_history):
if role == "You":
st.markdown(f'<div class="user-message"><strong>You:</strong> {msg}</div>', unsafe_allow_html=True)
else:
st.markdown(f'<div class="bot-message"><strong>{profile.get("name", "Interviewee")}:</strong> {msg}</div>', unsafe_allow_html=True)
else:
st.markdown('<div class="bot-message"><em>👋 Hi! I\'m ready to chat. Ask me anything!</em></div>', unsafe_allow_html=True)
st.markdown('</div>', unsafe_allow_html=True)
# Chat input area
st.markdown('<div class="input-container">', unsafe_allow_html=True)
col1, col2 = st.columns([4, 1])
with col1:
user_input = st.text_input(
"Type your message...",
key=f"chat_input_{st.session_state.input_key}",
placeholder="Ask me about my research, experiences, or anything else..."
)
with col2:
send_button = st.button("Send", type="primary", use_container_width=True)
# Handle sending messages
if send_button and user_input:
with st.spinner("🤔 Thinking..."):
try:
response = qa_chain.invoke({"question": user_input})
st.session_state.chat_history.append(("You", user_input))
st.session_state.chat_history.append(("Interviewee", response["answer"]))
st.session_state.input_key += 1 # Increment key to clear input
st.rerun()
except Exception as e:
st.error(f"Error getting response: {str(e)}")
st.markdown('</div>', unsafe_allow_html=True)
# Clear chat button (outside the chat container)
st.markdown('</div>', unsafe_allow_html=True) # Close the chat container
col1, col2, col3 = st.columns([1, 1, 1])
with col1:
if st.button("🗑️ Clear Chat", use_container_width=True):
st.session_state.chat_history = []
st.session_state.input_key += 1 # Reset input key to clear field
st.rerun()
with col2:
# Download Jamie's transcript
with open("interview.pdf", "rb") as pdf_file:
st.download_button(
label="📄 Download Jamie's Transcript",
data=pdf_file.read(),
file_name="Jamie_Interview_Transcript.pdf",
mime="application/pdf",
use_container_width=True
)
with col3:
if st.button("🔄 Reset System", use_container_width=True):
st.session_state.qa_chain = None
st.session_state.profile = None
st.session_state.chat_history = []
st.session_state.setup_complete = False
st.rerun()