Spaces:
Runtime error
Runtime error
File size: 5,579 Bytes
4f82820 |
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
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()
|