chatbytes commited on
Commit
fa67bc8
·
verified ·
1 Parent(s): 19bed3b

creating chatbot

Browse files
Files changed (1) hide show
  1. app.py +65 -0
app.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from langchain.llms import GooglePalm
3
+ from langchain.embeddings import HuggingFaceInstructEmbeddings
4
+ from langchain.text_splitter import CharacterTextSplitter
5
+ from langchain.embeddings import GooglePalmEmbeddings
6
+ from langchain.vectorstores import FAISS
7
+ from langchain.document_loaders import PyPDFLoader
8
+ from langchain.chains import RetrievalQA
9
+ from secret1 import GOOGLE_API as google_api
10
+ import PyPDF2
11
+ def chatbot_response(user_input, history):
12
+ # This is a placeholder function. Replace with your actual chatbot logic.
13
+ bot_response = "You said: " + user_input
14
+ history.append((user_input, bot_response))
15
+ return history, history
16
+
17
+ def text_splitter_function(text):
18
+ text_splitter = CharacterTextSplitter(
19
+ separator = '\n',
20
+ chunk_size = 1000,
21
+ chunk_overlap = 40,
22
+ length_function = len,
23
+ )
24
+ texts = text_splitter.split_text(text)
25
+ return texts;
26
+
27
+ def text_extract(file):
28
+ pdf_reader = PyPDF2.PdfReader(file.name)
29
+ # Get the number of pages
30
+ num_pages = len(pdf_reader.pages)
31
+ # Extract text from each page
32
+ text = ""
33
+ for page_num in range(num_pages):
34
+ page = pdf_reader.pages[page_num]
35
+ text += page.extract_text()
36
+ text_splitter=text_splitter_function(text);
37
+ db = FAISS.from_texts(text_splitter, embeddings);
38
+ retriever = db.as_retriever(search_type="similarity", search_kwargs={"k": 2})
39
+ llm=GooglePalm(google_api_key=google_api)
40
+ qa = RetrievalQA.from_chain_type(
41
+ llm=llm, chain_type="stuff", retriever=retriever, return_source_documents=True
42
+ )
43
+ print(db)
44
+ return text
45
+
46
+
47
+ with gr.Blocks() as demo:
48
+ gr.Markdown("# Chat with ChatGPT-like Interface")
49
+
50
+ chatbot = gr.Chatbot()
51
+ state = gr.State([])
52
+
53
+ with gr.Row():
54
+ with gr.Column():
55
+ user_input = gr.Textbox(show_label=False, placeholder="Type your message here...")
56
+ send_btn = gr.Button("Send")
57
+ with gr.Column():
58
+ input_file=gr.File(label="Upload PDF", file_count="single")
59
+ submit_btn=gr.Button("Submit")
60
+ submit_btn.click(text_extract, [input_file], [user_input])
61
+ send_btn.click(chatbot_response,[user_input,state],[chatbot, state])
62
+
63
+ if __name__ == "__main__":
64
+ embeddings=GooglePalmEmbeddings(google_api_key=google_api)
65
+ demo.launch()