Karthikeyan commited on
Commit
87b3a2f
1 Parent(s): 18aeb65

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +122 -88
app.py CHANGED
@@ -1,103 +1,137 @@
1
- import gradio as gr
2
  import os
3
- import time
4
-
5
- from langchain.document_loaders import OnlinePDFLoader
6
-
7
  from langchain.text_splitter import CharacterTextSplitter
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
9
 
10
- from langchain.llms import OpenAI
11
 
12
- from langchain.embeddings import OpenAIEmbeddings
13
-
14
- from langchain.vectorstores import Chroma
15
-
16
- from langchain.chains import ConversationalRetrievalChain
17
-
18
- def loading_pdf():
19
- return "Loading..."
20
-
21
- def pdf_changes(pdf_doc, open_ai_key):
22
- if openai_key is not None:
23
- os.environ['OPENAI_API_KEY'] = open_ai_key
24
- loader = OnlinePDFLoader(pdf_doc.name)
25
- documents = loader.load()
26
- text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
27
- texts = text_splitter.split_documents(documents)
28
- embeddings = OpenAIEmbeddings()
29
- db = Chroma.from_documents(texts, embeddings)
30
- retriever = db.as_retriever()
31
- global qa
32
- qa = ConversationalRetrievalChain.from_llm(
33
- llm=OpenAI(temperature=0.5),
34
- retriever=retriever,
35
- return_source_documents=False)
36
- return "Ready"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  else:
38
- return "You forgot OpenAI API key"
39
-
40
- def add_text(history, text):
41
- history = history + [(text, None)]
42
- return history, ""
43
-
44
- def bot(history):
45
- response = infer(history[-1][0], history)
46
- history[-1][1] = ""
47
-
48
- for character in response:
49
- history[-1][1] += character
50
- time.sleep(0.05)
51
- yield history
52
-
53
-
54
- def infer(question, history):
55
-
56
- res = []
57
- for human, ai in history[:-1]:
58
- pair = (human, ai)
59
- res.append(pair)
60
-
61
- chat_history = res
62
- #print(chat_history)
63
- query = question
64
- result = qa({"question": query, "chat_history": chat_history})
65
- #print(result)
66
- return result["answer"]
67
-
68
- css="""
69
- #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
70
- """
71
 
72
  title = """
73
  <div style="text-align: center;max-width: 700px;">
74
- <h1>LangChain ChatBot</h1>
75
- <p style="text-align: center;">Upload a PDF, click the "Load PDF to LangChain" button, <br /></p>
76
  <a style="display:inline-block; margin-left: 1em" href="https://www.adople.com"><img src="https://lh6.googleusercontent.com/FQJXx8B6Tbq7SvSE3wvJyXusFZxKcsY92eQaPnZj5pIDdXHVjs10tXXBqWcF0BgC_riSFcje2qUd-XWaiaJByI6dMOkEFdAtpeG7KK8xh7nH8KE3GfSOMrySKPVWXGdEvg=w1280" alt="Adople AI"></a>
77
  </div>
78
  """
79
 
80
-
81
-
82
- with gr.Blocks(css=css,theme=gr.themes.Soft()) as demo:
83
  with gr.Column(elem_id="col-container"):
84
- gr.HTML(title)
85
- with gr.Column():
86
- openai_key = gr.Textbox(label="You OpenAI API key", type="password")
87
- pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
88
- with gr.Row():
89
- langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
90
- load_pdf = gr.Button("Load pdf to langchain")
91
-
92
- chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
93
- question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
94
- submit_btn = gr.Button("Send Message")
95
- load_pdf.click(loading_pdf, None, langchain_status, queue=False)
96
- load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)
97
- question.submit(add_text, [chatbot, question], [chatbot, question]).then(
98
- bot, chatbot, chatbot
99
- )
100
- submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
101
- bot, chatbot, chatbot)
102
 
103
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pydantic import NoneStr
2
  import os
3
+ from langchain.chains.question_answering import load_qa_chain
4
+ from langchain.document_loaders import UnstructuredFileLoader
5
+ from langchain.embeddings.openai import OpenAIEmbeddings
6
+ from langchain.llms import OpenAI
7
  from langchain.text_splitter import CharacterTextSplitter
8
+ from langchain.vectorstores import FAISS
9
+ from pypdf import PdfReader
10
+ import mimetypes
11
+ import validators
12
+ import requests
13
+ import tempfile
14
+ import gradio as gr
15
+ import openai
16
 
17
+ openai_api_key ='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'
18
+ os.environ["OPENAI_API_KEY"] = openai_api_key
19
+
20
+ def get_empty_state():
21
+ return {"knowledge_base": None}
22
 
 
23
 
