Spaces:
Running
Running
added tabs
Browse files- app.py +140 -41
- app_old.py → app_chatbot.py +0 -0
- app_passagefinder.py +86 -0
app.py
CHANGED
@@ -1,11 +1,16 @@
|
|
1 |
import gradio as gr
|
2 |
from langchain_core.messages import HumanMessage
|
3 |
import src.passage_finder as pf
|
|
|
|
|
|
|
4 |
|
5 |
-
# Initialize PassageFinder
|
6 |
passage_finder = pf.PassageFinder()
|
|
|
7 |
|
8 |
-
|
|
|
9 |
config = passage_finder.get_configurable()
|
10 |
results = passage_finder.graph.invoke({"messages": [HumanMessage(content=message)]}, config)
|
11 |
|
@@ -27,8 +32,8 @@ def respond(message):
|
|
27 |
|
28 |
return output
|
29 |
|
30 |
-
def
|
31 |
-
results =
|
32 |
html_output = "<div class='response-container'>"
|
33 |
for result in results:
|
34 |
html_output += f"""
|
@@ -44,43 +49,137 @@ def process_input(message):
|
|
44 |
html_output += "</div>"
|
45 |
return html_output
|
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 |
-
"Importance of good posture",
|
80 |
-
"How can I find inner peace?",
|
81 |
-
"What does Paramahansa Yogananda say about meditation?",
|
82 |
-
],
|
83 |
-
inputs=input_text,
|
84 |
-
)
|
85 |
|
86 |
-
|
|
|
|
1 |
import gradio as gr
|
2 |
from langchain_core.messages import HumanMessage
|
3 |
import src.passage_finder as pf
|
4 |
+
import src.srf_bot as sb
|
5 |
+
import prompts.system_prompts as sp
|
6 |
+
import os
|
7 |
|
8 |
+
# Initialize PassageFinder and Chatbot
|
9 |
passage_finder = pf.PassageFinder()
|
10 |
+
chatbot = sb.SRFChatbot()
|
11 |
|
12 |
+
# Passage Finder functions
|
13 |
+
def respond_passage_finder(message):
|
14 |
config = passage_finder.get_configurable()
|
15 |
results = passage_finder.graph.invoke({"messages": [HumanMessage(content=message)]}, config)
|
16 |
|
|
|
32 |
|
33 |
return output
|
34 |
|
35 |
+
def process_input_passage_finder(message):
|
36 |
+
results = respond_passage_finder(message)
|
37 |
html_output = "<div class='response-container'>"
|
38 |
for result in results:
|
39 |
html_output += f"""
|
|
|
49 |
html_output += "</div>"
|
50 |
return html_output
|
51 |
|
52 |
+
# Chatbot functions
|
53 |
+
def respond_chatbot(query, history):
|
54 |
+
formatted_query = [HumanMessage(content=query)]
|
55 |
+
# Invoke the graph with properly formatted input
|
56 |
+
result = chatbot.graph.invoke({"messages": formatted_query}, chatbot.config)
|
57 |
+
# Get the passages from the graph and append to history if documents exist
|
58 |
+
state = chatbot.graph.get_state(config=chatbot.config).values
|
59 |
+
documents = state.get("documents")
|
60 |
+
passages = ''
|
61 |
+
if documents and len(documents) > 0:
|
62 |
+
for d in documents:
|
63 |
+
passages += f'<b>{d.metadata["publication_name"]} - {d.metadata["chapter_name"]}</b>\n{d.page_content}\n\n'
|
64 |
+
history.append((f'Passages: {query}', passages))
|
65 |
+
# Extract the assistant's response and append to history
|
66 |
+
response = result["messages"][-1].content
|
67 |
+
system_message_dropdown = state.get("system_message_dropdown")
|
68 |
+
history.append((query, f"<i>[{system_message_dropdown}]</i>\n" + response))
|
69 |
+
return history
|
70 |
+
|
71 |
+
# Define the CSS
|
72 |
+
css = """
|
73 |
+
body { background-color: #f0f0f0; }
|
74 |
+
.gradio-container { background-color: #ffffff; }
|
75 |
+
.response-container { border: 1px solid #e0e0e0; border-radius: 8px; padding: 20px; background-color: #f9f9f9; }
|
76 |
+
.result-item { margin-bottom: 20px; background-color: white; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
|
77 |
+
.reference { color: #2c3e50; margin-bottom: 10px; }
|
78 |
+
.quotes { font-style: italic; margin-bottom: 10px; }
|
79 |
+
.full-passage { margin-top: 10px; padding: 10px; background-color: #f0f0f0; border-radius: 5px; }
|
80 |
+
details summary { cursor: pointer; color: #3498db; font-weight: bold; }
|
81 |
+
details summary:hover { text-decoration: underline; }
|
82 |
+
|
83 |
+
/* Chatbot specific styles */
|
84 |
+
.gr-button { background-color: #333333; color: white; font-size: 18px; padding: 10px; }
|
85 |
+
.gr-textbox textarea { font-size: 18px; color: black; }
|
86 |
+
.gr-dropdown { font-size: 18px; color: black; }
|
87 |
+
.source-box { background-color: white; padding: 10px; border-radius: 8px; margin-top: 20px; color: black; border: 1px solid #D0D0D0; }
|
88 |
+
|
89 |
+
/* Dark mode and responsive styles */
|
90 |
+
@media (prefers-color-scheme: dark) {
|
91 |
+
.gradio-container { background-color: #1e1e1e; color: white; }
|
92 |
+
h1, h2, p { color: white; }
|
93 |
+
.gr-textbox textarea { background-color: #333333; color: white; }
|
94 |
+
.gr-button { background-color: #555555; color: white; }
|
95 |
+
.gr-dropdown { background-color: #333333; color: white; }
|
96 |
+
.source-box { background-color: #333333; color: white; border: 1px solid #555555; }
|
97 |
+
}
|
98 |
+
|
99 |
+
@media (max-width: 600px) {
|
100 |
+
.gr-row { flex-direction: column !important; }
|
101 |
+
.gr-column { width: 100% !important; }
|
102 |
+
}
|
103 |
+
"""
|
104 |
+
|
105 |
+
with gr.Blocks(css=css) as demo:
|
106 |
+
gr.Markdown("# SRF Teachings App")
|
107 |
|
108 |
+
with gr.Tabs():
|
109 |
+
with gr.TabItem("Passage Finder"):
|
110 |
+
gr.Markdown("Ask questions about Self-Realization Fellowship teachings and receive responses with relevant quotes.")
|
111 |
+
|
112 |
+
with gr.Row():
|
113 |
+
input_text_pf = gr.Textbox(
|
114 |
+
placeholder="Ask about the meaning of life, spirituality, or any other topic...",
|
115 |
+
label="Your Question"
|
116 |
+
)
|
117 |
+
submit_btn_pf = gr.Button("Submit", variant="primary")
|
118 |
+
|
119 |
+
output_area_pf = gr.HTML()
|
120 |
+
|
121 |
+
gr.Markdown("### Sources")
|
122 |
+
gr.Textbox(value="Journey to Self Realization, Second Coming of Christ, and Autobiography of a Yogi",
|
123 |
+
label="Available Sources", interactive=False)
|
124 |
+
|
125 |
+
submit_btn_pf.click(process_input_passage_finder, inputs=input_text_pf, outputs=output_area_pf)
|
126 |
+
|
127 |
+
gr.Examples(
|
128 |
+
examples=[
|
129 |
+
"What is the meaning of life?",
|
130 |
+
"Importance of good posture",
|
131 |
+
"How can I find inner peace?",
|
132 |
+
"What does Paramahansa Yogananda say about meditation?",
|
133 |
+
],
|
134 |
+
inputs=input_text_pf,
|
135 |
+
)
|
136 |
+
|
137 |
+
with gr.TabItem("Chatbot"):
|
138 |
+
with gr.Row():
|
139 |
+
with gr.Column(scale=4):
|
140 |
+
chatbot_output = gr.Chatbot(height=600)
|
141 |
+
user_input_cb = gr.Textbox(placeholder="Type your question here...", label="Your Question", value="What is the meaning of life?")
|
142 |
+
submit_button_cb = gr.Button("Submit")
|
143 |
+
|
144 |
+
with gr.Column(scale=1):
|
145 |
+
system_prompt_dropdown = gr.Dropdown(
|
146 |
+
choices=list(sp.system_prompt_templates.keys()),
|
147 |
+
label="Select Chatbot Instructions",
|
148 |
+
value=list(sp.system_prompt_templates.keys())[0],
|
149 |
+
)
|
150 |
+
system_prompt_display = gr.Textbox(
|
151 |
+
value=sp.system_prompt_templates[list(sp.system_prompt_templates.keys())[0]],
|
152 |
+
label="Current Chatbot Instructions",
|
153 |
+
lines=5,
|
154 |
+
interactive=False
|
155 |
+
)
|
156 |
+
|
157 |
+
gr.Markdown("""
|
158 |
+
<div class="source-box">
|
159 |
+
<strong>Available sources:</strong>
|
160 |
+
<ul>
|
161 |
+
<li>Journey to Self-Realization</li>
|
162 |
+
<li>The Second Coming of Christ</li>
|
163 |
+
<li>Autobiography of a Yogi</li>
|
164 |
+
</ul>
|
165 |
+
</div>
|
166 |
+
""")
|
167 |
+
|
168 |
+
system_prompt_dropdown.change(
|
169 |
+
fn=chatbot.reset_system_prompt,
|
170 |
+
inputs=[system_prompt_dropdown],
|
171 |
+
outputs=[system_prompt_display]
|
172 |
+
)
|
173 |
+
|
174 |
+
submit_button_cb.click(
|
175 |
+
fn=respond_chatbot,
|
176 |
+
inputs=[user_input_cb, chatbot_output],
|
177 |
+
outputs=[chatbot_output]
|
178 |
+
)
|
179 |
|
180 |
+
# Access the secrets
|
181 |
+
username = os.getenv("USERNAME")
|
182 |
+
password = os.getenv("PASSWORD")
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
|
184 |
+
# Launch the interface
|
185 |
+
demo.launch(share=True, auth=(username, password), debug=True)
|
app_old.py → app_chatbot.py
RENAMED
File without changes
|
app_passagefinder.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from langchain_core.messages import HumanMessage
|
3 |
+
import src.passage_finder as pf
|
4 |
+
|
5 |
+
# Initialize PassageFinder
|
6 |
+
passage_finder = pf.PassageFinder()
|
7 |
+
|
8 |
+
def respond(message):
|
9 |
+
config = passage_finder.get_configurable()
|
10 |
+
results = passage_finder.graph.invoke({"messages": [HumanMessage(content=message)]}, config)
|
11 |
+
|
12 |
+
documents = results.get('documents', [])
|
13 |
+
|
14 |
+
output = []
|
15 |
+
for doc in documents:
|
16 |
+
quotes = doc.metadata.get('matched_quotes', [])
|
17 |
+
publication = doc.metadata.get('publication_name', 'Unknown Publication')
|
18 |
+
chapter = doc.metadata.get('chapter_name', 'Unknown Chapter')
|
19 |
+
full_passage = doc.metadata.get('highlighted_content', '')
|
20 |
+
|
21 |
+
quote_text = "\n".join([f"• \"{q.quote}\"" for q in quotes])
|
22 |
+
output.append({
|
23 |
+
"quotes": quote_text,
|
24 |
+
"reference": f"{publication}: {chapter}",
|
25 |
+
"full_passage": full_passage
|
26 |
+
})
|
27 |
+
|
28 |
+
return output
|
29 |
+
|
30 |
+
def process_input(message):
|
31 |
+
results = respond(message)
|
32 |
+
html_output = "<div class='response-container'>"
|
33 |
+
for result in results:
|
34 |
+
html_output += f"""
|
35 |
+
<div class='result-item'>
|
36 |
+
<h3 class='reference'>{result['reference']}</h3>
|
37 |
+
<div class='quotes'>{result['quotes'].replace("• ", "<br>• ")}</div>
|
38 |
+
<details>
|
39 |
+
<summary>Show full passage</summary>
|
40 |
+
<div class='full-passage'>{result['full_passage']}</div>
|
41 |
+
</details>
|
42 |
+
</div>
|
43 |
+
"""
|
44 |
+
html_output += "</div>"
|
45 |
+
return html_output
|
46 |
+
|
47 |
+
with gr.Blocks(css="""
|
48 |
+
body { background-color: #f0f0f0; }
|
49 |
+
.gradio-container { background-color: #ffffff; }
|
50 |
+
.response-container { border: 1px solid #e0e0e0; border-radius: 8px; padding: 20px; background-color: #f9f9f9; }
|
51 |
+
.result-item { margin-bottom: 20px; background-color: white; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }
|
52 |
+
.reference { color: #2c3e50; margin-bottom: 10px; }
|
53 |
+
.quotes { font-style: italic; margin-bottom: 10px; }
|
54 |
+
.full-passage { margin-top: 10px; padding: 10px; background-color: #f0f0f0; border-radius: 5px; }
|
55 |
+
details summary { cursor: pointer; color: #3498db; font-weight: bold; }
|
56 |
+
details summary:hover { text-decoration: underline; }
|
57 |
+
""") as demo:
|
58 |
+
gr.Markdown("# SRF Teachings Chatbot")
|
59 |
+
gr.Markdown("Ask questions about Self-Realization Fellowship teachings and receive responses with relevant quotes.")
|
60 |
+
|
61 |
+
with gr.Row():
|
62 |
+
input_text = gr.Textbox(
|
63 |
+
placeholder="Ask about the meaning of life, spirituality, or any other topic...",
|
64 |
+
label="Your Question"
|
65 |
+
)
|
66 |
+
submit_btn = gr.Button("Submit", variant="primary")
|
67 |
+
|
68 |
+
output_area = gr.HTML()
|
69 |
+
|
70 |
+
gr.Markdown("### Sources")
|
71 |
+
gr.Textbox(value="Journey to Self Realization, Second Coming of Christ, and Autobiography of a Yogi",
|
72 |
+
label="Available Sources", interactive=False)
|
73 |
+
|
74 |
+
submit_btn.click(process_input, inputs=input_text, outputs=output_area)
|
75 |
+
|
76 |
+
gr.Examples(
|
77 |
+
examples=[
|
78 |
+
"What is the meaning of life?",
|
79 |
+
"Importance of good posture",
|
80 |
+
"How can I find inner peace?",
|
81 |
+
"What does Paramahansa Yogananda say about meditation?",
|
82 |
+
],
|
83 |
+
inputs=input_text,
|
84 |
+
)
|
85 |
+
|
86 |
+
demo.launch()
|