wholewhale commited on
Commit
4659cc6
·
1 Parent(s): cfc65ef

breakup fxns

Browse files
Files changed (1) hide show
  1. app.py +39 -41
app.py CHANGED
@@ -30,51 +30,49 @@ def summary(self):
30
  return f"Number of documents: {num_documents}, Average document length: {avg_doc_length}"
31
 
32
  # Gradio state
33
- summary_state = gr.State(initial_value="pending")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
34
 
35
- # PDF summary and query using stuffing
36
  def pdf_changes(pdf_doc):
37
  try:
38
- # Initialize loader and load documents
39
- loader = OnlinePDFLoader(pdf_doc.name)
40
- documents = loader.load()
41
-
42
- # Define the prompt for summarization
43
- prompt_template = """Write a concise summary of the following:
44
- "{text}"
45
- CONCISE SUMMARY:"""
46
- prompt = PromptTemplate.from_template(prompt_template)
47
-
48
- # Define the LLM chain with the specified prompt
49
- llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k")
50
- llm_chain = LLMChain(llm=llm, prompt=prompt)
51
-
52
- # Initialize StuffDocumentsChain
53
- stuff_chain = StuffDocumentsChain(
54
- llm_chain=llm_chain, document_variable_name="text"
55
- )
56
-
57
- # Generate summary using StuffDocumentsChain
58
  global full_summary
59
- full_summary = stuff_chain.run(documents)
60
- # Update the state variable
61
- return {summary_state: full_summary}
62
-
63
- # Other existing logic for Chroma, embeddings, and retrieval
64
- embeddings = OpenAIEmbeddings()
65
- global db
66
- db = Chroma.from_documents(documents, embeddings)
67
-
68
- retriever = db.as_retriever()
69
- global qa
70
- qa = ConversationalRetrievalChain.from_llm(
71
- llm=OpenAI(temperature=0.2, model_name="gpt-3.5-turbo-16k", max_tokens=-1, n=2),
72
- retriever=retriever,
73
- return_source_documents=False
74
- )
75
  summary_box.set_value(full_summary)
76
  return f"Ready. Full Summary loaded."
77
-
78
  except Exception as e:
79
  return f"Error processing PDF: {str(e)}"
80
 
@@ -179,7 +177,7 @@ with gr.Blocks(css=css) as demo:
179
  question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
180
  submit_btn = gr.Button("Send Message")
181
 
182
- load_pdf.click(loading_pdf, None, langchain_status, queue=False)
183
  load_pdf.click(pdf_changes, inputs=[pdf_doc], outputs=[langchain_status], queue=False).then(
184
  update_summary_box
185
  )
@@ -194,4 +192,4 @@ with gr.Blocks(css=css) as demo:
194
  bot, chatbot, chatbot
195
  )
196
 
197
- demo.launch()
 
30
  return f"Number of documents: {num_documents}, Average document length: {avg_doc_length}"
31
 
32
  # Gradio state
33
+ summary_state = gr.State(initial_value="")
34
+
35
+ # Initialize loader and load documents
36
+ def load_documents(pdf_doc):
37
+ loader = OnlinePDFLoader(pdf_doc.name)
38
+ return loader.load()
39
+
40
+ # Generate summary using StuffDocumentsChain
41
+ def generate_summary(documents):
42
+ prompt_template = """Write a concise summary of the following:
43
+ "{text}"
44
+ CONCISE SUMMARY:"""
45
+ prompt = PromptTemplate.from_template(prompt_template)
46
+ llm = ChatOpenAI(temperature=0, model_name="gpt-3.5-turbo-16k")
47
+ llm_chain = LLMChain(llm=llm, prompt=prompt)
48
+ stuff_chain = StuffDocumentsChain(
49
+ llm_chain=llm_chain, document_variable_name="text"
50
+ )
51
+ return stuff_chain.run(documents)
52
+
53
+ # Setup Chroma, embeddings, and retrieval
54
+ def setup_retrieval(documents):
55
+ embeddings = OpenAIEmbeddings()
56
+ db = Chroma.from_documents(documents, embeddings)
57
+ retriever = db.as_retriever()
58
+ qa = ConversationalRetrievalChain.from_llm(
59
+ llm=OpenAI(temperature=0.2, model_name="gpt-3.5-turbo-16k", max_tokens=-1, n=2),
60
+ retriever=retriever,
61
+ return_source_documents=False
62
+ )
63
+ return db, qa
64
 
65
+ # Main function to handle PDF changes
66
  def pdf_changes(pdf_doc):
67
  try:
68
+ documents = load_documents(pdf_doc)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
  global full_summary
70
+ full_summary = generate_summary(documents)
71
+ summary_state.value = full_summary
72
+ global db, qa
73
+ db, qa = setup_retrieval(documents)
 
 
 
 
 
 
 
 
 
 
 
 
74
  summary_box.set_value(full_summary)
75
  return f"Ready. Full Summary loaded."
 
76
  except Exception as e:
77
  return f"Error processing PDF: {str(e)}"
78
 
 
177
  question = gr.Textbox(label="Question", placeholder="Type your question and hit Enter")
178
  submit_btn = gr.Button("Send Message")
179
 
180
+ load_pdf.click(loading_pdf, None, langchain_status)
181
  load_pdf.click(pdf_changes, inputs=[pdf_doc], outputs=[langchain_status], queue=False).then(
182
  update_summary_box
183
  )
 
192
  bot, chatbot, chatbot
193
  )
194
 
195
+ demo.launch()