Spaces:
Sleeping
Sleeping
Update app.py
Browse files21 unmodified lines2223242526272526302728293023 unmodified lines54555660576263585965606162686970716364654 unmodified lines70717282838485868788899091929394959697989910073747576777879808182838485868788899091929394951039621 unmodified lines# Create hash from the original passwordhashed_password = bcrypt.hashpw(pswd.encode('utf-8'), bcrypt.gensalt())
# Global variable to track login statusis_logged_in = False
# Initialize OpenAI client and LLMclient = OpenAI(api_key=api_key)llm = OpenAI(temperature=0.2, model="gpt-4-turbo-preview")llm = LlamaOpenAI(temperature=0.2, model="gpt-4-turbo-preview")
# Set up the indexPERSIST_DIR = "storage"23 unmodified lineschat_engine.reset()
def login(password): global is_logged_in if bcrypt.checkpw(password.encode('utf-8'), hashed_password): is_logged_in = True return "Login successful! You can now access the chatbot." return gr.update(visible=False), gr.update(visible=True) else: return "Incorrect password. Please try again." return gr.update(visible=True), gr.update(visible=False)
def generate_response(message, history): if not is_logged_in: yield "Please log in to use the chatbot." return
response = chat_engine.stream_chat(message) text = "" for token in response.response_gen:4 unmodified lines chat_engine.reset() return None
# Create the login interfacelogin_interface = gr.Interface( fn=login, inputs=gr.Textbox(type="password", label="Password"), outputs="text", title="Login", description="Please enter your password to access the chatbot.")
# Create the chatbot interfacechatbot_interface = gr.ChatInterface( fn=generate_response, title="RAG Chatbot", description="Chat with our AI assistant about Ministry of Transportation regulations. Please log in first to use this feature.", clear_btn=gr.Button("Clear History"))
# Combine both interfacesapp = gr.TabbedInterface([login_interface, chatbot_interface], ["Login", "Chatbot"])with gr.Blocks() as demo: with gr.Row(): with gr.Column(scale=1, min_width=300): login_interface = gr.Interface( fn=login, inputs=gr.Textbox(type="password", label="Password"), outputs=[gr.Row(visible=True), gr.Row(visible=False)], title="Login", description="Please enter your password to access the chatbot." ) with gr.Column(scale=3, min_width=700): with gr.Row(visible=False) as chatbot_row: chatbot_interface = gr.ChatInterface( fn=generate_response, title="RAG Chatbot", description="Chat with our AI assistant about Ministry of Transportation regulations.", clear_btn=gr.Button("Clear History") ) login_interface.submit(fn=login, inputs=[login_interface.inputs[0]], outputs=[login_interface.elem_id, chatbot_row])
if __name__ == "__main__": app.launch(share=True) demo.launch(share=True)app.py+25-32