itzbhav commited on
Commit
65fcf22
·
verified ·
1 Parent(s): 748b6ee

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +156 -0
app.py ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import json
3
+ import os
4
+ from dataclasses import dataclass
5
+ from typing import Literal
6
+ from langchain_community.document_loaders import PyPDFLoader
7
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
8
+ from langchain_community.embeddings import SentenceTransformerEmbeddings
9
+ from langchain_community.vectorstores import Chroma
10
+ from langchain_core.prompts import ChatPromptTemplate
11
+ from langchain_community.llms import Ollama
12
+ from langchain.chains.combine_documents import create_stuff_documents_chain
13
+ from langchain.chains import create_retrieval_chain
14
+ from langchain_core.output_parsers import StrOutputParser
15
+ import streamlit.components.v1 as components
16
+ import langchain_ollama
17
+ import langchain_huggingface
18
+ # Initialize the LLM (make sure the model is correct)
19
+ llm = Ollama(model="llama3")
20
+
21
+ # Load the Coimbatore-related PDF for personalized recommendations
22
+ loader = PyPDFLoader("Transcript.pdf")
23
+ docs = loader.load()
24
+
25
+ # Split the document into chunks
26
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=100)
27
+ documents = text_splitter.split_documents(docs)
28
+
29
+ # Create embeddings and vector database
30
+ embeddings = SentenceTransformerEmbeddings(model_name="all-MiniLM-L6-v2")
31
+ db = Chroma.from_documents(documents, embeddings)
32
+
33
+ # Determine whether a query is related to Coimbatore
34
+ def is_coimbatore_query(query):
35
+ keywords = ["meeting", "minutes", "transcript", "discussion", "virtual"]
36
+ return any(k in query.lower() for k in keywords)
37
+
38
+ # Chat history file
39
+ HISTORY_FILE = "chat_history.json"
40
+
41
+ @dataclass
42
+ class Message:
43
+ origin: Literal["human", "ai"]
44
+ message: str
45
+
46
+ def load_chat_history():
47
+ if os.path.exists(HISTORY_FILE):
48
+ with open(HISTORY_FILE, "r") as f:
49
+ return json.load(f)
50
+ return []
51
+
52
+ def save_chat_history(history):
53
+ history_to_save = []
54
+ for msg in history:
55
+ if isinstance(msg['message'], dict):
56
+ msg['message'] = str(msg['message'])
57
+ history_to_save.append(msg)
58
+ with open(HISTORY_FILE, "w") as f:
59
+ json.dump(history_to_save, f)
60
+
61
+ def clear_chat_history():
62
+ if os.path.exists(HISTORY_FILE):
63
+ os.remove(HISTORY_FILE)
64
+
65
+ def initialize_session_state():
66
+ if "history" not in st.session_state:
67
+ st.session_state.history = load_chat_history()
68
+
69
+ if "conversation_chain" not in st.session_state:
70
+ coimbatore_prompt = """You are an intelligent meeting assistant called RUBY for helping employees and clarify their queries regarding the virtual meet.
71
+ Use the following pieces of retrieved context to answer the question in detail: {context}.
72
+ Greet if the user greets you.
73
+ If you don't know the answer, just say that you don't know.
74
+ Only answer relevant content and not anything extra.
75
+ Don't return the prompt in the answer.
76
+ Don't respond irrelevant or anything outside the context.
77
+ ___________
78
+ {context}"""
79
+ prompt = ChatPromptTemplate.from_messages([
80
+ ("system", coimbatore_prompt),
81
+ ("human", "{input}"),
82
+ ])
83
+ question_answer_chain = create_stuff_documents_chain(llm, prompt)
84
+ retriever = db.as_retriever()
85
+ rag_chain = create_retrieval_chain(retriever, question_answer_chain)
86
+ st.session_state.retrieval_chain = rag_chain
87
+
88
+ general_prompt = ChatPromptTemplate.from_messages([
89
+ ("system", "You are a highly knowledgeable virtual meeting assistant named RUBY. You help users summarize the meeting, provide semantic analysis, clear doubts, answer who said what, classify opening and closing statements, and identify takeaway points. Always ask follow-up questions."),
90
+ ("user", "Question: {input}")
91
+ ])
92
+ st.session_state.general_chain = general_prompt | llm | StrOutputParser()
93
+
94
+ # Handle user input and update chat
95
+ def on_click_callback():
96
+ user_input = st.session_state.user_input
97
+ context = "\n".join([f"{msg['origin']}: {msg['message']}" for msg in st.session_state.history])
98
+
99
+ if is_coimbatore_query(user_input):
100
+ response = st.session_state.retrieval_chain.invoke({"input": context + "\n" + user_input})
101
+ answer = response['answer']
102
+ else:
103
+ response = st.session_state.general_chain.invoke({"input": context + "\n" + user_input})
104
+ answer = response
105
+
106
+ st.session_state.history.append({"origin": "human", "message": user_input})
107
+ st.session_state.history.append({"origin": "ai", "message": answer})
108
+ save_chat_history(st.session_state.history)
109
+
110
+ # New chat button
111
+ if st.sidebar.button("New Chat"):
112
+ st.session_state.history = []
113
+ clear_chat_history()
114
+
115
+ # Initialize session
116
+ initialize_session_state()
117
+
118
+
119
+
120
+
121
+ st.title("RUBY, INTELLIGENT MEETING BOT 🤖")
122
+
123
+ # Display chat history
124
+ chat_placeholder = st.container()
125
+ prompt_placeholder = st.form("chat-form")
126
+
127
+ with chat_placeholder:
128
+ for chat in st.session_state.history:
129
+ div = f"""
130
+ <div class="chat-row {'row-reverse' if chat['origin'] == 'human' else ''}">
131
+ <div class="chat-bubble {'human-bubble' if chat['origin'] == 'human' else 'ai-bubble'}">
132
+ {chat['message']}
133
+ </div>
134
+ </div>
135
+ """
136
+ st.markdown(div, unsafe_allow_html=True)
137
+
138
+ with prompt_placeholder:
139
+ st.markdown("*Ask RUBY about your meeting !*")
140
+ cols = st.columns((6, 1))
141
+ cols[0].text_input("Chat", key="user_input", label_visibility="collapsed")
142
+ cols[1].form_submit_button("Submit", on_click=on_click_callback)
143
+
144
+ # Press "Enter" to submit input
145
+ components.html("""
146
+ <script>
147
+ const streamlitDoc = window.parent.document;
148
+ const buttons = Array.from(streamlitDoc.querySelectorAll('.stButton > button'));
149
+ const submitButton = buttons.find(el => el.innerText === 'Submit');
150
+ streamlitDoc.addEventListener('keydown', function(e) {
151
+ if (e.key === 'Enter') {
152
+ submitButton.click();
153
+ }
154
+ });
155
+ </script>
156
+ """, height=0, width=0)