Spaces:
Runtime error
Runtime error
Mehrdad Esmaeili
commited on
Commit
•
0325e85
1
Parent(s):
8d566c8
Update app.py
Browse files
app.py
CHANGED
@@ -41,15 +41,9 @@ Chroma().delete_collection()
|
|
41 |
|
42 |
for file in os.listdir(path):
|
43 |
loader = TextLoader(f'{path}{file}',encoding='unicode_escape')
|
44 |
-
# loader.load()[0].metadata['category']='biography'
|
45 |
-
# print(loader.load()[0].metadata)
|
46 |
documents += loader.load()
|
47 |
-
# print(documents)
|
48 |
-
print(len(documents))
|
49 |
-
'''This is the code used for without memory chat'''
|
50 |
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
|
51 |
texts = text_splitter.split_documents(documents)
|
52 |
-
|
53 |
embeddings = CohereEmbeddings(model='embed-english-v3.0')
|
54 |
docsearch = Chroma.from_documents(texts, embeddings)
|
55 |
retriever=docsearch.as_retriever(search_kwargs={'k':1})
|
@@ -64,8 +58,6 @@ compression_retriever = ContextualCompressionRetriever(
|
|
64 |
memory=ConversationSummaryMemory(
|
65 |
llm=cohereLLM, memory_key="chat_history", return_messages=True
|
66 |
)
|
67 |
-
# qa = ConversationalRetrievalChain.from_llm(llm=cohereLLM, chain_type="stuff", \
|
68 |
-
# retriever=docsearch.as_retriever(search_kwargs={'k':1}),return_source_documents=True)
|
69 |
question_generator = LLMChain(llm=cohereLLM, prompt=CONDENSE_QUESTION_PROMPT)
|
70 |
doc_chain = load_qa_with_sources_chain(cohereLLM, chain_type="refine")
|
71 |
|
@@ -77,9 +69,6 @@ rag_chain=chain = ConversationalRetrievalChain(
|
|
77 |
)
|
78 |
#
|
79 |
|
80 |
-
# qa = RetrievalQA.from_chain_type(llm=cohereLLM, chain_type="stuff", \
|
81 |
-
# retriever=docsearch.as_retriever(search_kwargs={'k':1}),return_source_documents=True)
|
82 |
-
|
83 |
btuTuples=pickle.load(open('./bookTitleUrlTuples.pkl','rb'))
|
84 |
bookTitleUrlDict={x:y for x,y in btuTuples}
|
85 |
chat_history = []
|
@@ -91,36 +80,10 @@ def predict(message, history):
|
|
91 |
|
92 |
message="you are a language model that gives book recommendation based on your context"+message+\
|
93 |
'just give the book title and author'
|
94 |
-
# history_langchain_format = []
|
95 |
-
# for human, ai in history:
|
96 |
-
# history_langchain_format.append(HumanMessage(content=human))
|
97 |
-
# history_langchain_format.append(AIMessage(content=ai))
|
98 |
result=ai_msg = rag_chain({"question": message, "chat_history": chat_history})
|
99 |
chat_history.extend([HumanMessage(content=message), AIMessage(content=ai_msg['answer'])])
|
100 |
-
|
101 |
-
|
102 |
-
# return gpt_response['result']
|
103 |
-
# gpt_response = qa({'query':''.join(history)+f'.\n given the previous conversation respond using the following prompt:{message}'})
|
104 |
-
# # print(gpt_response)
|
105 |
-
# history.append((f'HumanMessage:{message}',f'AIMessage: {gpt_response},'))
|
106 |
-
# # history=history_langchain_format
|
107 |
-
# return gpt_response['result']
|
108 |
-
|
109 |
-
# result = qa({"query": message}) # uncomment this to get back to original state
|
110 |
-
|
111 |
-
# r1=docsearch.similarity_search_with_score(query=q,k=3)
|
112 |
-
# print([(item[-2].metadata,item[-1]) for item in r1],\
|
113 |
-
# '\n\n',result['result'],f'|| {result["source_documents"][0].metadata}','\n*****\n')
|
114 |
-
# if result['answer'] not in ["I don't know","I don't know."]: #change key from 'answer' tor 'result'
|
115 |
-
# bookNamePath=result["source_documents"][0].metadata["source"]
|
116 |
-
# return result['answer']+f'\n---\n***Ignore the description below if the chatbot was unsure about its response \
|
117 |
-
# or if the response is not about the book shown below. Note that this is a zero memory chatbot as I found it performs better this way.\
|
118 |
-
# Refer to github.com/mehrdad-es/Amazon-But-Better for more info.\n***\nAmazon Kindle ebook description is:\n {result["source_documents"][0].page_content}'+\
|
119 |
-
# f'\n**from this file:** {bookNamePath}\n'+\
|
120 |
-
# f'**link==>** {bookTitleUrlDict[bookNamePath.split("/")[-1][:-4]]}'
|
121 |
-
# else:
|
122 |
-
# return result['answer']
|
123 |
-
return result['answer']
|
124 |
|
125 |
gr.ChatInterface(predict,
|
126 |
chatbot=gr.Chatbot(height='auto'),
|
|
|
41 |
|
42 |
for file in os.listdir(path):
|
43 |
loader = TextLoader(f'{path}{file}',encoding='unicode_escape')
|
|
|
|
|
44 |
documents += loader.load()
|
|
|
|
|
|
|
45 |
text_splitter = CharacterTextSplitter(chunk_size=500, chunk_overlap=0)
|
46 |
texts = text_splitter.split_documents(documents)
|
|
|
47 |
embeddings = CohereEmbeddings(model='embed-english-v3.0')
|
48 |
docsearch = Chroma.from_documents(texts, embeddings)
|
49 |
retriever=docsearch.as_retriever(search_kwargs={'k':1})
|
|
|
58 |
memory=ConversationSummaryMemory(
|
59 |
llm=cohereLLM, memory_key="chat_history", return_messages=True
|
60 |
)
|
|
|
|
|
61 |
question_generator = LLMChain(llm=cohereLLM, prompt=CONDENSE_QUESTION_PROMPT)
|
62 |
doc_chain = load_qa_with_sources_chain(cohereLLM, chain_type="refine")
|
63 |
|
|
|
69 |
)
|
70 |
#
|
71 |
|
|
|
|
|
|
|
72 |
btuTuples=pickle.load(open('./bookTitleUrlTuples.pkl','rb'))
|
73 |
bookTitleUrlDict={x:y for x,y in btuTuples}
|
74 |
chat_history = []
|
|
|
80 |
|
81 |
message="you are a language model that gives book recommendation based on your context"+message+\
|
82 |
'just give the book title and author'
|
|
|
|
|
|
|
|
|
83 |
result=ai_msg = rag_chain({"question": message, "chat_history": chat_history})
|
84 |
chat_history.extend([HumanMessage(content=message), AIMessage(content=ai_msg['answer'])])
|
85 |
+
bookNamePath=result["source_documents"][0].metadata["source"]
|
86 |
+
return result['answer'] +f'''---\nlink: {bookTitleUrlDict[bookNamePath.split("/")[-1][:-4]]}'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
|
88 |
gr.ChatInterface(predict,
|
89 |
chatbot=gr.Chatbot(height='auto'),
|