Srinivasulu kethanaboina commited on
Commit
c005a2c
·
verified ·
1 Parent(s): d7e2267

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -11
app.py CHANGED
@@ -25,10 +25,13 @@ Settings.embed_model = HuggingFaceEmbedding(
25
  PERSIST_DIR = "db"
26
  PDF_DIRECTORY = 'data' # Changed to the directory containing PDFs
27
 
28
- # Ensure PDF directory exists
29
  os.makedirs(PDF_DIRECTORY, exist_ok=True)
30
  os.makedirs(PERSIST_DIR, exist_ok=True)
31
 
 
 
 
32
  def data_ingestion_from_directory():
33
  # Use SimpleDirectoryReader on the directory containing the PDF files
34
  documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
@@ -36,7 +39,7 @@ def data_ingestion_from_directory():
36
  index = VectorStoreIndex.from_documents(documents)
37
  index.storage_context.persist(persist_dir=PERSIST_DIR)
38
 
39
- def handle_query(query, chat_history):
40
  chat_text_qa_msgs = [
41
  (
42
  "user",
@@ -57,7 +60,7 @@ def handle_query(query, chat_history):
57
 
58
  # Use chat history to enhance response
59
  context_str = ""
60
- for past_query, response in reversed(chat_history):
61
  if past_query.strip():
62
  context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
63
 
@@ -71,10 +74,10 @@ def handle_query(query, chat_history):
71
  else:
72
  response = "Sorry, I couldn't find an answer."
73
 
74
- return response
 
75
 
76
- # Initialize chat history
77
- chat_history = []
78
 
79
  # Example usage: Process PDF ingestion from directory
80
  print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
@@ -88,15 +91,14 @@ input_component = gr.Textbox(
88
 
89
  output_component = gr.Textbox()
90
 
91
- # Function to add chat history to output
92
- def chat_with_history(query):
93
- response = handle_query(query, chat_history)
94
- chat_history.append((query, response))
95
  return response
96
 
97
  # Create the Gradio interface
98
  interface = gr.Interface(
99
- fn=chat_with_history,
100
  inputs=input_component,
101
  outputs=output_component,
102
  title="RedfernsTech Q&A Chatbot",
 
25
  PERSIST_DIR = "db"
26
  PDF_DIRECTORY = 'data' # Changed to the directory containing PDFs
27
 
28
+ # Ensure directories exist
29
  os.makedirs(PDF_DIRECTORY, exist_ok=True)
30
  os.makedirs(PERSIST_DIR, exist_ok=True)
31
 
32
+ # Variable to store current chat conversation
33
+ current_chat_history = []
34
+
35
  def data_ingestion_from_directory():
36
  # Use SimpleDirectoryReader on the directory containing the PDF files
37
  documents = SimpleDirectoryReader(PDF_DIRECTORY).load_data()
 
39
  index = VectorStoreIndex.from_documents(documents)
40
  index.storage_context.persist(persist_dir=PERSIST_DIR)
41
 
42
+ def handle_query(query):
43
  chat_text_qa_msgs = [
44
  (
45
  "user",
 
60
 
61
  # Use chat history to enhance response
62
  context_str = ""
63
+ for past_query, response in reversed(current_chat_history):
64
  if past_query.strip():
65
  context_str += f"User asked: '{past_query}'\nBot answered: '{response}'\n"
66
 
 
74
  else:
75
  response = "Sorry, I couldn't find an answer."
76
 
77
+ # Update current chat history
78
+ current_chat_history.append((query, response))
79
 
80
+ return response
 
81
 
82
  # Example usage: Process PDF ingestion from directory
83
  print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
 
91
 
92
  output_component = gr.Textbox()
93
 
94
+ # Function to handle queries
95
+ def chatbot_handler(query):
96
+ response = handle_query(query)
 
97
  return response
98
 
99
  # Create the Gradio interface
100
  interface = gr.Interface(
101
+ fn=chatbot_handler,
102
  inputs=input_component,
103
  outputs=output_component,
104
  title="RedfernsTech Q&A Chatbot",