Spaces:
Sleeping
Sleeping
File size: 4,278 Bytes
43139c6 dfbf21d 43139c6 dfbf21d 43139c6 dfbf21d 43139c6 dfbf21d 43139c6 dfbf21d 43139c6 dfbf21d 43139c6 dfbf21d 43139c6 dfbf21d 43139c6 dfbf21d 43139c6 |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
import gradio as gr
import src.srf_bot as sb
import prompts.system_prompts as sp
from langchain_core.messages import HumanMessage
# Initialize chatbot
chatbot = sb.SRFChatbot()
# Define the respond function
def respond(query, history):
formatted_query = [HumanMessage(content=query)]
# Invoke the graph with properly formatted input
result = chatbot.graph.invoke({"messages": formatted_query}, chatbot.config)
# Get the passages from the graph and append to history if documents exist
state = chatbot.graph.get_state(config=chatbot.config).values
documents = state.get("documents")
passages = ''
if documents and len(documents) > 0:
for d in documents:
passages += f'<b>{d.metadata["publication_name"]} - {d.metadata["chapter_name"]}</b>\n{d.page_content}\n\n'
history.append((f'Passages: {query}', passages))
# Extract the assistant's response and append to history
response = result["messages"][-1].content
system_message_dropdown = state.get("system_message_dropdown")
history.append((query, f"<i>[{system_message_dropdown}]</i>\n" + response))
return history
# Gradio interface with black and grey color scheme
with gr.Blocks(css="""
.gradio-container {
background-color: #F0F0F0;
font-family: 'Arial', sans-serif;
}
h1, h2, p {
color: black;
}
h1 {
font-size: 32px;
text-align: left;
}
h2 {
font-size: 24px;
}
p {
font-size: 18px;
margin-bottom: 15px;
}
.gr-button {
background-color: #333333;
color: white;
font-size: 18px;
padding: 10px;
}
.gr-textbox textarea {
font-size: 18px;
color: black;
}
.gr-dropdown {
font-size: 18px;
color: black;
}
.source-box {
background-color: white;
padding: 10px;
border-radius: 8px;
margin-top: 20px;
color: black;
border: 1px solid #D0D0D0;
}
@media (max-width: 600px) {
.gr-row { flex-direction: column !important; }
.gr-column { width: 100% !important; }
}
""") as demo:
# Title
gr.Markdown("# SRF Chatbot")
with gr.Row(elem_classes="gr-row"):
with gr.Column(scale=4, elem_classes="gr-column"):
# Chatbot interface
chatbot_output = gr.Chatbot(height=600) # Increased height for longer chat interface
user_input = gr.Textbox(placeholder="Type your question here...", label="Your Question", value="What is the meaning of life?")
submit_button = gr.Button("Submit")
with gr.Column(scale=1, elem_classes="gr-column"):
# Dropdown to select system prompts
system_prompt_dropdown = gr.Dropdown(
choices=list(sp.system_prompt_templates.keys()),
label="Select Chatbot Instructions",
value=list(sp.system_prompt_templates.keys())[0],
elem_classes="gr-dropdown"
)
# Display the selected system prompt
system_prompt_display = gr.Textbox(
value=sp.system_prompt_templates[list(sp.system_prompt_templates.keys())[0]],
label="Current Chatbot Instructions",
lines=5,
interactive=False
)
# Sources box (Now white, matching the other boxes)
gr.Markdown("""
<div class="source-box">
<strong>Available sources:</strong>
<ul>
<li>Journey to Self-Realization</li>
<li>The Second Coming of Christ</li>
<li>Autobiography of a Yogi</li>
</ul>
</div>
""")
# Update system prompt display when a new prompt is selected
system_prompt_dropdown.change(
fn=chatbot.reset_system_prompt,
inputs=[system_prompt_dropdown],
outputs=[system_prompt_display]
)
# Submit button logic to handle chatbot conversation
submit_button.click(
fn=respond,
inputs=[user_input, chatbot_output],
outputs=[chatbot_output]
)
# Launch the interface
demo.launch()
|