fffiloni commited on
Commit
c2af1e5
·
1 Parent(s): 2645c4d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -17
app.py CHANGED
@@ -1,4 +1,5 @@
1
  import gradio as gr
 
2
 
3
  from langchain.document_loaders import OnlinePDFLoader
4
 
@@ -17,18 +18,22 @@ from langchain.chains import ConversationalRetrievalChain
17
  def loading_pdf():
18
  return "Loading..."
19
 
20
- def pdf_changes(pdf_doc):
21
- loader = OnlinePDFLoader(pdf_doc.name)
22
- documents = loader.load()
23
- texts = text_splitter.split_documents(documents)
24
- db = Chroma.from_documents(texts, embeddings)
25
- retriever = db.as_retriever()
26
- global qa
27
- qa = ConversationalRetrievalChain.from_llm(
28
- llm=OpenAI(temperature=0.5),
29
- retriever=retriever,
30
- return_source_documents=False)
31
- return "Ready"
 
 
 
 
32
 
33
  def add_text(history, text):
34
  history = history + [(text, None)]
@@ -40,18 +45,17 @@ def bot(history):
40
  return history
41
 
42
  def infer(question, history):
43
- print(history[-1])
44
 
45
  res = []
46
  for human, ai in history[:-1]:
47
- pair = f"Human:{human}\nAI:{ai}"
48
  res.append(pair)
49
  res = ["\n".join(res)]
50
  chat_history = res
51
- print(chat_history)
52
  query = question
53
  result = qa({"question": query, "chat_history": chat_history})
54
- print(result)
55
  return result["answer"]
56
 
57
  css="""
@@ -72,6 +76,7 @@ with gr.Blocks(css=css) as demo:
72
  gr.HTML(title)
73
 
74
  with gr.Column():
 
75
  pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
76
  with gr.Row():
77
  langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
@@ -81,7 +86,7 @@ with gr.Blocks(css=css) as demo:
81
  with gr.Row():
82
  question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
83
  load_pdf.click(loading_pdf, None, langchain_status, queue=False)
84
- load_pdf.click(pdf_changes, pdf_doc, langchain_status, queue=False)
85
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
86
  bot, chatbot, chatbot
87
  )
 
1
  import gradio as gr
2
+ import os
3
 
4
  from langchain.document_loaders import OnlinePDFLoader
5
 
 
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
+ texts = text_splitter.split_documents(documents)
27
+ db = Chroma.from_documents(texts, embeddings)
28
+ retriever = db.as_retriever()
29
+ global qa
30
+ qa = ConversationalRetrievalChain.from_llm(
31
+ llm=OpenAI(temperature=0.5),
32
+ retriever=retriever,
33
+ return_source_documents=False)
34
+ return "Ready"
35
+ else:
36
+ return "You forgot OpenAI API key"
37
 
38
  def add_text(history, text):
39
  history = history + [(text, None)]
 
45
  return history
46
 
47
  def infer(question, history):
 
48
 
49
  res = []
50
  for human, ai in history[:-1]:
51
+ pair = (human, ai)
52
  res.append(pair)
53
  res = ["\n".join(res)]
54
  chat_history = res
55
+ #print(chat_history)
56
  query = question
57
  result = qa({"question": query, "chat_history": chat_history})
58
+ #print(result)
59
  return result["answer"]
60
 
61
  css="""
 
76
  gr.HTML(title)
77
 
78
  with gr.Column():
79
+ openai_key = gr.Textbox(label="You OpenAI API key", type="password")
80
  pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
81
  with gr.Row():
82
  langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
 
86
  with gr.Row():
87
  question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
88
  load_pdf.click(loading_pdf, None, langchain_status, queue=False)
89
+ load_pdf.click(pdf_changes, inputs=[pdf_doc, openai_key], outputs=[langchain_status], queue=False)
90
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
91
  bot, chatbot, chatbot
92
  )