html templates has been added
Browse files- app.py +104 -15
- htmlTemplates.py +44 -0
app.py
CHANGED
@@ -1,20 +1,109 @@
|
|
1 |
-
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
9 |
-
page_icon="🤖",
|
10 |
-
layout="wide",
|
11 |
-
initial_sidebar_state="expanded",
|
12 |
-
)
|
13 |
-
login(token='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', add_to_git_credential=True)
|
14 |
|
15 |
-
st.title("Code Generation")
|
16 |
-
st.write('MODEL: TinyPixel/red1xe/Llama-2-7B-codeGPT')
|
17 |
-
model_name='lmsys/vicuna-7b-v1.1'
|
18 |
-
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
19 |
-
model= AutoModelForCausalLM.from_pretrained(model_name)
|
20 |
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import streamlit as st
|
4 |
+
from dotenv import load_dotenv
|
5 |
+
from htmlTemplates import css, bot_template, user_template
|
6 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
7 |
+
from langchain.vectorstores import Chroma
|
8 |
+
from langchain.memory import ConversationBufferMemory
|
9 |
+
from langchain.prompts import PromptTemplate
|
10 |
+
from langchain.chains import RetrievalQA
|
11 |
+
from langchain.llms import HuggingFaceHub
|
12 |
+
from langchain import PromptTemplate
|
13 |
+
from pdfminer.high_level import extract_text
|
14 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
15 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
16 |
|
17 |
|
18 |
+
# Updated Prompt Template
|
19 |
+
template = """You are an expert on TeamCenter. Use the following pieces of context to answer the question at the end.
|
20 |
+
If you don't know the answer, it's okay to say that you don't know. Please don't try to make up an answer.
|
21 |
+
Use two sentences minimum and keep the answer as concise as possible (maximum 200 characters each).
|
22 |
+
Always use proper grammar and punctuation. End of the answer always say "End of answer" (without quotes).
|
23 |
+
|
24 |
+
Context:
|
25 |
+
{context}
|
26 |
+
|
27 |
+
Question: {question}
|
28 |
+
Helpful Answer (Two sentences minimum, maximum 200 characters each):"""
|
29 |
+
|
30 |
+
tokenizer = AutoTokenizer.from_pretrained("red1xe/falcon-7b-codeGPT-3K")
|
31 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("red1xe/falcon-7b-codeGPT-3K")
|
32 |
+
## QA_CHAIN_PROMPT = PromptTemplate(template=template, input_variables=["question", "context"])
|
33 |
+
|
34 |
+
load_dotenv()
|
35 |
+
persist_directory = os.environ.get('PERSIST_DIRECTORY')
|
36 |
+
embeddings_model_name = os.environ.get("EMBEDDINGS_MODEL_NAME")
|
37 |
+
model_path = os.environ.get('MODEL_PATH')
|
38 |
+
|
39 |
+
def get_vector_store(target_source_chunks):
|
40 |
+
embeddings = HuggingFaceEmbeddings(model_name=embeddings_model_name)
|
41 |
+
db = Chroma(persist_directory=persist_directory, embedding_function=embeddings)
|
42 |
+
retriver = db.as_retriever(search_kwargs={"k": target_source_chunks})
|
43 |
+
return retriver
|
44 |
+
|
45 |
+
def get_conversation_chain(retriever):
|
46 |
+
memory = ConversationBufferMemory(memory_key='chat_history', return_messages=True,)
|
47 |
+
chain = RetrievalQA.from_llm(
|
48 |
+
llm=model,
|
49 |
+
memory=memory,
|
50 |
+
retriever=retriever,
|
51 |
+
)
|
52 |
+
return chain
|
53 |
+
|
54 |
+
|
55 |
+
def handle_userinput(user_question):
|
56 |
+
if st.session_state.conversation is None:
|
57 |
+
st.warning("Please load the Vectorstore first!")
|
58 |
+
return
|
59 |
+
else:
|
60 |
+
with st.spinner('Thinking...', ):
|
61 |
+
start_time = time.time()
|
62 |
+
response = st.session_state.conversation({'query': user_question})
|
63 |
+
end_time = time.time()
|
64 |
+
|
65 |
+
st.session_state.chat_history = response['chat_history']
|
66 |
+
|
67 |
+
for i, message in enumerate(st.session_state.chat_history):
|
68 |
+
if i % 2 == 0:
|
69 |
+
st.write(user_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
|
70 |
+
else:
|
71 |
+
st.write(bot_template.replace("{{MSG}}", message.content), unsafe_allow_html=True)
|
72 |
+
|
73 |
+
st.write('Elapsed time: {:.2f} seconds'.format(end_time - start_time))
|
74 |
+
st.balloons()
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
def main():
|
80 |
+
|
81 |
+
st.set_page_config(page_title='Chat with PDF', page_icon=':rocket:', layout='wide', )
|
82 |
+
with st.sidebar.title(':gear: Parameters'):
|
83 |
+
model_n_ctx = st.sidebar.slider('Model N_CTX', min_value=128, max_value=2048, value=1024, step=2)
|
84 |
+
model_n_batch = st.sidebar.slider('Model N_BATCH', min_value=1, max_value=model_n_ctx, value=512, step=2)
|
85 |
+
target_source_chunks = st.sidebar.slider('Target Source Chunks', min_value=1, max_value=10, value=4, step=1)
|
86 |
+
st.write(css, unsafe_allow_html=True)
|
87 |
+
|
88 |
+
if "conversation" not in st.session_state:
|
89 |
+
st.session_state.conversation = None
|
90 |
+
if "chat_history" not in st.session_state:
|
91 |
+
st.session_state.chat_history = None
|
92 |
+
|
93 |
+
st.header('Chat with PDF :robot_face:')
|
94 |
+
st.subheader('Upload your PDF file and start chatting with it!')
|
95 |
+
user_question = st.text_input('Enter your message here:')
|
96 |
+
|
97 |
+
if st.button('Start Chain'):
|
98 |
+
with st.spinner('Working in progress ...'):
|
99 |
+
vector_store = get_vector_store(target_source_chunks)
|
100 |
+
st.session_state.conversation = get_conversation_chain(
|
101 |
+
retriever=vector_store,
|
102 |
+
)
|
103 |
|
104 |
+
if user_question:
|
105 |
+
handle_userinput(user_question)
|
|
|
|
|
|
|
|
|
|
|
106 |
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
+
if __name__ == '__main__':
|
109 |
+
main()
|
htmlTemplates.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
css = '''
|
2 |
+
<style>
|
3 |
+
.chat-message {
|
4 |
+
padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex
|
5 |
+
}
|
6 |
+
.chat-message.user {
|
7 |
+
background-color: #2b313e
|
8 |
+
}
|
9 |
+
.chat-message.bot {
|
10 |
+
background-color: #475063
|
11 |
+
}
|
12 |
+
.chat-message .avatar {
|
13 |
+
width: 20%;
|
14 |
+
}
|
15 |
+
.chat-message .avatar img {
|
16 |
+
max-width: 78px;
|
17 |
+
max-height: 78px;
|
18 |
+
border-radius: 70%;
|
19 |
+
object-fit: cover;
|
20 |
+
}
|
21 |
+
.chat-message .message {
|
22 |
+
width: 80%;
|
23 |
+
padding: 0 1.5rem;
|
24 |
+
color: #fff;
|
25 |
+
}
|
26 |
+
'''
|
27 |
+
|
28 |
+
bot_template = '''
|
29 |
+
<div class="chat-message bot">
|
30 |
+
<div class="avatar">
|
31 |
+
<img src="https://media.licdn.com/dms/image/C4E0BAQHSBVuHaXe6DA/company-logo_200_200/0/1519893058477?e=1697673600&v=beta&t=ISf-4u1cOnN0KNQSNLt5MY5RvxYL50PIFIyvuUrIf9g" style="max-height: 78px; max-width: 78px; border-radius: 50%; object-fit: cover;">
|
32 |
+
</div>
|
33 |
+
<div class="message">{{MSG}}</div>
|
34 |
+
</div>
|
35 |
+
'''
|
36 |
+
|
37 |
+
user_template = '''
|
38 |
+
<div class="chat-message user">
|
39 |
+
<div class="avatar">
|
40 |
+
<img src="https://cdn-icons-png.flaticon.com/512/1077/1077012.png">
|
41 |
+
</div>
|
42 |
+
<div class="message">{{MSG}}</div>
|
43 |
+
</div>
|
44 |
+
'''
|