Spaces:
Running
Running
import gradio as gr | |
from rag import get_best_answer | |
# Custom CSS for the interface | |
css = """ | |
#chatbot { | |
height: 350px; | |
overflow: auto; | |
border-radius: 10px; | |
border: 1px solid #e0e0e0; | |
} | |
.textbox { | |
border-radius: 20px !important; | |
padding: 12px 20px !important; | |
} | |
.btn-column { | |
display: flex; | |
flex-direction: column; | |
gap: 10px; | |
} | |
""" | |
def create_interface(): | |
with gr.Blocks(css=css, theme="soft") as demo: | |
gr.Markdown(""" | |
<h1 style='text-align: center;'>University of Education Lahore Chatbot</h1> | |
<p style='text-align: center;'>Official AI Assistant for University Information</p> | |
""") | |
# Define the chat interface | |
chatbot = gr.Chatbot(elem_id="chatbot") | |
examples = [ | |
"What are the admission requirements?", | |
"How can I contact the administration?", | |
"What programs are offered?" | |
] | |
with gr.Row(): | |
message = gr.Textbox( | |
label="Type your question here", | |
placeholder="Ask about admissions, programs, or university services...", | |
elem_classes="textbox", | |
scale=4 | |
) | |
with gr.Column(scale=1, elem_classes="btn-column"): | |
submit_button = gr.Button("↩️ Enter") | |
reset_button = gr.Button("🗑️ Reset Chat") | |
# Set up both Enter key and button to trigger the response | |
def respond(message, chat_history): | |
bot_message = get_best_answer(message) | |
chat_history.append((message, bot_message)) | |
return "", chat_history | |
message.submit(respond, [message, chatbot], [message, chatbot]) | |
submit_button.click(respond, [message, chatbot], [message, chatbot]) | |
# Reset button to clear history | |
def reset_conversation(): | |
return [] | |
reset_button.click(reset_conversation, [], [chatbot]) | |
gr.Examples(examples, inputs=message) | |
return demo | |
if __name__ == "__main__": | |
demo = create_interface() | |
demo.launch() |