Yingxu He commited on
Commit
4e18b30
·
1 Parent(s): 7507474

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -20
app.py CHANGED
@@ -16,27 +16,26 @@ 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(open_ai_key):
23
- if open_ai_key is not None:
24
- os.environ['OPENAI_API_KEY'] = open_ai_key
25
- loader = PyPDFLoader("He Yingxu_2806.pdf")
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)]
@@ -85,7 +84,7 @@ with gr.Blocks(css=css) as demo:
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)
@@ -95,7 +94,7 @@ with gr.Blocks(css=css) as demo:
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=[openai_key], outputs=[langchain_status], queue=False)
99
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
100
  bot, chatbot, chatbot
101
  )
 
16
 
17
  from langchain.chains import ConversationalRetrievalChain
18
 
19
+ os.environ['OPENAI_API_KEY'] = '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'
20
+
21
  def loading_pdf():
22
  return "Loading..."
23
 
24
+ def pdf_changes():
25
+
26
+ loader = PyPDFLoader("He Yingxu_2806.pdf")
27
+ documents = loader.load()
28
+ text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
29
+ texts = text_splitter.split_documents(documents)
30
+ embeddings = OpenAIEmbeddings()
31
+ db = Chroma.from_documents(texts, embeddings)
32
+ retriever = db.as_retriever()
33
+ global qa
34
+ qa = ConversationalRetrievalChain.from_llm(
35
+ llm=OpenAI(temperature=0.5),
36
+ retriever=retriever,
37
+ return_source_documents=False)
38
+ return "Ready"
 
 
 
39
 
40
  def add_text(history, text):
41
  history = history + [(text, None)]
 
84
  gr.HTML(title)
85
 
86
  with gr.Column():
87
+ # openai_key = gr.Textbox(label="You OpenAI API key", type="password")
88
  # pdf_doc = gr.File(label="Load a pdf", file_types=['.pdf'], type="file")
89
  with gr.Row():
90
  langchain_status = gr.Textbox(label="Status", placeholder="", interactive=False)
 
94
  question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter ")
95
  submit_btn = gr.Button("Send Message")
96
  load_pdf.click(loading_pdf, None, langchain_status, queue=False)
97
+ load_pdf.click(pdf_changes, inputs=[], outputs=[langchain_status], queue=False)
98
  question.submit(add_text, [chatbot, question], [chatbot, question]).then(
99
  bot, chatbot, chatbot
100
  )