Spaces:
Sleeping
Sleeping
Update app.py
Browse files9 unmodified lines10111213141516161718192021222323242526271 unmodified line293031313233343521 unmodified lines57585959606162632 unmodified lines666768686970717272737475769 unmodified lines868788899091929394959697919293989910010110210310410510610 unmodified lines1171181191101201211221232 unmodified lines1261271281191201291301311321331341351361371381391401281291411421431441451461471481491501511521371381531541551561571581591601611621441631641651669 unmodified linesDEFAULT_INSTRUCTION = "Given a query, retrieve documents that answer the query."MAX_DOCS = 20MAX_DOC_CHARS = 4000DOC_SEPARATOR = "---"
def parse_documents(text: str) -> List[str]: docs = [doc.strip() for doc in text.split("\n\n") if doc.strip()] docs = [doc.strip() for doc in text.split(DOC_SEPARATOR) if doc.strip()] docs = docs[:MAX_DOCS] docs = [doc[:MAX_DOC_CHARS] for doc in docs] return docs
@spaces.GPUdef rerank(query: str, documents: str, instruction: str):def rerank(query: str, documents: str, instruction: str, chunk_size: int): reranker = KaLMReranker( MODEL_ID, device=None,1 unmodified line batch_size=4, query_max_length=512, max_length=1024, chunk_size=4, chunk_size=chunk_size, )
query = query.strip()21 unmodified lines
summary = ( f"Reranked {len(docs)} documents with " f"`{MODEL_ID}`. Higher score means more relevant." f"`{MODEL_ID}` (chunk_size={chunk_size}). Higher score means more relevant." ) return table, summary except Exception as error:2 unmodified lines
with gr.Blocks(title="KaLM-Reranker-V1 Demo") as demo: gr.Markdown( """ f"""# KaLM-Reranker-V1 Demo**KaLM-Reranker-V1** is a fast but not late-interaction reranker for compressed document reranking.Input a query and several candidate documents. The demo returns relevance scores and reranked results.**Document format:** separate candidate documents with one blank line.**Document format:** separate candidate documents with `{DOC_SEPARATOR}` (three dashes on its own line). """ )
9 unmodified lines value=DEFAULT_INSTRUCTION, lines=2, ) chunk_size = gr.Dropdown( label="Chunk Size", choices=[2, 4, 8, 16], value=4, interactive=True, ) documents = gr.Textbox( label="Candidate Documents", value=( "The capital of China is Beijing.\n\n" "Gravity attracts bodies toward one another.\n\n" "Shanghai is a major city in China.\n\n" "The capital of China is Beijing.\n" "---\n" "Gravity attracts bodies toward one another.\n" "---\n" "Shanghai is a major city in China.\n" "---\n" "Paris is the capital of France." ), lines=14,10 unmodified lines
submit.click( fn=rerank, inputs=[query, documents, instruction], inputs=[query, documents, instruction, chunk_size], outputs=[output_table, output_summary], )
2 unmodified lines [ "What is the capital of China?", ( "The capital of China is Beijing.\n\n" "Gravity attracts bodies toward one another.\n\n" "The capital of China is Beijing.\n" "---\n" "Gravity attracts bodies toward one another.\n" "---\n" "Paris is the capital of France." ), DEFAULT_INSTRUCTION, 4, ], [ "Which model is suitable for efficient reranking?", ( "KaLM-Reranker-V1-Nano is designed for efficient reranking.\n\n" "Large language models are often expensive for reranking.\n\n" "KaLM-Reranker-V1-Nano is designed for efficient reranking.\n" "---\n" "Large language models are often expensive for reranking.\n" "---\n" "Image classifiers are used for visual recognition." ), DEFAULT_INSTRUCTION, 4, ], [ "What is KaLM-Reranker-V1 designed for?", ( "KaLM-Reranker-V1 is a reranker for compressed document reranking.\n\n" "KaLM-Embedding is a general-purpose embedding model.\n\n" "KaLM-Reranker-V1 is a reranker for compressed document reranking.\n" "---\n" "KaLM-Embedding is a general-purpose embedding model.\n" "---\n" "Weather forecasting predicts future weather conditions." ), DEFAULT_INSTRUCTION, 4, ], ], inputs=[query, documents, instruction], inputs=[query, documents, instruction, chunk_size], )
gr.Markdown(app.py+36-17