Spaces:
Sleeping
Sleeping
Update app.py
Browse files21 unmodified lines22232425262728293031323323 unmodified lines5758596061626364656667686970717273744 unmodified lines79808182838485868788899091929394959697989910010110210321 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")# Set up the indexPERSIST_DIR = "storage"23 unmodified lineschat_engine.reset()def login(password):global is_logged_inif bcrypt.checkpw(password.encode('utf-8'), hashed_password):is_logged_in = Truereturn "Login successful! You can now access the chatbot."else:return "Incorrect password. Please try again."def generate_response(message, history):if not is_logged_in:yield "Please log in to use the chatbot."returnresponse = chat_engine.stream_chat(message)text = ""for token in response.response_gen:4 unmodified lineschat_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"])if __name__ == "__main__":app.launch(share=True)21 unmodified lines22232425262728293023 unmodified lines5455565758596061626364654 unmodified lines70717273747576777879808182838485868788899091929394959621 unmodified lines# Create hash from the original passwordhashed_password = bcrypt.hashpw(pswd.encode('utf-8'), bcrypt.gensalt())# Initialize OpenAI client and LLMclient = OpenAI(api_key=api_key)llm = LlamaOpenAI(temperature=0.2, model="gpt-4-turbo-preview")# Set up the indexPERSIST_DIR = "storage"23 unmodified lineschat_engine.reset()def login(password):if bcrypt.checkpw(password.encode('utf-8'), hashed_password):return gr.update(visible=False), gr.update(visible=True)else:return gr.update(visible=True), gr.update(visible=False)def generate_response(message, history):response = chat_engine.stream_chat(message)text = ""for token in response.response_gen:4 unmodified lineschat_engine.reset()return Nonewith 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__":demo.launch(share=True)
app.py+25-32