Update app.py
Browse files
app.py
CHANGED
@@ -58,77 +58,77 @@ def main():
|
|
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 |
-
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
98 |
-
chunked_documents = text_splitter.split_documents(loaded_documents)
|
99 |
-
retriever = FAISS.from_documents(docs, embeddings).as_retriever()
|
100 |
-
|
101 |
-
# Wrap retrievers in a Tool
|
102 |
-
tools.append(
|
103 |
-
Tool(
|
104 |
-
name="Comparison tool",
|
105 |
-
description="useful when you want to answer questions about the uploaded documents",
|
106 |
-
func=RetrievalQA.from_chain_type(llm=llm, retriever=retriever),
|
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 |
-
st.warning("Please enter a question.")
|
132 |
|
133 |
|
134 |
if __name__ == "__main__":
|
|
|
58 |
|
59 |
|
60 |
|
61 |
+
if st.button("Get Answer"):
|
62 |
+
if query:
|
63 |
+
# Load model, set prompts, create vector database, and retrieve answer
|
64 |
+
try:
|
65 |
+
start = timeit.default_timer()
|
66 |
+
config = {
|
67 |
+
'max_new_tokens': 1024,
|
68 |
+
'repetition_penalty': 1.1,
|
69 |
+
'temperature': 0.1,
|
70 |
+
'top_k': 50,
|
71 |
+
'top_p': 0.9,
|
72 |
+
'stream': True,
|
73 |
+
'threads': int(os.cpu_count() / 2)
|
74 |
+
}
|
75 |
+
|
76 |
+
llm = CTransformers(
|
77 |
+
model="TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF",
|
78 |
+
model_file="mistral-7b-instruct-v0.2.Q4_0.gguf",
|
79 |
+
model_type="mistral",
|
80 |
+
lib="avx2", #for CPU use
|
81 |
+
**config
|
82 |
+
)
|
83 |
+
|
84 |
+
print("LLM Initialized...")
|
85 |
+
|
86 |
+
|
87 |
+
|
88 |
+
model_name = "BAAI/bge-large-en"
|
89 |
+
model_kwargs = {'device': 'cpu'}
|
90 |
+
encode_kwargs = {'normalize_embeddings': False}
|
91 |
+
embeddings = HuggingFaceBgeEmbeddings(
|
92 |
+
model_name=model_name,
|
93 |
+
model_kwargs=model_kwargs,
|
94 |
+
encode_kwargs=encode_kwargs
|
95 |
+
)
|
96 |
+
|
97 |
+
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
|
98 |
+
chunked_documents = text_splitter.split_documents(loaded_documents)
|
99 |
+
retriever = FAISS.from_documents(docs, embeddings).as_retriever()
|
100 |
|
101 |
+
# Wrap retrievers in a Tool
|
102 |
+
tools.append(
|
103 |
+
Tool(
|
104 |
+
name="Comparison tool",
|
105 |
+
description="useful when you want to answer questions about the uploaded documents",
|
106 |
+
func=RetrievalQA.from_chain_type(llm=llm, retriever=retriever),
|
107 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
108 |
)
|
109 |
+
|
110 |
+
agent = initialize_agent(
|
111 |
+
tools=tools,
|
112 |
+
llm=llm,
|
113 |
+
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
|
114 |
+
verbose=True
|
115 |
+
)
|
116 |
+
|
117 |
+
response = agent.run(query)
|
118 |
+
|
119 |
+
end = timeit.default_timer()
|
120 |
+
st.write("Elapsed time:")
|
121 |
+
st.write(end - start)
|
122 |
+
|
123 |
+
st.write("Bot Response:")
|
124 |
+
st.write(response)
|
125 |
+
|
126 |
+
|
127 |
+
|
128 |
+
except Exception as e:
|
129 |
+
st.error(f"An error occurred: {str(e)}")
|
130 |
+
else:
|
131 |
+
st.warning("Please enter a question.")
|
|
|
132 |
|
133 |
|
134 |
if __name__ == "__main__":
|