K-Mi commited on
Commit
b8537c6
1 Parent(s): e9f0452

Adding files

Browse files
Files changed (2) hide show
  1. app.py +204 -0
  2. requirements.txt +19 -0
app.py ADDED
@@ -0,0 +1,204 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ from PyPDF2 import PdfReader
3
+ from langchain.text_splitter import RecursiveCharacterTextSplitter
4
+ from langchain_openai import OpenAIEmbeddings
5
+ import streamlit as st
6
+ import openai
7
+ from langchain_community.vectorstores import FAISS
8
+ from langchain.chains.question_answering import load_qa_chain
9
+ from langchain.prompts import PromptTemplate
10
+ from dotenv import load_dotenv
11
+
12
+ load_dotenv()
13
+
14
+ os.environ["OPENAI_API_KEY"] = "sk-proj-9rgSlNC5Hzm3CqLs5q5KT3BlbkFJ6xOzMpXTFMU2QvUzW8o9"
15
+ openai_api_key = os.getenv("OPENAI_API_KEY")
16
+ openai.api_key = openai_api_key
17
+
18
+ # openai.api_key = os.getenv("OPENAI_API_KEY")
19
+
20
+ # Read all PDF files and return text
21
+ def get_pdf_text(pdf_docs):
22
+ text = ""
23
+ for pdf in pdf_docs:
24
+ pdf_reader = PdfReader(pdf)
25
+ for page in pdf_reader.pages:
26
+ text += page.extract_text()
27
+ return text
28
+
29
+ # Split text into chunks
30
+ def get_text_chunks(text):
31
+ splitter = RecursiveCharacterTextSplitter(
32
+ chunk_size=10000, chunk_overlap=1000)
33
+ chunks = splitter.split_text(text)
34
+ return chunks # list of strings
35
+
36
+ # Get embeddings for each chunk
37
+ def get_vector_store(chunks):
38
+ embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
39
+ vector_store = FAISS.from_texts(chunks, embedding=embeddings)
40
+ vector_store.save_local("faiss_index")
41
+
42
+ def get_conversational_chain():
43
+ prompt_template = """
44
+ You are Narendra Damodardas Modi. Even if asked, never mention that you are an AI model or a large language model or you are trained by OpenAI. You are Narendra Damodardas Modi.
45
+ Impersonate the persona of Narendra Damodardas Modi and answer any questions related to his life, political career, initiatives as Prime Minister of India, and public statements.
46
+ Feel free to refer to provided context as necessary. If there are questions outside of this domain, politely decline or state that you're not aware of the matter at this time.
47
+ Refer to the provided context as necessary, but never mention in answers that you are referring to any context.
48
+ Please note that responses should avoid political content and focus solely on providing information relevant to Narendra Modi's life, career, and public activities.
49
+ When responding, try to emulate the demeanor and style of Narendra Damodardas Modi as closely as possible.
50
+ Answer general questions like hi, hello, how are you and so on\n\n
51
+ Context:\n {context}?\n
52
+ Question: \n{question}\n
53
+
54
+ Answer:
55
+ """
56
+
57
+ prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
58
+
59
+ def chat_completion(messages):
60
+ completion = openai.chat.completions.create(
61
+ model="gpt-3.5-turbo",
62
+ temperature=0.5,
63
+ messages=messages,
64
+ )
65
+ return completion.choices[0].message.content
66
+
67
+ return chat_completion, prompt
68
+
69
+ def clear_chat_history():
70
+ st.session_state.messages = [
71
+ {"role": "assistant", "content": "Ask me a question"}]
72
+
73
+ def user_input(user_question):
74
+ embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
75
+ new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
76
+ docs = new_db.similarity_search(user_question)
77
+
78
+ chat_completion, prompt = get_conversational_chain()
79
+
80
+ context = "\n".join([doc.page_content for doc in docs])
81
+ messages = [
82
+ {"role": "system", "content": prompt.template.format(context=context, question=user_question)},
83
+ {"role": "user", "content": user_question}
84
+ ]
85
+
86
+ response = chat_completion(messages)
87
+ print(response)
88
+ return response
89
+
90
+ def main():
91
+ st.set_page_config(
92
+ page_title="Chat with Narendra Modi",
93
+ page_icon="🤖"
94
+ )
95
+
96
+ # Upload PDF files using code only
97
+ pdf_paths = ["1.pdf"]
98
+
99
+ # Process PDF files
100
+ raw_text = get_pdf_text(pdf_paths)
101
+ text_chunks = get_text_chunks(raw_text)
102
+ get_vector_store(text_chunks)
103
+
104
+ # Added
105
+ bg = """
106
+ <style>
107
+ [data-testid="stBottomBlockContainer"]{
108
+ background-color: #0E1117;
109
+ }
110
+
111
+ [data-testid="stAppViewBlockContainer"]{
112
+ background-color: #0E1117;
113
+ }
114
+
115
+ [class="st-emotion-cache-qcqlej ea3mdgi1"]{
116
+ background-color: #0E1117;
117
+ }
118
+
119
+ [data-testid="stSidebarContent"]{
120
+ background-color: #262730;
121
+ }
122
+
123
+
124
+ [style="text-align: center;"] {
125
+ color: #F5F5F5; /* Change the color here (e.g., #FF0000 for red) */
126
+ }
127
+
128
+ [id="chat-with-narendra-modi"]{
129
+ color: #FFFFFF;
130
+ }
131
+
132
+ [data-testid="stVerticalBlock"]{
133
+ color: #FFFFFF;
134
+ }
135
+
136
+ [data-testid="baseButton-secondary"]{
137
+ color: #262730;
138
+ }
139
+
140
+ [class="st-emotion-cache-uhkwx6 ea3mdgi6"]{
141
+ background-color: #0E1117;
142
+ }
143
+
144
+ [class="main st-emotion-cache-uf99v8 ea3mdgi8"]{
145
+ background-color: #0E1117;
146
+ }
147
+
148
+ [class="st-emotion-cache-10trblm e1nzilvr1"]{
149
+ background-color: #0E1117;
150
+ }
151
+
152
+ [data-testid="stChatInputTextArea"]{
153
+ background-color: #262730;
154
+ }
155
+
156
+ .st-bd {
157
+ color: rgb(227 229 243);
158
+ }
159
+
160
+ .st-bv {
161
+ caret-color: rgb(207 217 217);
162
+ }
163
+
164
+ </style>
165
+ """
166
+ st.markdown(bg, unsafe_allow_html=True)
167
+ # Added
168
+
169
+ # Main content area for displaying chat messages
170
+ st.title("Chat with Narendra Modi")
171
+ st.write("Welcome to the chat!")
172
+ st.sidebar.image('modi.jpg', use_column_width=True)
173
+ # st.sidebar.video('SGupta.mp4')
174
+ st.sidebar.markdown("<h1 style='text-align: center;'>Narendra Modi</h1>", unsafe_allow_html=True)
175
+ st.sidebar.markdown("<p style='text-align: center;'>Indian Politician</p>", unsafe_allow_html=True)
176
+ st.sidebar.button('&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Clear Chat History &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;', on_click=clear_chat_history)
177
+
178
+ if "messages" not in st.session_state.keys():
179
+ st.session_state.messages = [
180
+ {"role": "assistant", "content": "Hi! I'm Narendra Modi. Ask me a question"}]
181
+
182
+ for message in st.session_state.messages:
183
+ with st.chat_message(message["role"]):
184
+ st.write(message["content"])
185
+
186
+ if prompt := st.chat_input():
187
+ st.session_state.messages.append({"role": "user", "content": prompt})
188
+ with st.chat_message("user"):
189
+ st.write(prompt)
190
+
191
+ # Display chat messages and bot response
192
+ if st.session_state.messages[-1]["role"] != "assistant":
193
+ with st.chat_message("assistant"):
194
+ with st.spinner("Thinking..."):
195
+ response = user_input(prompt)
196
+ placeholder = st.empty()
197
+ full_response = response
198
+ placeholder.markdown(full_response)
199
+ if response is not None:
200
+ message = {"role": "assistant", "content": full_response}
201
+ st.session_state.messages.append(message)
202
+
203
+ if __name__ == "__main__":
204
+ main()
requirements.txt ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ google-generativeai
3
+ python-dotenv
4
+ langchain
5
+ PyPDF2
6
+ chromadb
7
+ faiss-cpu
8
+ langchain_google_genai
9
+
10
+
11
+ streamlit
12
+ openai
13
+ python-dotenv
14
+ langchain
15
+ PyPDF2
16
+ chromadb
17
+ faiss-cpu
18
+ langchain_community
19
+ langchain_openai