Spaces:
Runtime error
Runtime error
Updating UI
Browse files24 unmodified lines252627282829303031323233343536373738391 unmodified line414243454445464713 unmodified lines6162636566646566676869707172737475767778798081828384858687888990919293949524 unmodified lines
def pull_relevant_info(query, top_k=3): query_embedding = model.encode(query, convert_to_tensor=True) query_embedding_normalized = query_embedding / query_embedding.norm() query_embedding = query_embedding / query_embedding.norm()
chunk_embeddings_normalized = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True) norm_chunk_embeddings = chunk_embeddings / chunk_embeddings.norm(dim=1, keepdim=True)
similarities = torch.matmul(chunk_embeddings_normalized, query_embedding_normalized) 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_info
client = 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") 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 lines
yield response
chatbot = gr.ChatInterface(respond, type="messages")title = "# 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