Mehrdad Esmaeili
commited on
Commit
•
5abdd75
1
Parent(s):
7963641
Update app.py
Browse files
app.py
CHANGED
@@ -11,6 +11,7 @@ from langchain.llms import Cohere
|
|
11 |
from langchain.text_splitter import CharacterTextSplitter
|
12 |
from langchain.vectorstores import Chroma
|
13 |
import os
|
|
|
14 |
from tqdm import tqdm
|
15 |
import gradio as gr
|
16 |
# from langchain.memory import ConversationSummaryMemory
|
@@ -44,6 +45,8 @@ docsearch = Chroma.from_documents(texts, embeddings)
|
|
44 |
qa = RetrievalQA.from_chain_type(llm=Cohere(model='command'), chain_type="stuff", \
|
45 |
retriever=docsearch.as_retriever(search_kwargs={'k':1}),return_source_documents=True)
|
46 |
|
|
|
|
|
47 |
def predict(message, history):
|
48 |
'''experimenation with memory and conversation retrieval chain has resulted in less
|
49 |
performance, usefulness, and more halucination. Hence, this chat bot provides one
|
@@ -69,9 +72,11 @@ def predict(message, history):
|
|
69 |
# print([(item[-2].metadata,item[-1]) for item in r1],\
|
70 |
# '\n\n',result['result'],f'|| {result["source_documents"][0].metadata}','\n*****\n')
|
71 |
if result['result'] not in ["I don't know","I don't know."]:
|
72 |
-
|
73 |
-
|
74 |
-
|
|
|
|
|
75 |
else:
|
76 |
return result['result']
|
77 |
|
|
|
11 |
from langchain.text_splitter import CharacterTextSplitter
|
12 |
from langchain.vectorstores import Chroma
|
13 |
import os
|
14 |
+
import pickle
|
15 |
from tqdm import tqdm
|
16 |
import gradio as gr
|
17 |
# from langchain.memory import ConversationSummaryMemory
|
|
|
45 |
qa = RetrievalQA.from_chain_type(llm=Cohere(model='command'), chain_type="stuff", \
|
46 |
retriever=docsearch.as_retriever(search_kwargs={'k':1}),return_source_documents=True)
|
47 |
|
48 |
+
btuTuples=pickle.load(open('./bookTitleUrlTuples.pkl','rb'))
|
49 |
+
bookTitleUrlDict={x:y for x,y in btuTuples}
|
50 |
def predict(message, history):
|
51 |
'''experimenation with memory and conversation retrieval chain has resulted in less
|
52 |
performance, usefulness, and more halucination. Hence, this chat bot provides one
|
|
|
72 |
# print([(item[-2].metadata,item[-1]) for item in r1],\
|
73 |
# '\n\n',result['result'],f'|| {result["source_documents"][0].metadata}','\n*****\n')
|
74 |
if result['result'] not in ["I don't know","I don't know."]:
|
75 |
+
bookNamePath=result["source_documents"][0].metadata["source"]
|
76 |
+
return result['result']+f'\n---\n*Ignore the description below if the chatbot was unsure about its response \
|
77 |
+
or if the response is not about the book shown below*\nAmazon Kindle ebook description is:\n {result["source_documents"][0].page_content}'+\
|
78 |
+
f'\n**from this file:** {bookNamePath}\n'+\
|
79 |
+
f'**link==> {bookTitleUrlDict[bookNamePath.split("/")[-1]]}'
|
80 |
else:
|
81 |
return result['result']
|
82 |
|