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+
import knowledge_base
|
| 5 |
+
import time
|
| 6 |
+
|
| 7 |
+
# --- Initial Setup ---
|
| 8 |
+
# Ensure the database and knowledge base are ready on startup.
|
| 9 |
+
print("Performing initial setup...")
|
| 10 |
+
knowledge_base.create_knowledge_base()
|
| 11 |
+
auth.create_user_table()
|
| 12 |
+
# Initialize the agent system (this can take a moment as models are loaded)
|
| 13 |
+
agent_executor = agent_system.create_agent_system()
|
| 14 |
+
print("Setup complete. Launching application.")
|
| 15 |
+
|
| 16 |
+
# --- Gradio Application Logic ---
|
| 17 |
+
|
| 18 |
+
def login_user(identifier, password, session_state):
|
| 19 |
+
"""Handles the user login process."""
|
| 20 |
+
user = auth.verify_user(identifier, password)
|
| 21 |
+
if user:
|
| 22 |
+
session_state['logged_in'] = True
|
| 23 |
+
session_state['user'] = user
|
| 24 |
+
# Switch UI views on successful login
|
| 25 |
+
return (
|
| 26 |
+
session_state,
|
| 27 |
+
gr.update(visible=False), # Hide login block
|
| 28 |
+
gr.update(visible=True), # Show chat block
|
| 29 |
+
gr.update(visible=user['role'] == 'faculty') # Show/hide faculty panel
|
| 30 |
+
)
|
| 31 |
+
else:
|
| 32 |
+
# Show an error message on failed login
|
| 33 |
+
return (
|
| 34 |
+
session_state,
|
| 35 |
+
gr.update(),
|
| 36 |
+
gr.update(),
|
| 37 |
+
gr.update(visible=False)
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
def logout_user(session_state):
|
| 41 |
+
"""Handles the user logout process."""
|
| 42 |
+
session_state['logged_in'] = False
|
| 43 |
+
session_state['user'] = None
|
| 44 |
+
# Reset UI to the login screen
|
| 45 |
+
return (
|
| 46 |
+
session_state,
|
| 47 |
+
gr.update(visible=True), # Show login block
|
| 48 |
+
gr.update(visible=False), # Hide chat block
|
| 49 |
+
gr.update(visible=False), # Hide faculty panel
|
| 50 |
+
, # Clear chatbot history
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
def enroll_student(email, enrollment_no, password, session_state):
|
| 54 |
+
"""Handles the student enrollment process by a faculty member."""
|
| 55 |
+
if not session_state.get('logged_in') or session_state['user']['role']!= 'faculty':
|
| 56 |
+
return "Error: Only faculty can enroll students."
|
| 57 |
+
|
| 58 |
+
faculty_id = session_state['user']['id']
|
| 59 |
+
message = auth.add_student(email, enrollment_no, password, faculty_id)
|
| 60 |
+
return message
|
| 61 |
+
|
| 62 |
+
def chat_response(message, history, session_state):
|
| 63 |
+
"""
|
| 64 |
+
Main chat function that gets a response from the agent system.
|
| 65 |
+
"""
|
| 66 |
+
if not session_state.get('logged_in'):
|
| 67 |
+
yield "Your session has expired. Please log out and log back in."
|
| 68 |
+
return
|
| 69 |
+
|
| 70 |
+
user_info = session_state['user']
|
| 71 |
+
response = agent_system.run_query(agent_executor, message, user_info)
|
| 72 |
+
|
| 73 |
+
# Stream the response for a better user experience
|
| 74 |
+
for i in range(len(response)):
|
| 75 |
+
time.sleep(0.01)
|
| 76 |
+
yield response[: i+1]
|
| 77 |
+
|
| 78 |
+
# --- Gradio UI Definition ---
|
| 79 |
+
with gr.Blocks(theme=gr.themes.Soft(), title="GGITS Digital Campus Assistant") as demo:
|
| 80 |
+
# Session state to store user login information
|
| 81 |
+
session_state = gr.State({'logged_in': False, 'user': None})
|
| 82 |
+
|
| 83 |
+
# --- Login View ---
|
| 84 |
+
with gr.Blocks(visible=True) as login_block:
|
| 85 |
+
gr.Markdown("# GGITS Digital Campus Assistant Login")
|
| 86 |
+
login_identifier = gr.Textbox(label="Email or Enrollment No.", placeholder="Enter your email or enrollment number")
|
| 87 |
+
login_password = gr.Textbox(label="Password", type="password", placeholder="Enter your password")
|
| 88 |
+
login_button = gr.Button("Login")
|
| 89 |
+
|
| 90 |
+
# --- Main Chat and Faculty View ---
|
| 91 |
+
with gr.Blocks(visible=False) as chat_block:
|
| 92 |
+
with gr.Row():
|
| 93 |
+
gr.Markdown("# GGITS Digital Campus Assistant")
|
| 94 |
+
logout_button = gr.Button("Logout")
|
| 95 |
+
|
| 96 |
+
with gr.Row():
|
| 97 |
+
# Main Chat Interface
|
| 98 |
+
with gr.Column(scale=3):
|
| 99 |
+
chatbot = gr.Chatbot(
|
| 100 |
+
,
|
| 101 |
+
elem_id="chatbot",
|
| 102 |
+
bubble_full_width=False,
|
| 103 |
+
avatar_images=(None, "https://ggits.org/wp-content/uploads/2023/11/cropped-GGITS-LOGO-2-1-192x192.png"),
|
| 104 |
+
height=600
|
| 105 |
+
)
|
| 106 |
+
chat_input = gr.Textbox(
|
| 107 |
+
show_label=False,
|
| 108 |
+
placeholder="Ask me anything about GGITS...",
|
| 109 |
+
container=False,
|
| 110 |
+
scale=7,
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Faculty-only Panel
|
| 114 |
+
with gr.Column(scale=1, visible=False) as faculty_panel:
|
| 115 |
+
gr.Markdown("### Faculty Panel")
|
| 116 |
+
with gr.Accordion("Enroll New Student", open=True):
|
| 117 |
+
enroll_email = gr.Textbox(label="Student Email", placeholder="student@ggits.net")
|
| 118 |
+
enroll_enr_no = gr.Textbox(label="Enrollment No.", placeholder="0123CS211XXX")
|
| 119 |
+
enroll_password = gr.Textbox(label="Temporary Password", type="password")
|
| 120 |
+
enroll_button = gr.Button("Enroll Student")
|
| 121 |
+
enroll_status = gr.Textbox(label="Status", interactive=False)
|
| 122 |
+
|
| 123 |
+
# --- Event Handlers ---
|
| 124 |
+
login_button.click(
|
| 125 |
+
fn=login_user,
|
| 126 |
+
inputs=[login_identifier, login_password, session_state],
|
| 127 |
+
outputs=[session_state, login_block, chat_block, faculty_panel]
|
| 128 |
+
)
|
| 129 |
+
|
| 130 |
+
logout_button.click(
|
| 131 |
+
fn=logout_user,
|
| 132 |
+
inputs=[session_state],
|
| 133 |
+
outputs=[session_state, login_block, chat_block, faculty_panel, chatbot]
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
enroll_button.click(
|
| 137 |
+
fn=enroll_student,
|
| 138 |
+
inputs=[enroll_email, enroll_enr_no, enroll_password, session_state],
|
| 139 |
+
outputs=[enroll_status]
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
chat_input.submit(
|
| 143 |
+
fn=chat_response,
|
| 144 |
+
inputs=[chat_input, chatbot, session_state],
|
| 145 |
+
outputs=[chatbot]
|
| 146 |
+
)
|
| 147 |
+
chat_input.submit(lambda: "", None, chat_input)
|
| 148 |
+
|
| 149 |
+
|
| 150 |
+
if __name__ == "__main__":
|
| 151 |
+
demo.launch()
|