Spaces:
Sleeping
Sleeping
Update app.py
Browse files9 unmodified lines10111213141516171819202122232425261 unmodified line2829303132333421 unmodified lines565758596061622 unmodified lines65666768697071727374759 unmodified lines85868788899091929394959610 unmodified lines1071081091101111121132 unmodified lines1161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461479 unmodified linesDEFAULT_INSTRUCTION = "Given a query, retrieve documents that answer the query."MAX_DOCS = 20MAX_DOC_CHARS = 4000def parse_documents(text: str) -> List[str]:docs = [doc.strip() for doc in text.split("\n\n") 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):reranker = KaLMReranker(MODEL_ID,device=None,1 unmodified linebatch_size=4,query_max_length=512,max_length=1024,chunk_size=4,)query = query.strip()21 unmodified linessummary = (f"Reranked {len(docs)} documents with "f"`{MODEL_ID}`. Higher score means more relevant.")return table, summaryexcept Exception as error:2 unmodified lineswith gr.Blocks(title="KaLM-Reranker-V1 Demo") as demo:gr.Markdown("""# 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.""")9 unmodified linesvalue=DEFAULT_INSTRUCTION,lines=2,)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""Paris is the capital of France."),lines=14,10 unmodified linessubmit.click(fn=rerank,inputs=[query, documents, instruction],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""Paris is the capital of France."),DEFAULT_INSTRUCTION,],["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""Image classifiers are used for visual recognition."),DEFAULT_INSTRUCTION,],["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""Weather forecasting predicts future weather conditions."),DEFAULT_INSTRUCTION,],],inputs=[query, documents, instruction],)gr.Markdown(9 unmodified lines1011121314151617181920212223242526271 unmodified line2930313233343521 unmodified lines575859606162632 unmodified lines66676869707172737475769 unmodified lines868788899091929394959697989910010110210310410510610 unmodified lines1171181191201211221232 unmodified lines1261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651669 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(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, chunk_size: int):reranker = KaLMReranker(MODEL_ID,device=None,1 unmodified linebatch_size=4,query_max_length=512,max_length=1024,chunk_size=chunk_size,)query = query.strip()21 unmodified linessummary = (f"Reranked {len(docs)} documents with "f"`{MODEL_ID}` (chunk_size={chunk_size}). Higher score means more relevant.")return table, summaryexcept Exception as error:2 unmodified lineswith 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 `{DOC_SEPARATOR}` (three dashes on its own line).""")9 unmodified linesvalue=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""Paris is the capital of France."),lines=14,10 unmodified linessubmit.click(fn=rerank,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""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""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""Weather forecasting predicts future weather conditions."),DEFAULT_INSTRUCTION,4,],],inputs=[query, documents, instruction, chunk_size],)gr.Markdown(
app.py+36-17