Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -73,8 +73,8 @@ PQC_rules="""
|
|
| 73 |
|
| 74 |
#===========================
|
| 75 |
#retriever = retrieve_chunks(repo_id)
|
| 76 |
-
retriever=retrieve_chunks_GPC()
|
| 77 |
-
def create_qa_chain():
|
| 78 |
prompt = ChatPromptTemplate.from_template(
|
| 79 |
"Use context to answer: {context}\n\nQ: {input}"
|
| 80 |
)
|
|
@@ -87,7 +87,7 @@ def create_qa_chain():
|
|
| 87 |
)
|
| 88 |
return chain
|
| 89 |
|
| 90 |
-
|
| 91 |
#=======================
|
| 92 |
def chat(message, history):
|
| 93 |
answer = qa_chain.invoke(message)
|
|
@@ -98,7 +98,8 @@ def chat(message, history):
|
|
| 98 |
history.append([message, full])
|
| 99 |
return history, ""
|
| 100 |
#============starting extract_docx_text
|
| 101 |
-
def respond(message, history):
|
|
|
|
| 102 |
word_count = len(message.strip().split())
|
| 103 |
|
| 104 |
# If less than 3 words, do not call LLM, just ask user to clarify
|
|
@@ -110,12 +111,23 @@ def respond(message, history):
|
|
| 110 |
]
|
| 111 |
return "", new_history
|
| 112 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
with get_openai_callback() as cb:
|
| 114 |
answer = qa_chain.invoke(message)
|
| 115 |
#answer = qa_chain.invoke(message)
|
| 116 |
docs = retriever.invoke(message)
|
| 117 |
-
refs
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
|
| 120 |
# CRITICAL: Append ONLY pure dicts - no metadata, tuples, or extras
|
| 121 |
new_history = history + [ # Or history.append() then return history
|
|
@@ -388,15 +400,24 @@ with gr.Blocks(css=css) as demo:
|
|
| 388 |
run_btn.click(check_compliance, inputs=inp, outputs=out)
|
| 389 |
|
| 390 |
with gr.TabItem("NRL ChatBot"):
|
| 391 |
-
gr.Markdown("""# RAG Chatbot -
|
| 392 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 393 |
chatbot = gr.Chatbot(height=500) # Defaults to messages
|
| 394 |
msg = gr.Textbox(placeholder="Ask a question...", label="Query")
|
| 395 |
submit_btn = gr.Button("Submit")
|
| 396 |
|
| 397 |
# Events
|
| 398 |
-
submit_btn.click(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 399 |
-
msg.submit(respond, inputs=[msg, chatbot], outputs=[msg, chatbot])
|
| 400 |
with gr.TabItem("Compliance Check of user technical doc"):
|
| 401 |
with gr.Row():
|
| 402 |
inp_tech = gr.File(
|
|
|
|
| 73 |
|
| 74 |
#===========================
|
| 75 |
#retriever = retrieve_chunks(repo_id)
|
| 76 |
+
#retriever=retrieve_chunks_GPC()
|
| 77 |
+
def create_qa_chain(retriever):
|
| 78 |
prompt = ChatPromptTemplate.from_template(
|
| 79 |
"Use context to answer: {context}\n\nQ: {input}"
|
| 80 |
)
|
|
|
|
| 87 |
)
|
| 88 |
return chain
|
| 89 |
|
| 90 |
+
|
| 91 |
#=======================
|
| 92 |
def chat(message, history):
|
| 93 |
answer = qa_chain.invoke(message)
|
|
|
|
| 98 |
history.append([message, full])
|
| 99 |
return history, ""
|
| 100 |
#============starting extract_docx_text
|
| 101 |
+
def respond(message, history, doc_choice):
|
| 102 |
+
|
| 103 |
word_count = len(message.strip().split())
|
| 104 |
|
| 105 |
# If less than 3 words, do not call LLM, just ask user to clarify
|
|
|
|
| 111 |
]
|
| 112 |
return "", new_history
|
| 113 |
else:
|
| 114 |
+
if doc_choice == "gpc_goods":
|
| 115 |
+
retriever=retrieve_chunks_GPC()
|
| 116 |
+
else:
|
| 117 |
+
retriever = retrieve_chunks(repo_id)
|
| 118 |
+
qa_chain = create_qa_chain(retriever)
|
| 119 |
with get_openai_callback() as cb:
|
| 120 |
answer = qa_chain.invoke(message)
|
| 121 |
#answer = qa_chain.invoke(message)
|
| 122 |
docs = retriever.invoke(message)
|
| 123 |
+
refs=[]
|
| 124 |
+
if doc_choice == "gpc_goods":
|
| 125 |
+
refs= [f"NRL GPC point No: {d.metadata.get('condition_number', 'N/A')} / Heading: {d.metadata.get('condition_heading', 'N/A')}" for d in docs]
|
| 126 |
+
else:
|
| 127 |
+
refs = [f"Page {d.metadata.get('page', 'N/A')}" for d in docs]
|
| 128 |
+
full_answer = f"""Input tokens: {cb.prompt_tokens},
|
| 129 |
+
Ouput tokens: {cb.completion_tokens}, Total tokens: {cb.total_tokens},
|
| 130 |
+
Cost: ${cb.total_cost}\n{answer}\n\n**References:**\n""" + "\n".join(refs)
|
| 131 |
|
| 132 |
# CRITICAL: Append ONLY pure dicts - no metadata, tuples, or extras
|
| 133 |
new_history = history + [ # Or history.append() then return history
|
|
|
|
| 400 |
run_btn.click(check_compliance, inputs=inp, outputs=out)
|
| 401 |
|
| 402 |
with gr.TabItem("NRL ChatBot"):
|
| 403 |
+
gr.Markdown("""# RAG Chatbot - NRL Documents""")
|
| 404 |
+
# RADIO BUTTON for document selection
|
| 405 |
+
doc_selector = gr.Radio(
|
| 406 |
+
choices=[
|
| 407 |
+
("GPC Goods", "gpc_goods"),
|
| 408 |
+
("Procurement Manual", "manual")
|
| 409 |
+
],
|
| 410 |
+
value="gpc_goods", # Default
|
| 411 |
+
label="Select Document:",
|
| 412 |
+
info="Choose which document to query"
|
| 413 |
+
)
|
| 414 |
chatbot = gr.Chatbot(height=500) # Defaults to messages
|
| 415 |
msg = gr.Textbox(placeholder="Ask a question...", label="Query")
|
| 416 |
submit_btn = gr.Button("Submit")
|
| 417 |
|
| 418 |
# Events
|
| 419 |
+
submit_btn.click(respond, inputs=[msg, chatbot, doc_selector], outputs=[msg, chatbot])
|
| 420 |
+
msg.submit(respond, inputs=[msg, chatbot, doc_selector], outputs=[msg, chatbot])
|
| 421 |
with gr.TabItem("Compliance Check of user technical doc"):
|
| 422 |
with gr.Row():
|
| 423 |
inp_tech = gr.File(
|