Spaces:
Sleeping
Sleeping
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import gradio as grfrom huggingface_hub import InferenceClient"""For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference"""client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")def respond(message,history: list[tuple[str, str]],system_message,max_tokens,temperature,top_p,):messages = [{"role": "system", "content": system_message}]for val in history:if val[0]:messages.append({"role": "user", "content": val[0]})if val[1]:messages.append({"role": "assistant", "content": val[1]})messages.append({"role": "user", "content": message})response = ""for message in client.chat_completion(messages,max_tokens=max_tokens,stream=True,temperature=temperature,top_p=top_p,):token = message.choices[0].delta.contentresponse += tokenyield response"""For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface"""demo = gr.ChatInterface(respond,additional_inputs=[gr.Textbox(value="You are a friendly Chatbot.", label="System message"),gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),gr.Slider(minimum=0.1,maximum=1.0,value=0.95,step=0.05,label="Top-p (nucleus sampling)",),],)if __name__ == "__main__":demo.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778import gradio as grimport osimport os.pathfrom openai import OpenAIfrom llama_index.core import (VectorStoreIndex,SimpleDirectoryReader,StorageContext,load_index_from_storage,)import bcryptfrom llama_index.llms.openai import OpenAIfrom 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()
app.py+72-57