ramwar commited on
Commit
f6bf3b8
1 Parent(s): f7004f9

first version

Browse files
Files changed (2) hide show
  1. app.py +85 -0
  2. requirements.txt +6 -0
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import requests
3
+ import io
4
+ import time
5
+ from getpass import getpass
6
+ from langchain import HuggingFaceHub
7
+ from langchain.embeddings import HuggingFaceEmbeddings
8
+ from langchain.text_splitter import TokenTextSplitter, CharacterTextSplitter
9
+ from langchain import PromptTemplate, LLMChain
10
+ from langchain.chains.question_answering import load_qa_chain
11
+ from langchain.vectorstores import FAISS
12
+ from langchain.document_loaders import TextLoader
13
+ from langchain.callbacks.manager import CallbackManager
14
+ from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
15
+ import gradio as gr
16
+
17
+
18
+ question_examples = [
19
+ "What is the name of the captain of the Nautilus?",
20
+ "What are the names of the crew members of the Nautilus?",
21
+ "What is the name of the Canadian?",
22
+ "What is Ned Land's profession?",
23
+ "What musical instrument does Captain Nemo play?",
24
+ "What is the name of Professor Aronnax's butler?",
25
+ "In which areas was the Nautilus traveling?",
26
+ "Why doesn't Captain Nemo hate the society?"
27
+ ]
28
+
29
+ os.environ["HUGGINGFACEHUB_API_TOKEN"]
30
+
31
+ REPO_ID = "declare-lab/flan-alpaca-large"
32
+
33
+ llm = HuggingFaceHub(
34
+ repo_id=REPO_ID,
35
+ model_kwargs={"temperature":0, "max_length":512}
36
+ )
37
+
38
+ embeddings = HuggingFaceEmbeddings()
39
+ callback_manager = CallbackManager([StreamingStdOutCallbackHandler()])
40
+
41
+ def read_textfile(File):
42
+ loader = TextLoader(File.name)
43
+ documents = loader.load()
44
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=10)
45
+ docs = text_splitter.split_documents(documents)
46
+ db = FAISS.from_documents(docs, embeddings)
47
+ db.save_local(folder_path=".", index_name="faiss_index")
48
+
49
+ def ask_question(question, chat_history):
50
+ chain = load_qa_chain(llm, chain_type="stuff", verbose=False)
51
+ db = FAISS.load_local(folder_path=".", embeddings=embeddings, index_name="faiss_index")
52
+ relevant_docs = db.similarity_search(question)
53
+
54
+ answer = chain.run(
55
+ input_documents=relevant_docs,
56
+ question=question,
57
+ callbacks=callback_manager
58
+ )
59
+
60
+ chat_history.append((question, answer))
61
+ time.sleep(1)
62
+ return "", chat_history
63
+
64
+ with gr.Blocks() as demo:
65
+ gr.Markdown("## 📚AskYourBooks")
66
+ gr.Markdown("""
67
+ Ask questions to your books in txt format. After a short indexing time,
68
+ ask questions about the content of the book.
69
+ """
70
+ )
71
+ textfile = gr.File(label="Drag your EPUB book here")
72
+ ask_textbox = gr.Textbox(label="Question")
73
+ sample_questions = gr.Examples(examples=question_examples, inputs=[ask_textbox])
74
+ ask_button = gr.Button("Ask your book")
75
+ clear_button = gr.Button("Clear chat")
76
+ chatbot = gr.Chatbot(label="Conversation")
77
+
78
+ textfile.change(fn=read_textfile, inputs=[textfile], outputs=[])
79
+ ask_button.click(
80
+ fn=ask_question,
81
+ inputs=[ask_textbox, chatbot],
82
+ outputs=[ask_textbox, chatbot]
83
+ )
84
+ clear_button.click(lambda: None, None, chatbot, queue=False)
85
+
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ gradio
2
+ langchain
3
+ transformers
4
+ sentence_transformers
5
+ tiktoken
6
+ faiss-cpu