24
+ def create_knowledge_base(docs):
25
+ # split into chunks
26
+ text_splitter = CharacterTextSplitter(
27
+ separator="\n", chunk_size=500, chunk_overlap=0, length_function=len
28
+ )
29
+ chunks = text_splitter.split_documents(docs)
30
+
31
+ # Create embeddings
32
+ embeddings = OpenAIEmbeddings()
33
+ knowledge_base = FAISS.from_documents(chunks, embeddings)
34
+ return knowledge_base
35
+
36
+
37
+ def upload_file(file_obj):
38
+ try:
39
+ loader = UnstructuredFileLoader(file_obj.name, strategy="fast")
40
+ docs = loader.load()
41
+
42
+ knowledge_base = create_knowledge_base(docs)
43
+ except:
44
+ text="Try Another file"
45
+ return file_obj.name, text
46
+
47
+ return file_obj.name, {"knowledge_base": knowledge_base}
48
+
49
+
50
+ def upload_via_url(url):
51
+ if validators.url(url):
52
+ r = requests.get(url)
53
+
54
+ if r.status_code != 200:
55
+ raise ValueError(
56
+ "Check the url of your file; returned status code %s" % r.status_code
57
+ )
58
+
59
+ content_type = r.headers.get("content-type")
60
+ file_extension = mimetypes.guess_extension(content_type)
61
+ temp_file = tempfile.NamedTemporaryFile(suffix=file_extension, delete=False)
62
+ temp_file.write(r.content)
63
+ file_path = temp_file.name
64
+ loader = UnstructuredFileLoader(file_path, strategy="fast")
65
+ docs = loader.load()
66
+ with open(file_path, mode="rb") as f:
67
+ pass
68
+ knowledge_base = create_knowledge_base(docs)
69
+ return file_path, {"knowledge_base": knowledge_base}
70
  else:
71
+ raise ValueError("Please enter a valid URL")
72
+
73
+
74
+ def answer_question(question, state):
75
+
76
+ try:
77
+ knowledge_base = state["knowledge_base"]
78
+ docs = knowledge_base.similarity_search(question)
79
+
80
+ llm = OpenAI(temperature=0.4)
81
+ chain = load_qa_chain(llm, chain_type="stuff")
82
+ response = chain.run(input_documents=docs, question=question)
83
+ return response
84
+ except:
85
+ return "Please upload Proper Document"
86
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
 
88
  title = """
89
  <div style="text-align: center;max-width: 700px;">
90
+ <h1>ADOPLE AI - Document ChatBot</h1>
91
+ <p style="text-align: center;">Upload a PDF, click the "Load PDF" button, <br /></p>
92
  <a style="display:inline-block; margin-left: 1em" href="https://www.adople.com"><img src="https://lh6.googleusercontent.com/FQJXx8B6Tbq7SvSE3wvJyXusFZxKcsY92eQaPnZj5pIDdXHVjs10tXXBqWcF0BgC_riSFcje2qUd-XWaiaJByI6dMOkEFdAtpeG7KK8xh7nH8KE3GfSOMrySKPVWXGdEvg=w1280" alt="Adople AI"></a>
93
  </div>
94
  """
95
 
96
+ with gr.Blocks(css="style.css",theme=gr.themes.Soft()) as demo:
97
+ state = gr.State(get_empty_state())
98
+ gr.HTML(title)
99
  with gr.Column(elem_id="col-container"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
100
 
101
+ gr.HTML(
102
+ """<br>
103
+ <h1 style="text-align:center;">
104
+ Adople AI Document QA Model
105
+ </h1> """
106
+ )
107
+ gr.HTML(
108
+ """<hr style="border-top: 5px solid white;">"""
109
+ )
110
+
111
+ gr.Markdown("**Upload your file**")
112
+ with gr.Row(elem_id="row-flex"):
113
+ with gr.Column(scale=0.85):
114
+ file_url = gr.Textbox(
115
+ value="",
116
+ label="Upload your file",
117
+ placeholder="Enter a url",
118
+ show_label=False,
119
+ visible=False
120
+ )
121
+ with gr.Column(scale=0.15, min_width=160):
122
+ upload_button = gr.UploadButton(
123
+ "Browse File", file_types=[".txt", ".pdf", ".doc", ".docx"],
124
+ )
125
+ file_output = gr.File(elem_classes="filenameshow")
126
+ with gr.Row():
127
+ with gr.Column(scale=1, min_width=0):
128
+ user_question = gr.Textbox(value="",label='Question Box :',show_label=True, placeholder="Ask a question about your file:",elem_classes="spaceH")
129
+ with gr.Row():
130
+ with gr.Column(scale=1, min_width=0):
131
+ answer = gr.Textbox(value="",label='Answer Box :',show_label=True, placeholder="",lines=5)
132
+
133
+ file_url.submit(upload_via_url, file_url, [file_output, state])
134
+ upload_button.upload(upload_file, upload_button, [file_output,state])
135
+ user_question.submit(answer_question, [user_question, state], [answer])
136
+
137
+ demo.queue().launch(share=True)