-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
566 lines (467 loc) · 22.5 KB
/
app.py
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
import streamlit as st
from fpdf import FPDF
from langchain.chains import create_history_aware_retriever, create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_chroma import Chroma
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.chat_history import BaseChatMessageHistory
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain_groq import ChatGroq
from langchain_core.runnables.history import RunnableWithMessageHistory
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import PyPDFLoader
from google.oauth2.credentials import Credentials
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from datetime import datetime, time
from email.mime.text import MIMEText
import base64
import json
import os
import requests
import speech_recognition as sr
import moviepy.editor as mp
import tempfile
import os
from pydub import AudioSegment
import io
import whisper
import speech_recognition as sr
from dotenv import load_dotenv
load_dotenv()
# Function to handle meeting agenda creation
def page_agenda():
st.title("Meeting Agenda")
# Set up environment variables and API keys
os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN')
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
# Initialize embeddings and language model
embeddings=HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
# Setup Streamlit
st.write("Upload Documents Related to the meeting")
# Check if api key is provided
llm=ChatGroq(groq_api_key=GROQ_API_KEY, model_name="Gemma2-9b-It")
##chat interface
session_id=st.sidebar.text_input("Session ID", value="default_session")
if 'store' not in st.session_state:
st.session_state.store={}
uploaded_files = st.file_uploader("Upload your Documents", type=["pdf"], accept_multiple_files=True)
## Process Uploaded files
if uploaded_files:
# Load and process documents
documents=[]
for uploaded_file in uploaded_files:
# Save uploaded file temporarily
temppdf=f"./temp.pdf"
with open(temppdf, "wb") as file:
file.write(uploaded_file.getvalue())
file_name=uploaded_file.name
# Save uploaded file temporarily
loader=PyPDFLoader(temppdf)
docs=loader.load()
documents.extend(docs)
#Split and create embeddings for the documents
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=200)
splits = text_splitter.split_documents(documents)
vectorstore = Chroma.from_documents(documents=splits,embedding = embeddings)
retriever = vectorstore.as_retriever()
contextualize_q_system_prompt=(
"You are an expert meeting facilitator. Your task is to create a comprehensive and effective meeting agenda based on the provided documents and any additional input from the user. Consider the following:"
"1. The overall purpose and desired outcomes of the meeting"
"2. The key topics that need to be addressed"
"3. The appropriate time allocation for each agenda item"
"4. Any pre-meeting preparation required for attendees"
"5. Opportunities for participant engagement and discussion"
"6. A clear structure that flows logically from one topic to the next"
"Create a well-structured, time-bound agenda that will ensure a productive and focused meeting."
)
# Set up prompts and chains for agenda creation
contextualize_q_prompt= ChatPromptTemplate.from_messages(
[
("system",contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human","{input}")
]
)
history_aware_retriever=create_history_aware_retriever(llm,retriever,contextualize_q_prompt)
# Set up system prompt and QA chain
system_prompt = (
"Your are a meeting manager"
"Your aim is to create agendas for current meeting from the uploaded file"
"\n\n"
"{context}"
)
qa_prompt = ChatPromptTemplate.from_messages(
[
("system" , system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}")
]
)
question_answer_chain=create_stuff_documents_chain(llm,qa_prompt)
rag_chain=create_retrieval_chain(history_aware_retriever,question_answer_chain)
# Function to get or create session history
def get_session_history(session:str)->BaseChatMessageHistory:
if session_id not in st.session_state.store:
st.session_state.store[session_id]=ChatMessageHistory()
return st.session_state.store[session_id]
# Set up conversational RAG chain
conversational_rag_chain=RunnableWithMessageHistory(
rag_chain,get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer"
)
# Generate meeting agenda
user_input = "Create a point wise structured agenda for the meeting"
if user_input:
if 'response' not in st.session_state:
session_history=get_session_history(session_id)
response = conversational_rag_chain.invoke(
{"input": user_input},
config={
"configurable": {"session_id":session_id}
},
)
st.session_state.response = response # Store the response in session state
st.session_state.history = session_history.messages
# Display generated agenda
st.title("Meeting Agenda")
st.write("Assistant:", st.session_state.response['answer'])
# Provide option to download agenda as PDF
if st.button("Download AI Response as PDF"):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
# Add content
pdf.cell(200, 10, txt="AI Meeting Manager Response", ln=True, align="C")
pdf.ln(10) # New line
# AI response
pdf.multi_cell(0, 10, txt="Assistant's Response:\n" + st.session_state.response['answer'])
# Save the PDF
pdf_path = os.path.join(os.getcwd(),'data' ,'Agenda.pdf')
pdf.output(pdf_path)
with open(pdf_path, "rb") as file:
st.download_button(
label="Download PDF",
data=file,
file_name=f"Agenda.pdf",
mime="application/pdf"
)
# Function to handle meeting video upload and transcription
def page_upload():
st.title("Upload Meeting")
# Function to convert video to audio
def video_to_audio(video_file):
with tempfile.NamedTemporaryFile(delete=False, suffix='.mp4') as tmpfile:
tmpfile.write(video_file.read())
video_path = tmpfile.name
current_dir=os.getcwd()
audio_path=os.path.join(current_dir,'temp','audio.wav')
try:
video = mp.VideoFileClip(video_path)
video.audio.write_audiofile(audio_path, codec='pcm_s16le') # Specify codec
video.close()
except Exception as e:
st.error(f"Error converting video to audio: {str(e)}")
return None
finally:
if 'video' in locals():
video.close()
for attempt in range(5):
try:
os.unlink(video_path)
break
except PermissionError:
time.sleep(1)
return audio_path
# Function to transcribe audio
def transcribe_audio(audio_file_path):
try:
model=sr.Recognizer()
with sr.AudioFile(audio_file_path) as source:
audio = model.listen(source)
transcript = model.recognize_whisper(audio)
except Exception as e:
st.error(f"An unexpected error occurred: {str(e)}")
return transcript
# Function to save transcript as PDF
def save_transcript_as_pdf(transcript,save_path):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size = 12)
pdf.multi_cell(0, 10, transcript)
pdf.output(save_path)
# Set up file uploader for video
uploaded_file = st.file_uploader("Choose a video file", type=["mp4", "avi", "mov"])
if uploaded_file is not None:
st.video(uploaded_file)
# Transcribe video when button is clicked
if st.button("Transcribe"):
with st.spinner("Processing..."):
try:
audio_file_path = video_to_audio(uploaded_file)
transcript = transcribe_audio(audio_file_path)
st.success("Transcription Complete!")
pdf_path = os.path.join(os.getcwd(),'data' ,'Transcript.pdf')
pdf_output = save_transcript_as_pdf(transcript,pdf_path)
# Provide option to download transcript as PDF
with open(pdf_path, "rb") as pdf_file:
pdf_bytes = pdf_file.read()
st.download_button(
label="Download Transcript as PDF",
data=pdf_bytes,
file_name="transcript.pdf",
mime="application/pdf"
)
except Exception as e:
st.error(f"An unexpected error occurred: {str(e)}")
def page_track():
st.title("Meeting Tracking") # Set the page title
# Set API keys from environment variables
os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN')
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
# Load HuggingFace embeddings model for text embedding
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
# Initialize the LLM with API key for the model "Gemma2-9b-It"
llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="Gemma2-9b-It")
# User input for session ID (default value is "default_session")
session_id = st.sidebar.text_input("Session ID", value="default_session")
# Initialize session state to store information if it doesn't already exist
if 'store' not in st.session_state:
st.session_state.store = {}
# Upload file handler, accepting PDF files
uploaded_files = st.file_uploader("Upload your Documents", type=["pdf"], accept_multiple_files=True)
# If files are uploaded, process them
if uploaded_files:
documents = []
for uploaded_file in uploaded_files:
temppdf = f"./temp.pdf"
# Save uploaded PDF file locally
with open(temppdf, "wb") as file:
file.write(uploaded_file.getvalue())
file_name = uploaded_file.name
documents = []
# Load the content of the uploaded PDF file
loader = PyPDFLoader(temppdf)
docs = loader.load()
documents.extend(docs)
# Split the documents into chunks for embedding
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=200)
splits = text_splitter.split_documents(documents)
# Store document embeddings in Chroma vectorstore
vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
retriever = vectorstore.as_retriever()
# Define a system prompt to contextualize the question
contextualize_q_system_prompt = (
"What is the tracking of meeting"
)
# Create prompt template for conversation history
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}")
]
)
# Create a history-aware retriever
history_aware_retriever = create_history_aware_retriever(llm, retriever, contextualize_q_prompt)
# Define the system prompt for meeting analysis
system_prompt = (
"You are an AI meeting analyst. Your task is to compare the original meeting agenda with the actual meeting transcript and provide a detailed analysis"
"\n\n"
"{context}"
)
# Create prompt template for answering questions
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}")
]
)
# Chain LLM and document retriever for question-answering
question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
# Function to retrieve session-specific chat history
def get_session_history(session: str) -> BaseChatMessageHistory:
if session_id not in st.session_state.store:
st.session_state.store[session_id] = ChatMessageHistory()
return st.session_state.store[session_id]
# Define a chain to handle conversation with history
conversational_rag_chain = RunnableWithMessageHistory(
rag_chain, get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer"
)
# Example user input to analyze meeting flow
user_input = "Analyze the meeting flow and effectiveness based on the agenda and transcript."
if user_input:
# If no previous response, run RAG model to generate response
if 'response' not in st.session_state:
session_history = get_session_history(session_id)
response = conversational_rag_chain.invoke(
{"input": user_input},
config={
"configurable": {"session_id": session_id}
},
)
st.session_state.response = response # Store the response in session state
st.session_state.history = session_history.messages
# Display AI response for meeting flow
st.title("Meeting Flow")
st.write("Assistant:", st.session_state.response['answer'])
# Option to download the AI-generated response as a PDF
if st.button("Download AI Response as PDF"):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
# Add content to the PDF
pdf.cell(200, 10, txt="AI Meeting Manager Response", ln=True, align="C")
pdf.ln(10) # New line
# Write the AI response in the PDF
pdf.multi_cell(0, 10, txt="Assistant's Response:\n" + st.session_state.response['answer'])
# Save the PDF
pdf_path = os.path.join(os.getcwd(), 'data', 'Track.pdf')
pdf.output(pdf_path)
# Provide download link for the PDF
with open(pdf_path, "rb") as file:
st.download_button(
label="Download PDF",
data=file,
file_name=f"Track.pdf",
mime="application/pdf"
)
def page_summary():
st.title("Meeting Summary") # Set the page title
# Set API keys from environment variables
os.environ['HF_TOKEN'] = os.getenv('HF_TOKEN')
GROQ_API_KEY = os.getenv('GROQ_API_KEY')
# Load HuggingFace embeddings model for text embedding
embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2")
# Write introductory text for file upload
st.write("Upload Documents Related to the meeting")
# Initialize the LLM with API key for the model "Gemma2-9b-It"
llm = ChatGroq(groq_api_key=GROQ_API_KEY, model_name="Gemma2-9b-It")
# User input for session ID (default value is "default_session")
session_id = st.sidebar.text_input("Session ID", value="default_session")
# Initialize session state to store information if it doesn't already exist
if 'store' not in st.session_state:
st.session_state.store = {}
# Upload file handler, accepting PDF files
uploaded_files = st.file_uploader("Upload your Documents", type=["pdf"], accept_multiple_files=True)
# If files are uploaded, process them
if uploaded_files:
documents = []
for uploaded_file in uploaded_files:
temppdf = f"./temp.pdf"
# Save uploaded PDF file locally
with open(temppdf, "wb") as file:
file.write(uploaded_file.getvalue())
file_name = uploaded_file.name
documents = []
# Load the content of the uploaded PDF file
loader = PyPDFLoader(temppdf)
docs = loader.load()
documents.extend(docs)
# Split the documents into chunks for embedding
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=200)
splits = text_splitter.split_documents(documents)
# Store document embeddings in Chroma vectorstore
vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
retriever = vectorstore.as_retriever()
# Define a system prompt to contextualize the question
contextualize_q_system_prompt = (
"What is the summary of meeting"
)
# Create prompt template for conversation history
contextualize_q_prompt = ChatPromptTemplate.from_messages(
[
("system", contextualize_q_system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}")
]
)
# Create a history-aware retriever
history_aware_retriever = create_history_aware_retriever(llm, retriever, contextualize_q_prompt)
# Define the system prompt for meeting summary
system_prompt = (
"As an AI meeting summarizer, your task is to create a concise yet comprehensive summary of the entire meeting"
"\n\n"
"{context}"
)
# Create prompt template for answering questions
qa_prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
MessagesPlaceholder("chat_history"),
("human", "{input}")
]
)
# Chain LLM and document retriever for question-answering
question_answer_chain = create_stuff_documents_chain(llm, qa_prompt)
rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
# Function to retrieve session-specific chat history
def get_session_history(session: str) -> BaseChatMessageHistory:
if session_id not in st.session_state.store:
st.session_state.store[session_id] = ChatMessageHistory()
return st.session_state.store[session_id]
# Define a chain to handle conversation with history
conversational_rag_chain = RunnableWithMessageHistory(
rag_chain, get_session_history,
input_messages_key="input",
history_messages_key="chat_history",
output_messages_key="answer"
)
# Example user input to generate meeting summary
user_input = "Provide a comprehensive summary of the meeting based on the analysis."
if user_input:
# If no previous response, run RAG model to generate response
if 'response' not in st.session_state:
session_history = get_session_history(session_id)
response = conversational_rag_chain.invoke(
{"input": user_input},
config={
"configurable": {"session_id": session_id}
},
)
st.session_state.response = response # Store the response in session state
st.session_state.history = session_history.messages
# Display AI response for meeting summary
st.title("Meeting Summary")
st.write("Assistant:", st.session_state.response['answer'])
# Option to download the AI-generated summary as a PDF
if st.button("Download AI Response as PDF"):
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
# Add content to the PDF
pdf.cell(200, 10, txt="AI Meeting Manager Response", ln=True, align="C")
pdf.ln(10) # New line
# Write the AI response in the PDF
pdf.multi_cell(0, 10, txt="Assistant's Response:\n" + st.session_state.response['answer'])
# Save the PDF
pdf_path = os.path.join(os.getcwd(), 'data', 'Summary.pdf')
pdf.output(pdf_path)
# Provide download link for the PDF
with open(pdf_path, "rb") as file:
st.download_button(
label="Download PDF",
data=file,
file_name=f"Summary.pdf",
mime="application/pdf"
)
# Sidebar navigation to switch between pages
st.sidebar.title("Navigation")
page = st.sidebar.radio("Go to", ["Meeting Agenda", "Upload Meeting", "Meeting Tracking", "Meeting Summary"])
# Display the selected page
if page == "Meeting Agenda":
page_agenda()
elif page == "Upload Meeting":
page_upload()
elif page == "Meeting Tracking":
page_track()
elif page == "Meeting Summary":
page_summary()