File size: 3,337 Bytes
973ae4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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()