Spaces:
Runtime error
Runtime error
Commit
·
05d6996
1
Parent(s):
b994f8d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from PyPDF2 import PdfReader
|
2 |
+
from langchain.embeddings.openai import OpenAIEmbeddings
|
3 |
+
from langchain.text_splitter import CharacterTextSplitter
|
4 |
+
from langchain.vectorstores import ElasticVectorSearch, Pinecone, Weaviate, FAISS
|
5 |
+
from langchain.chains.question_answering import load_qa_chain
|
6 |
+
from langchain.llms import OpenAI
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
import os
|
10 |
+
|
11 |
+
|
12 |
+
|
13 |
+
reader = PdfReader('NEWChanakya Neeti - Radhakrishnan Pillai.pdf')
|
14 |
+
|
15 |
+
raw_text = ''
|
16 |
+
for i, page in enumerate(reader.pages):
|
17 |
+
text = page.extract_text()
|
18 |
+
if text:
|
19 |
+
raw_text += text
|
20 |
+
|
21 |
+
text_splitter = CharacterTextSplitter(
|
22 |
+
separator = "\n",
|
23 |
+
chunk_size = 1000,
|
24 |
+
chunk_overlap = 200,
|
25 |
+
length_function = len,
|
26 |
+
)
|
27 |
+
texts = text_splitter.split_text(raw_text)
|
28 |
+
|
29 |
+
embeddings = OpenAIEmbeddings()
|
30 |
+
docsearch = FAISS.from_texts(texts, embeddings)
|
31 |
+
|
32 |
+
chain = load_qa_chain(OpenAI(), chain_type="stuff")
|
33 |
+
|
34 |
+
def learn_from_chanakya_neeti(chat_history,query):
|
35 |
+
docs = docsearch.similarity_search(query)
|
36 |
+
result = chain.run(input_documents=docs, question=query)
|
37 |
+
return chat_history + [(query,result)]
|
38 |
+
|
39 |
+
with gr.Blocks() as demo:
|
40 |
+
chatbot = gr.Chatbot()
|
41 |
+
textbox = gr.Textbox(label="Your query")
|
42 |
+
|
43 |
+
textbox.submit(fn=learn_from_chanakya_neeti,inputs=[chatbot,textbox],outputs=[chatbot])
|
44 |
+
|
45 |
+
|
46 |
+
demo.launch(debug=True)
|