Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import auth | |
| import agent_system | |
| import knowledge_base | |
| import time | |
| # --- Initial Setup --- | |
| # Ensure the database and knowledge base are ready on startup. | |
| print("Performing initial setup...") | |
| knowledge_base.create_knowledge_base() | |
| auth.create_user_table() | |
| # Initialize the agent system (this can take a moment as models are loaded) | |
| agent_executor = agent_system.create_agent_system() | |
| print("Setup complete. Launching application.") | |
| # --- Gradio Application Logic --- | |
| def login_user(identifier, password, session_state): | |
| """Handles the user login process.""" | |
| user = auth.verify_user(identifier, password) | |
| if user: | |
| session_state['logged_in'] = True | |
| session_state['user'] = user | |
| # Switch UI views on successful login | |
| return ( | |
| session_state, | |
| gr.update(visible=False), # Hide login block | |
| gr.update(visible=True), # Show chat block | |
| gr.update(visible=user['role'] == 'faculty') # Show/hide faculty panel | |
| ) | |
| else: | |
| # Show an error message on failed login | |
| return ( | |
| session_state, | |
| gr.update(), | |
| gr.update(), | |
| gr.update(visible=False) | |
| ) | |
| def logout_user(session_state): | |
| """Handles the user logout process.""" | |
| session_state['logged_in'] = False | |
| session_state['user'] = None | |
| # Reset UI to the login screen | |
| return ( | |
| session_state, | |
| gr.update(visible=True), # Show login block | |
| gr.update(visible=False), # Hide chat block | |
| gr.update(visible=False), # Hide faculty panel | |
| , # Clear chatbot history | |
| ) | |
| def enroll_student(email, enrollment_no, password, session_state): | |
| """Handles the student enrollment process by a faculty member.""" | |
| if not session_state.get('logged_in') or session_state['user']['role']!= 'faculty': | |
| return "Error: Only faculty can enroll students." | |
| faculty_id = session_state['user']['id'] | |
| message = auth.add_student(email, enrollment_no, password, faculty_id) | |
| return message | |
| def chat_response(message, history, session_state): | |
| """ | |
| Main chat function that gets a response from the agent system. | |
| """ | |
| if not session_state.get('logged_in'): | |
| yield "Your session has expired. Please log out and log back in." | |
| return | |
| user_info = session_state['user'] | |
| response = agent_system.run_query(agent_executor, message, user_info) | |
| # Stream the response for a better user experience | |
| for i in range(len(response)): | |
| time.sleep(0.01) | |
| yield response[: i+1] | |
| # --- Gradio UI Definition --- | |
| with gr.Blocks(theme=gr.themes.Soft(), title="GGITS Digital Campus Assistant") as demo: | |
| # Session state to store user login information | |
| session_state = gr.State({'logged_in': False, 'user': None}) | |
| # --- Login View --- | |
| with gr.Blocks(visible=True) as login_block: | |
| gr.Markdown("# GGITS Digital Campus Assistant Login") | |
| login_identifier = gr.Textbox(label="Email or Enrollment No.", placeholder="Enter your email or enrollment number") | |
| login_password = gr.Textbox(label="Password", type="password", placeholder="Enter your password") | |
| login_button = gr.Button("Login") | |
| # --- Main Chat and Faculty View --- | |
| with gr.Blocks(visible=False) as chat_block: | |
| with gr.Row(): | |
| gr.Markdown("# GGITS Digital Campus Assistant") | |
| logout_button = gr.Button("Logout") | |
| with gr.Row(): | |
| # Main Chat Interface | |
| with gr.Column(scale=3): | |
| chatbot = gr.Chatbot( | |
| , | |
| elem_id="chatbot", | |
| bubble_full_width=False, | |
| avatar_images=(None, "https://ggits.org/wp-content/uploads/2023/11/cropped-GGITS-LOGO-2-1-192x192.png"), | |
| height=600 | |
| ) | |
| chat_input = gr.Textbox( | |
| show_label=False, | |
| placeholder="Ask me anything about GGITS...", | |
| container=False, | |
| scale=7, | |
| ) | |
| # Faculty-only Panel | |
| with gr.Column(scale=1, visible=False) as faculty_panel: | |
| gr.Markdown("### Faculty Panel") | |
| with gr.Accordion("Enroll New Student", open=True): | |
| enroll_email = gr.Textbox(label="Student Email", placeholder="student@ggits.net") | |
| enroll_enr_no = gr.Textbox(label="Enrollment No.", placeholder="0123CS211XXX") | |
| enroll_password = gr.Textbox(label="Temporary Password", type="password") | |
| enroll_button = gr.Button("Enroll Student") | |
| enroll_status = gr.Textbox(label="Status", interactive=False) | |
| # --- Event Handlers --- | |
| login_button.click( | |
| fn=login_user, | |
| inputs=[login_identifier, login_password, session_state], | |
| outputs=[session_state, login_block, chat_block, faculty_panel] | |
| ) | |
| logout_button.click( | |
| fn=logout_user, | |
| inputs=[session_state], | |
| outputs=[session_state, login_block, chat_block, faculty_panel, chatbot] | |
| ) | |
| enroll_button.click( | |
| fn=enroll_student, | |
| inputs=[enroll_email, enroll_enr_no, enroll_password, session_state], | |
| outputs=[enroll_status] | |
| ) | |
| chat_input.submit( | |
| fn=chat_response, | |
| inputs=[chat_input, chatbot, session_state], | |
| outputs=[chatbot] | |
| ) | |
| chat_input.submit(lambda: "", None, chat_input) | |
| if __name__ == "__main__": | |
| demo.launch() | |