Spaces:
Runtime error
Runtime error
Updating UI
Browse files24 unmodified lines252627282930313233343536373839401 unmodified line4243444546474813 unmodified lines626364656667686924 unmodified linesdef pull_relevant_info(query, top_k=3):query_embedding = model.encode(query, convert_to_tensor=True)query_embedding_normalized = query_embedding / query_embedding.norm()chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized)top_indices = torch.topk(similarities, k=top_k).indices.cpu().numpy()relevant_info = "\n\n".join([cleaned_chunks[i] for i in top_indices])return relevant_infoclient = InferenceClient("HuggingFaceH4/zephyr-7b-beta", provider="auto")1 unmodified linedef respond(message, history):info = pull_relevant_info(message, top_k=3)system_message = (f"You are a friendly chatbot. Use the following information to help answer the user's question:\n{info}\n")messages = [{"role": "system", "content": system_message}]if history:13 unmodified linesyield responsechatbot = gr.ChatInterface(respond, type="messages")chatbot.launch()24 unmodified lines2526272829303132333435363738391 unmodified line4142434445464713 unmodified lines616263646566676869707172737475767778798081828384858687888990919293949524 unmodified linesdef pull_relevant_info(query, top_k=3):query_embedding = model.encode(query, convert_to_tensor=True)query_embedding = query_embedding / query_embedding.norm()norm_chunk_embeddings = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)similarities = torch.matmul(norm_chunk_embeddings, query_embedding)top_indices = torch.topk(similarities, k=top_k).indices.cpu().numpy()relevant_info = "\n\n".join([cleaned_chunks[i] for i in top_indices])return relevant_infoclient = InferenceClient("HuggingFaceH4/zephyr-7b-beta", provider="auto")1 unmodified linedef respond(message, history):info = pull_relevant_info(message, top_k=3)system_message = (f"You are a friendly chatbot. Use the following information to help answer the user's question:\n\n{info}\n\n")messages = [{"role": "system", "content": system_message}]if history:13 unmodified linesyield responsetitle = "# Writing Tutor"topics = """### Meet your friendly writing tutor, an AI-driven partner to turn to when you need help writing an essay.Feel free to ask me about the topics below:- How to organize your essay- What a thesis is and how to write it- How to craft an introduction paragraph- What your body paragraphs should accomplish- Important things to include in your conclusion- Examples of topic sentences"""with gr.BLocks(theme='JohnSmith9982/small_and_pretty') as chatbot:# gr.Markdown(welcome_message)with gr.Row():with gr.Column():gr.Markdown(title)gr.Markdown(topics)with gr.Row():with gr.Column():gr.ChatInterface(fn=respond,type="messages")question = gr.Textbox(label="Your question", placeholder="What do you want to ask about?")answer = gr.Textbox(label="Writing Tutor Response", placeholder="Writing Tutor will respond here...", interactive=False, lines=10)submit_button = gr.Button("Submit")submit_button.click(fn=query_model, inputs=question, outputs=answer)# chatbot = gr.ChatInterface(respond, type="messages")chatbot.launch()
app.py+33-7