Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -40,16 +40,17 @@ def data_ingestion_from_directory():
|
|
40 |
index = VectorStoreIndex.from_documents(documents)
|
41 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
42 |
|
43 |
-
def handle_query(
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
chat_text_qa_msgs = [
|
45 |
(
|
46 |
"user",
|
47 |
-
""
|
48 |
-
You are now the RedFerns Tech chatbot. Your aim is to provide answers to the user based on the conversation flow only.
|
49 |
-
{context_str}
|
50 |
-
Question:
|
51 |
-
{query_str}
|
52 |
-
"""
|
53 |
)
|
54 |
]
|
55 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
@@ -58,14 +59,9 @@ def handle_query(query):
|
|
58 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
59 |
index = load_index_from_storage(storage_context)
|
60 |
|
61 |
-
# Use
|
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 |
-
|
67 |
query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
|
68 |
-
answer = query_engine.query(
|
69 |
|
70 |
if hasattr(answer, 'response'):
|
71 |
response = answer.response
|
@@ -74,8 +70,8 @@ def handle_query(query):
|
|
74 |
else:
|
75 |
response = "Sorry, I couldn't find an answer."
|
76 |
|
77 |
-
# Update
|
78 |
-
|
79 |
|
80 |
return response
|
81 |
|
@@ -83,27 +79,13 @@ def handle_query(query):
|
|
83 |
print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
|
84 |
data_ingestion_from_directory()
|
85 |
|
86 |
-
# Define the input and output components for the Gradio interface
|
87 |
-
input_component = gr.Textbox(
|
88 |
-
show_label=False,
|
89 |
-
placeholder="Ask me anything about the document..."
|
90 |
-
)
|
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.ChatInterface(
|
101 |
-
fn=
|
102 |
-
inputs=
|
103 |
-
outputs=output_component,
|
104 |
title="RedfernsTech Q&A Chatbot",
|
105 |
description="Ask me anything about the uploaded document."
|
106 |
)
|
107 |
|
108 |
# Launch the Gradio interface
|
109 |
-
interface.launch(
|
|
|
40 |
index = VectorStoreIndex.from_documents(documents)
|
41 |
index.storage_context.persist(persist_dir=PERSIST_DIR)
|
42 |
|
43 |
+
def handle_query(message, chat_history):
|
44 |
+
# Prepare the chat history for context
|
45 |
+
context_str = ""
|
46 |
+
for user_message, bot_response in chat_history:
|
47 |
+
context_str += f"User asked: '{user_message}'\nBot answered: '{bot_response}'\n"
|
48 |
+
|
49 |
+
# Prepare the chat prompt template
|
50 |
chat_text_qa_msgs = [
|
51 |
(
|
52 |
"user",
|
53 |
+
f"You are now the RedFerns Tech chatbot. Your aim is to provide answers to the user based on the conversation flow only.\n\nQuestion:\n{message}"
|
|
|
|
|
|
|
|
|
|
|
54 |
)
|
55 |
]
|
56 |
text_qa_template = ChatPromptTemplate.from_messages(chat_text_qa_msgs)
|
|
|
59 |
storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)
|
60 |
index = load_index_from_storage(storage_context)
|
61 |
|
62 |
+
# Use the Llama index to generate a response
|
|
|
|
|
|
|
|
|
|
|
63 |
query_engine = index.as_query_engine(text_qa_template=text_qa_template, context_str=context_str)
|
64 |
+
answer = query_engine.query(message)
|
65 |
|
66 |
if hasattr(answer, 'response'):
|
67 |
response = answer.response
|
|
|
70 |
else:
|
71 |
response = "Sorry, I couldn't find an answer."
|
72 |
|
73 |
+
# Update chat history with the current interaction
|
74 |
+
chat_history.append([message, response])
|
75 |
|
76 |
return response
|
77 |
|
|
|
79 |
print("Processing PDF ingestion from directory:", PDF_DIRECTORY)
|
80 |
data_ingestion_from_directory()
|
81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
82 |
# Create the Gradio interface
|
83 |
interface = gr.ChatInterface(
|
84 |
+
fn=handle_query,
|
85 |
+
inputs=gr.Textbox(label="Ask me anything about the document...", placeholder="Type your question here."),
|
|
|
86 |
title="RedfernsTech Q&A Chatbot",
|
87 |
description="Ask me anything about the uploaded document."
|
88 |
)
|
89 |
|
90 |
# Launch the Gradio interface
|
91 |
+
interface.launch()
|