Spaces:
Sleeping
Sleeping
Update app.py
Browse files14 unmodified lines1516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959614 unmodified lines# Get password and OpenAI API key from environment variablespswd = os.getenv("PSWD")api_key = os.getenv("OPENAI_API_KEY")# 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 = OpenAI(temperature=0.2, model="gpt-4-turbo-preview")# Set up the indexPERSIST_DIR = "storage"if not os.path.exists(PERSIST_DIR):documents = SimpleDirectoryReader("olah").load_data()index = VectorStoreIndex.from_documents(documents)index.storage_context.persist(persist_dir=PERSIST_DIR)else:storage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)index = load_index_from_storage(storage_context)# Set up the chat enginememory = ChatMemoryBuffer.from_defaults(token_limit=1900)chat_engine = index.as_chat_engine(chat_mode="condense_plus_context",memory=memory,llm=llm,context_prompt=("You are a chatbot, able to have normal interactions, as well as talk ""about Regulations related to the Ministry of Transportation. ""Here are the relevant documents for the context:\n""{context_str}\n""Instruction: Use the previous chat history, or the context above, to interact and help the user."),verbose=False,)chat_engine.reset()def login(password):if bcrypt.checkpw(password.encode('utf-8'), hashed_password):return "Login successful! You can now access the chatbot.", gr.update(visible=True)else:return "Incorrect password. Please try again.", gr.update(visible=False)def generate_response(message, history):response = chat_engine.stream_chat(message)text = ""for token in response.response_gen:text += tokenyield textdef clear_history():chat_engine.reset()return Nonewith gr.Blocks() as demo:with gr.Row():with gr.Column(scale=1, min_width=300):password_input = gr.Textbox(type="password", label="Password")login_button = gr.Button("Login")login_output = gr.Textbox(label="Login Status")with gr.Column(scale=3, min_width=700):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"),visible=False)login_button.click(fn=login,inputs=[password_input],outputs=[login_output, chatbot_interface])if __name__ == "__main__":demo.launch(share=True)14 unmodified lines151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787914 unmodified lines# os.environ["OPENAI_API_KEY"] =os.getenv("OPENAI_API_KEY")pswd = os.getenv("PSWD")api_key = os.getenv("OPENAI_API_KEY")client = OpenAI(api_key=api_key)llm = OpenAI(temperature=0.2, model="gpt-4o")# check if storage already existsPERSIST_DIR = "storage"if not os.path.exists(PERSIST_DIR):# load the documents and create the indexdocuments = SimpleDirectoryReader("olah").load_data()index = VectorStoreIndex.from_documents(documents)# store it for laterindex.storage_context.persist(persist_dir=PERSIST_DIR)else:# load the existing indexstorage_context = StorageContext.from_defaults(persist_dir=PERSIST_DIR)index = load_index_from_storage(storage_context)# Either way we can now query the indexquery_engine = index.as_query_engine(llm=llm)memory = ChatMemoryBuffer.from_defaults(token_limit=1900)# Inisialisasi chat engine dengan retriever yang mengambil dokumen dari sistemchat_engine = index.as_chat_engine(chat_mode="condense_plus_context",memory=memory, # Memory digunakan untuk melacak percakapan sebelumnyallm=llm,context_prompt=("You are a chatbot, able to have normal interactions, as well as talk ""about Regulations related to the Ministry of Transportation.""Here are the relevant documents for the context:\n""{context_str}\n""Instruction: Use the previous chat history, or the context above, to interact and help the user."),verbose=False,)chat_engine.reset()def generate_response(message, history):response = chat_engine.stream_chat(message)text = ""for token in response.response_gen:text += tokenyield textdef clear_history(value):chat_engine.reset()clear_btn = gr.Button("Clear")with gr.Blocks() as demo:clear_btn.click(clear_history)gr.ChatInterface(generate_response, clear_btn=clear_btn)demo.launch(auth=("admin", pswd))# demo.launch(auth=("admin", "pass1234"))
app.py+27-44