Spaces:
Runtime error
Runtime error
Update app.py
Browse files43 unmodified lines444546474849505152535455565758596061626364656667686943 unmodified lines# load an embedding modelmodel = SentenceTransformer('all-MiniLM-L6-v2')chunk_embeddings = model.encode(cleaned_chunks, convert_to_tensor=True)def 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("microsoft/phi-4")def respond(message, history):info = pull_relevant_info(message, top_k=3)system_message = (f"You are a helpful and kind teacher named Ms. Honey. You respond clearly in no more than three complete sentences. If a user asks you to write something for them, you refuse and remind them they are capable of writing the piece themselves. Use the following information to help answer the user's question:\n\n{info}\n\n")messages = [{"role": "system", "content": system_message}]43 unmodified lines444546474849505152535455565758596061626364656667686970717273747576777843 unmodified lines# load an embedding modelmodel = SentenceTransformer('all-MiniLM-L6-v2')def create_embeddings(text_chunks):chunk_embeddings = model.encode(text_chunks, convert_to_tensor=True)return chunk_embeddingsessay_embeddings = create_embeddings(essay_chunks)def pull_relevant_info(message, chunk_embeddings, text_chunks):query_embedding = model.encode(message, 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=3).indicestop_chunks = []for i in top_indices:relevant_info = text_chunks[i]top_chunks.append(relevant_info)return top_chunksclient = InferenceClient("microsoft/phi-4")def respond(message, history):info = pull_relevant_info(message, essay_embeddings, essay_chunks)system_message = (f"You are a helpful and kind teacher named Ms. Honey. You respond clearly in no more than three complete sentences. If a user asks you to write something for them, you refuse and remind them they are capable of writing the piece themselves. Use the following information to help answer the user's question:\n\n{info}\n\n")messages = [{"role": "system", "content": system_message}]
app.py+16-7