Gaurav-2273 commited on
Commit
13a2c93
·
verified ·
1 Parent(s): e1fc3f5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -8
app.py CHANGED
@@ -9,7 +9,7 @@ from langchain.chains import ConversationalRetrievalChain
9
  from langchain.memory import ConversationBufferMemory
10
  from langchain_openai import ChatOpenAI
11
  from langchain_experimental.text_splitter import SemanticChunker
12
- import os
13
  openai_api_key = "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"
14
 
15
  def extract_text_from_pdf(pdf_file):
@@ -27,7 +27,6 @@ def clean_text(text):
27
  cleaned_text = re.sub(r'\b(\w+)\b(?:\s+\1\b)+', r'\1', cleaned_text)
28
  return cleaned_text.strip()
29
 
30
-
31
  def initialize_chatbot(cleaned_text, openai_api_key):
32
  embeddings = OpenAIEmbeddings(api_key=openai_api_key)
33
  text_splitter = SemanticChunker(embeddings)
@@ -49,20 +48,28 @@ def answer_query(pdf_file, question):
49
 
50
  def process_pdf_and_question(pdf_file, question, chat_history):
51
  if pdf_file is None:
52
- return chat_history + [("Please upload a PDF file.", "")]
 
53
  if not question.strip():
54
- return chat_history + [("Please enter a question.", "")]
55
-
 
56
  answer = answer_query(pdf_file, question)
57
  chat_history.append((question, answer))
58
  return chat_history
59
 
60
  with gr.Blocks() as demo:
 
61
 
62
  upload = gr.File(label="Upload PDF")
63
  chatbot = gr.Chatbot(label="Chat History")
64
- question = gr.Textbox(label="Ask a question")
65
- question.submit(process_pdf_and_question, inputs=[upload, question, chatbot], outputs=[chatbot])
 
 
 
 
 
66
 
67
  if __name__ == "__main__":
68
- demo.launch()
 
9
  from langchain.memory import ConversationBufferMemory
10
  from langchain_openai import ChatOpenAI
11
  from langchain_experimental.text_splitter import SemanticChunker
12
+
13
  openai_api_key = "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"
14
 
15
  def extract_text_from_pdf(pdf_file):
 
27
  cleaned_text = re.sub(r'\b(\w+)\b(?:\s+\1\b)+', r'\1', cleaned_text)
28
  return cleaned_text.strip()
29
 
 
30
  def initialize_chatbot(cleaned_text, openai_api_key):
31
  embeddings = OpenAIEmbeddings(api_key=openai_api_key)
32
  text_splitter = SemanticChunker(embeddings)
 
48
 
49
  def process_pdf_and_question(pdf_file, question, chat_history):
50
  if pdf_file is None:
51
+ chat_history.append(("System", "Please upload a PDF file."))
52
+ return chat_history
53
  if not question.strip():
54
+ chat_history.append(("System", "Please enter a question."))
55
+ return chat_history
56
+
57
  answer = answer_query(pdf_file, question)
58
  chat_history.append((question, answer))
59
  return chat_history
60
 
61
  with gr.Blocks() as demo:
62
+ chat_history = gr.State([])
63
 
64
  upload = gr.File(label="Upload PDF")
65
  chatbot = gr.Chatbot(label="Chat History")
66
+ question = gr.Textbox(label="Ask a question", placeholder="Type your question and hit Enter")
67
+
68
+ question.submit(
69
+ fn=process_pdf_and_question,
70
+ inputs=[upload, question, chat_history],
71
+ outputs=[chatbot],
72
+ )
73
 
74
  if __name__ == "__main__":
75
+ demo.launch()