srf_chatbot_v2 / app_passagefinder.py
nadaaaita's picture
added tabs
973ae4d
raw
history blame
3.34 kB
import gradio as gr
from langchain_core.messages import HumanMessage
import src.passage_finder as pf
# Initialize PassageFinder
passage_finder = pf.PassageFinder()
def respond(message):
config = passage_finder.get_configurable()
results = passage_finder.graph.invoke({"messages": [HumanMessage(content=message)]}, config)
documents = results.get('documents', [])
output = []
for doc in documents:
quotes = doc.metadata.get('matched_quotes', [])
publication = doc.metadata.get('publication_name', 'Unknown Publication')
chapter = doc.metadata.get('chapter_name', 'Unknown Chapter')
full_passage = doc.metadata.get('highlighted_content', '')
quote_text = "\n".join([f"• \"{q.quote}\"" for q in quotes])
output.append({
"quotes": quote_text,
"reference": f"{publication}: {chapter}",
"full_passage": full_passage
})
return output
def process_input(message):
results = respond(message)
html_output = "<div class='response-container'>"
for result in results:
html_output += f"""
<div class='result-item'>
<h3 class='reference'>{result['reference']}</h3>
<div class='quotes'>{result['quotes'].replace("• ", "<br>• ")}</div>
<details>
<summary>Show full passage</summary>
<div class='full-passage'>{result['full_passage']}</div>
</details>
</div>
"""
html_output += "</div>"
return html_output
with gr.Blocks(css="""
body { background-color: #f0f0f0; }
.gradio-container { background-color: #ffffff; }
.response-container { border: 1px solid #e0e0e0; border-radius: 8px; padding: 20px; background-color: #f9f9f9; }
.result-item { margin-bottom: 20px; background-color: white; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
.reference { color: #2c3e50; margin-bottom: 10px; }
.quotes { font-style: italic; margin-bottom: 10px; }
.full-passage { margin-top: 10px; padding: 10px; background-color: #f0f0f0; border-radius: 5px; }
details summary { cursor: pointer; color: #3498db; font-weight: bold; }
details summary:hover { text-decoration: underline; }
""") as demo:
gr.Markdown("# SRF Teachings Chatbot")
gr.Markdown("Ask questions about Self-Realization Fellowship teachings and receive responses with relevant quotes.")
with gr.Row():
input_text = gr.Textbox(
placeholder="Ask about the meaning of life, spirituality, or any other topic...",
label="Your Question"
)
submit_btn = gr.Button("Submit", variant="primary")
output_area = gr.HTML()
gr.Markdown("### Sources")
gr.Textbox(value="Journey to Self Realization, Second Coming of Christ, and Autobiography of a Yogi",
label="Available Sources", interactive=False)
submit_btn.click(process_input, inputs=input_text, outputs=output_area)
gr.Examples(
examples=[
"What is the meaning of life?",
"Importance of good posture",
"How can I find inner peace?",
"What does Paramahansa Yogananda say about meditation?",
],
inputs=input_text,
)
demo.launch()