Spaces:
Sleeping
Sleeping
Update app.py
Browse files12 unmodified lines13141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777812 unmodified linesfrom llama_index.core.memory import ChatMemoryBufferfrom llama_index.core.chat_engine import CondensePlusContextChatEngine# os.environ["OPENAI_API_KEY"] =os.getenv("OPENAI_API_KEY")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(share=True)if __name__ == "__main__":demo.launch()12 unmodified lines13141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810912 unmodified linesfrom llama_index.core.memory import ChatMemoryBufferfrom llama_index.core.chat_engine import CondensePlusContextChatEnginepswd = os.getenv("PSWD")# Buat hash dari password asli (ini bisa dilakukan sekali dan disimpan dalam database)hashed_password = bcrypt.hashpw(pswd.encode('utf-8'), bcrypt.gensalt())def main():# Cek apakah pengguna sudah loginif 'logged_in' in st.session_state and st.session_state.logged_in:show_main_page()else:show_login_page()def show_login_page():st.title("Login")st.write("Please enter your password to access the application.")# Input passwordpassword = st.text_input("Password", type="password")if st.button("Login"):if bcrypt.checkpw(password.encode('utf-8'), hashed_password):st.session_state.logged_in = Truest.experimental_rerun()else:st.error("Incorrect password. Please try again.")def show_main_page():# os.environ["OPENAI_API_KEY"] =os.getenv("OPENAI_API_KEY")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(share=True)if __name__ == "__main__":main()
app.py+83-52