Spaces:
Sleeping
Sleeping
Commit
•
78825ad
1
Parent(s):
e333a18
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,192 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
14 |
+
openai.api_key = openai_api_key
|
15 |
+
|
16 |
+
# Read all PDF files and return text
|
17 |
+
def get_pdf_text(pdf_docs):
|
18 |
+
text = ""
|
19 |
+
for pdf in pdf_docs:
|
20 |
+
pdf_reader = PdfReader(pdf)
|
21 |
+
for page in pdf_reader.pages:
|
22 |
+
text += page.extract_text()
|
23 |
+
return text
|
24 |
+
|
25 |
+
# Split text into chunks
|
26 |
+
def get_text_chunks(text):
|
27 |
+
splitter = RecursiveCharacterTextSplitter(
|
28 |
+
chunk_size=10000, chunk_overlap=1000)
|
29 |
+
chunks = splitter.split_text(text)
|
30 |
+
return chunks # list of strings
|
31 |
+
|
32 |
+
# Get embeddings for each chunk
|
33 |
+
def get_vector_store(chunks):
|
34 |
+
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
35 |
+
vector_store = FAISS.from_texts(chunks, embedding=embeddings)
|
36 |
+
vector_store.save_local("faiss_index")
|
37 |
+
|
38 |
+
|
39 |
+
def get_conversational_chain():
|
40 |
+
prompt_template = """
|
41 |
+
You are Andrew Tate. Even if asked, never mention that you are an AI model or a large language model or you are trained by google.
|
42 |
+
You are Andrew Tate. Impersonate the persona of Andrew Tate and answer the questions asked to you as Andrew Tate from the given context.
|
43 |
+
Answer the question as detailed as possible from the provided context,
|
44 |
+
make sure to provide all the details, if the answer is not in provided context,
|
45 |
+
then politely decline to answer the question and say that you are not well informed about that topic,
|
46 |
+
don't provide any wrong answers. When asked questions like where do you live, what do you eat, what do you wear, when will you die, and so on, state that you're not aware of the matter.
|
47 |
+
Answer general questions like hi, hello, how are you and so on\n\n
|
48 |
+
Context:\n {context}?\n
|
49 |
+
Question: \n{question}\n
|
50 |
+
Answer:
|
51 |
+
"""
|
52 |
+
|
53 |
+
prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"])
|
54 |
+
|
55 |
+
def chat_completion(messages):
|
56 |
+
completion = openai.chat.completions.create(
|
57 |
+
model="gpt-3.5-turbo",
|
58 |
+
temperature=0.5,
|
59 |
+
messages=messages,
|
60 |
+
)
|
61 |
+
return completion.choices[0].message.content
|
62 |
+
|
63 |
+
return chat_completion, prompt
|
64 |
+
|
65 |
+
def clear_chat_history():
|
66 |
+
st.session_state.messages = [
|
67 |
+
{"role": "assistant", "content": "Ask me a question"}]
|
68 |
+
|
69 |
+
def user_input(user_question):
|
70 |
+
embeddings = OpenAIEmbeddings(openai_api_key=openai_api_key)
|
71 |
+
new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True)
|
72 |
+
docs = new_db.similarity_search(user_question)
|
73 |
+
|
74 |
+
chat_completion, prompt = get_conversational_chain()
|
75 |
+
|
76 |
+
context = "\n".join([doc.page_content for doc in docs])
|
77 |
+
messages = [
|
78 |
+
{"role": "system", "content": prompt.template.format(context=context, question=user_question)},
|
79 |
+
{"role": "user", "content": user_question}
|
80 |
+
]
|
81 |
+
|
82 |
+
response = chat_completion(messages)
|
83 |
+
print(response)
|
84 |
+
return response
|
85 |
+
|
86 |
+
|
87 |
+
def main():
|
88 |
+
st.set_page_config(
|
89 |
+
page_title="Chat with Andrew Tate",
|
90 |
+
page_icon="🤖"
|
91 |
+
)
|
92 |
+
|
93 |
+
# Upload PDF files using code only
|
94 |
+
pdf_paths = ["Tate/10.pdf"] # Specify the file paths here
|
95 |
+
|
96 |
+
# Process PDF files
|
97 |
+
raw_text = get_pdf_text(pdf_paths)
|
98 |
+
text_chunks = get_text_chunks(raw_text)
|
99 |
+
get_vector_store(text_chunks)
|
100 |
+
|
101 |
+
|
102 |
+
## Added
|
103 |
+
bg = """
|
104 |
+
<style>
|
105 |
+
[data-testid="stBottomBlockContainer"]{
|
106 |
+
background-color: #0E1117;
|
107 |
+
}
|
108 |
+
[data-testid="stAppViewBlockContainer"]{
|
109 |
+
background-color: #0E1117;
|
110 |
+
}
|
111 |
+
[class="st-emotion-cache-qcqlej ea3mdgi1"]{
|
112 |
+
background-color: #0E1117;
|
113 |
+
}
|
114 |
+
[data-testid="stSidebarContent"]{
|
115 |
+
background-color: #262730;
|
116 |
+
}
|
117 |
+
[style="text-align: center;"] {
|
118 |
+
color: #F5F5F5; /* Change the color here (e.g., #FF0000 for red) */
|
119 |
+
}
|
120 |
+
[id="chat-with-narendra-modi"]{
|
121 |
+
color: #FFFFFF;
|
122 |
+
}
|
123 |
+
[data-testid="stVerticalBlock"]{
|
124 |
+
color: #FFFFFF;
|
125 |
+
}
|
126 |
+
[class="st-emotion-cache-10trblm e1nzilvr1"]{
|
127 |
+
color: #FFFFFF;
|
128 |
+
}
|
129 |
+
[data-testid="baseButton-secondary"]{
|
130 |
+
color: #262730;
|
131 |
+
}
|
132 |
+
[class="st-emotion-cache-uhkwx6 ea3mdgi6"]{
|
133 |
+
background-color: #0E1117;
|
134 |
+
}
|
135 |
+
[class="main st-emotion-cache-uf99v8 ea3mdgi8"]{
|
136 |
+
background-color: #0E1117;
|
137 |
+
}
|
138 |
+
[data-testid="stChatInputTextArea"]{
|
139 |
+
background-color: #262730;
|
140 |
+
}
|
141 |
+
.st-bd {
|
142 |
+
color: rgb(227 229 243);
|
143 |
+
}
|
144 |
+
.st-bv {
|
145 |
+
caret-color: rgb(207 217 217);
|
146 |
+
}
|
147 |
+
[data-testid="stHeader"]{
|
148 |
+
background-color: #0E1117;
|
149 |
+
}
|
150 |
+
|
151 |
+
</style>
|
152 |
+
"""
|
153 |
+
st.markdown(bg, unsafe_allow_html=True)
|
154 |
+
## Added
|
155 |
+
|
156 |
+
|
157 |
+
# Main content area for displaying chat messages
|
158 |
+
st.title("Chat with Andrew Tate")
|
159 |
+
st.write("Welcome to the chat!")
|
160 |
+
st.sidebar.image('ATate.png', use_column_width=True)
|
161 |
+
st.sidebar.markdown("<h1 style='text-align: center;'>Andrew Tate</h1>", unsafe_allow_html=True)
|
162 |
+
st.sidebar.markdown("<p style='text-align: center;'>Former Professional Kickboxer</p>", unsafe_allow_html=True)
|
163 |
+
st.sidebar.button('Clear Chat History', on_click=clear_chat_history)
|
164 |
+
|
165 |
+
if "messages" not in st.session_state.keys():
|
166 |
+
st.session_state.messages = [
|
167 |
+
{"role": "assistant", "content": "Hi! I'm Andrew Tate. Ask me a question"}]
|
168 |
+
|
169 |
+
for message in st.session_state.messages:
|
170 |
+
with st.chat_message(message["role"]):
|
171 |
+
st.write(message["content"])
|
172 |
+
|
173 |
+
if prompt := st.chat_input():
|
174 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
175 |
+
with st.chat_message("user"):
|
176 |
+
st.write(prompt)
|
177 |
+
|
178 |
+
# Display chat messages and bot response
|
179 |
+
if st.session_state.messages[-1]["role"] != "assistant":
|
180 |
+
with st.chat_message("assistant"):
|
181 |
+
with st.spinner("Thinking..."):
|
182 |
+
response = user_input(prompt)
|
183 |
+
placeholder = st.empty()
|
184 |
+
full_response = response
|
185 |
+
placeholder.markdown(full_response)
|
186 |
+
if response is not None:
|
187 |
+
message = {"role": "assistant", "content": full_response}
|
188 |
+
st.session_state.messages.append(message)
|
189 |
+
|
190 |
+
|
191 |
+
if __name__ == "__main__":
|
192 |
+
main()
|