wholewhale commited on
Commit
e455307
1 Parent(s): 2f0e211

Replaced API form

Browse files
Files changed (1) hide show
  1. app.py +26 -39
app.py CHANGED
@@ -1,42 +1,33 @@
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
-
15
  from langchain.vectorstores import Chroma
16
-
17
  from langchain.chains import ConversationalRetrievalChain
18
 
 
 
 
19
  def loading_pdf():
20
  return "Loading..."
21
 
22
- def pdf_changes(pdf_doc, open_ai_key):
23
- if openai_key is not None:
24
- os.environ['OPENAI_API_KEY'] = open_ai_key
25
- loader = OnlinePDFLoader(pdf_doc.name)
26
- documents = loader.load()
27
- text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
28
- texts = text_splitter.split_documents(documents)
29
- embeddings = OpenAIEmbeddings()
30
- db = Chroma.from_documents(texts, embeddings)
31
- retriever = db.as_retriever()
32
- global qa
33
- qa = ConversationalRetrievalChain.from_llm(
34
- llm=OpenAI(temperature=0.5),
35
- retriever=retriever,
36
- return_source_documents=False)
37
- return "Ready"
38
- else:
39
- return "You forgot OpenAI API key"
40
 
41
  def add_text(history, text):
42
  history = history + [(text, None)]
@@ -51,22 +42,18 @@ def bot(history):
51
  time.sleep(0.05)
52
  yield history
53
 
54
-
55
  def infer(question, history):
56
-
57
  res = []
58
  for human, ai in history[:-1]:
59
  pair = (human, ai)
60
  res.append(pair)
61
 
62
  chat_history = res
63
- #print(chat_history)
64
  query = question
65
  result = qa({"question": query, "chat_history": chat_history})
66
- #print(result)
67
  return result["answer"]
68
 
69
- css="""
70
  #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
71
  """
72
 
@@ -75,31 +62,31 @@ title = """
75
  <h1>Chat with PDF • OpenAI</h1>
76
  <p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
77
  when everything is ready, you can start asking questions about the pdf ;) <br />
78
- This version is set to store chat history, and uses OpenAI as LLM, don't forget to copy/paste your OpenAI API key</p>
79
  </div>
80
  """
81
 
82
-
83
  with gr.Blocks(css=css) as demo:
84
  with gr.Column(elem_id="col-container"):
85
  gr.HTML(title)
86
 
87
  with gr.Column():
88
- openai_key = gr.Textbox(label="You OpenAI API key", type="password")
89
  pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
90
  with gr.Row():
91
  langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
92
  load_pdf = gr.Button("Load pdf to langchain")
93
 
94
  chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
95
- question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
96
  submit_btn = gr.Button("Send Message")
97
- load_pdf.click(loading_pdf, None, langchain_status, queue=False)
98
- load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)
 
99
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
100
  bot, chatbot, chatbot
101
  )
102
  submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
103
- bot, chatbot, chatbot)
 
104
 
105
- demo.launch()
 
1
  import gradio as gr
2
  import os
3
  import time
 
4
  from langchain.document_loaders import OnlinePDFLoader
 
5
  from langchain.text_splitter import CharacterTextSplitter
 
 
6
  from langchain.llms import OpenAI
 
7
  from langchain.embeddings import OpenAIEmbeddings
 
 
8
  from langchain.vectorstores import Chroma
 
9
  from langchain.chains import ConversationalRetrievalChain
10
 
11
+ # Set OpenAI API key from environment variable
12
+ os.environ['OPENAI_API_KEY'] = os.getenv("Your_API_Key")
13
+
14
  def loading_pdf():
15
  return "Loading..."
16
 
17
+ def pdf_changes(pdf_doc):
18
+ loader = OnlinePDFLoader(pdf_doc.name)
19
+ documents = loader.load()
20
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
21
+ texts = text_splitter.split_documents(documents)
22
+ embeddings = OpenAIEmbeddings()
23
+ db = Chroma.from_documents(texts, embeddings)
24
+ retriever = db.as_retriever()
25
+ global qa
26
+ qa = ConversationalRetrievalChain.from_llm(
27
+ llm=OpenAI(temperature=0.5),
28
+ retriever=retriever,
29
+ return_source_documents=False)
30
+ return "Ready"
 
 
 
 
31
 
32
  def add_text(history, text):
33
  history = history + [(text, None)]
 
42
  time.sleep(0.05)
43
  yield history
44
 
 
45
  def infer(question, history):
 
46
  res = []
47
  for human, ai in history[:-1]:
48
  pair = (human, ai)
49
  res.append(pair)
50
 
51
  chat_history = res
 
52
  query = question
53
  result = qa({"question": query, "chat_history": chat_history})
 
54
  return result["answer"]
55
 
56
+ css = """
57
  #col-container {max-width: 700px; margin-left: auto; margin-right: auto;}
58
  """
59
 
 
62
  <h1>Chat with PDF • OpenAI</h1>
63
  <p style="text-align: center;">Upload a .PDF from your computer, click the "Load PDF to LangChain" button, <br />
64
  when everything is ready, you can start asking questions about the pdf ;) <br />
65
+ This version is set to store chat history, and uses OpenAI as LLM.</p>
66
  </div>
67
  """
68
 
 
69
  with gr.Blocks(css=css) as demo:
70
  with gr.Column(elem_id="col-container"):
71
  gr.HTML(title)
72
 
73
  with gr.Column():
 
74
  pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
75
  with gr.Row():
76
  langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
77
  load_pdf = gr.Button("Load pdf to langchain")
78
 
79
  chatbot = gr.Chatbot([], elem_id="chatbot").style(height=350)
80
+ question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
81
  submit_btn = gr.Button("Send Message")
82
+
83
+ load_pdf.click(loading_pdf, None, langchain_status, queue=False)
84
+ load_pdf.click(pdf_changes, inputs=[pdf_doc], outputs=[langchain_status], queue=False)
85
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
86
  bot, chatbot, chatbot
87
  )
88
  submit_btn.click(add_text, [chatbot, question], [chatbot, question]).then(
89
+ bot, chatbot, chatbot
90
+ )
91
 
92
+ demo.launch()