Spaces:
Sleeping
Sleeping
| from strive.reranker import Reranker, EmbeddingType, deduplicate_results | |
| import gradio as gr | |
| import time | |
| def rerank_and_deduplicate(query, corpus_text): | |
| corpus = [line.strip() for line in corpus_text.split("\n") if line.strip()] | |
| start = time.perf_counter() | |
| textual_reranker = Reranker(embedding_type=EmbeddingType.textual) | |
| semantic_reranker = Reranker(embedding_type=EmbeddingType.semantic) | |
| textual_results = textual_reranker.rerank_documents(query, corpus, top_k=len(corpus)) | |
| semantic_results = semantic_reranker.rerank_documents(query, corpus, top_k=len(corpus)) | |
| merged_results = textual_results + semantic_results | |
| deduplicated_results = deduplicate_results(merged_results, top_k=50) | |
| elapsed_ms = (time.perf_counter() - start) * 1000 | |
| header = f"Reranked {len(corpus)} documents in {elapsed_ms:.1f} ms on CPU\n\n" | |
| return header + "\n".join([f"{text} (Score: {score:.4f})" for text, score in deduplicated_results]) | |
| laptop_corpus = "\n".join([ | |
| "The best budget laptops for programming in 2026 balance a fast CPU, at least 16GB of RAM, and a comfortable keyboard.", | |
| "A good programming laptop needs enough memory to run an IDE, a browser with dozens of tabs, and a local dev server at the same time.", | |
| "Affordable laptops for coding usually trade a discrete GPU for a better CPU and more RAM within the same budget.", | |
| "The best budget laptops for programming in 2026 balance a fast CPU, at least 16GB of RAM, and a comfortable keyboard.", | |
| "The latest flagship smartphone features a titanium frame and a much larger camera sensor than last year's model.", | |
| "For gaming, a discrete GPU with at least 8GB of VRAM matters far more than raw CPU clock speed.", | |
| "A mechanical keyboard with hot-swappable switches is popular among developers who type for long hours.", | |
| "Budget desktop towers can be upgraded piece by piece, unlike laptops which are mostly fixed at purchase time.", | |
| "Look for a laptop with a matte 1080p or higher display, since glossy screens cause glare during long coding sessions.", | |
| "The new wireless earbuds promise 30 hours of battery life with the charging case included.", | |
| "Cheap laptops with only 8GB of RAM will struggle once you open a Docker container alongside your editor.", | |
| "A 14-inch or 15-inch laptop is usually the sweet spot between portability and screen real estate for coding.", | |
| ]) | |
| health_corpus = "\n".join([ | |
| "Common symptoms of vitamin D deficiency include fatigue, bone pain, muscle weakness, and mood changes.", | |
| "People with low vitamin D levels often report persistent tiredness even after a full night's sleep.", | |
| "Vitamin D deficiency can cause bone and muscle pain, and in severe cases contributes to osteoporosis.", | |
| "Common symptoms of vitamin D deficiency include fatigue, bone pain, muscle weakness, and mood changes.", | |
| "Iron deficiency anemia typically presents with pale skin, shortness of breath, and brittle nails.", | |
| "Sunlight exposure on bare skin triggers natural vitamin D synthesis, though sunscreen reduces this effect.", | |
| "A balanced diet rich in leafy greens supports overall micronutrient intake, including iron and folate.", | |
| "Low vitamin D has also been associated with a weakened immune response and slower wound healing.", | |
| "Regular cardiovascular exercise improves resting heart rate and long-term cardiovascular health.", | |
| "Vitamin D supplements are commonly recommended during winter months in regions with limited sunlight.", | |
| "Chronic fatigue can also stem from thyroid disorders, sleep apnea, or unmanaged stress.", | |
| "Blood tests measuring 25-hydroxyvitamin D are the standard way to confirm a deficiency diagnosis.", | |
| ]) | |
| devops_corpus = "\n".join([ | |
| "Reducing cloud infrastructure costs often starts with rightsizing over-provisioned virtual machines.", | |
| "Switching non-critical workloads to spot or preemptible instances can cut compute costs by over 60%.", | |
| "Unused storage volumes and orphaned snapshots quietly accumulate cost if nobody audits them regularly.", | |
| "Reducing cloud infrastructure costs often starts with rightsizing over-provisioned virtual machines.", | |
| "A well-designed CI/CD pipeline reduces the time between a commit and a production deployment.", | |
| "Reserved instances offer significant discounts in exchange for a one- or three-year usage commitment.", | |
| "Container orchestration platforms like Kubernetes can bin-pack workloads to improve node utilization.", | |
| "Feature flags let teams ship code to production without immediately exposing it to all users.", | |
| "Setting up autoscaling policies prevents both over-provisioning during quiet periods and outages during spikes.", | |
| "A service mesh adds observability and traffic control between microservices at the cost of some latency.", | |
| "Cost allocation tags make it possible to attribute cloud spend back to individual teams or products.", | |
| "Moving infrequently accessed data to cold storage tiers can reduce storage costs substantially.", | |
| ]) | |
| recipe_corpus_pt = "\n".join([ | |
| "Uma boa receita de bolo de chocolate leva farinha, cacau em pó, ovos, açúcar e fermento em pó.", | |
| "Para um bolo de chocolate úmido, é importante não bater a massa demais depois de adicionar a farinha.", | |
| "O segredo de um bolo de chocolate fofo está no ponto certo do fermento e no tempo de forno.", | |
| "Uma boa receita de bolo de chocolate leva farinha, cacau em pó, ovos, açúcar e fermento em pó.", | |
| "A receita de brigadeiro gourmet costuma usar chocolate meio amargo em vez do achocolatado tradicional.", | |
| "Um bom risoto de camarão exige caldo quente sendo adicionado aos poucos, sem parar de mexer.", | |
| "Cobertura de ganache é feita derretendo chocolate com creme de leite quente em partes iguais.", | |
| "A farofa crocante combina bem com carnes assadas e é feita com farinha de mandioca torrada na manteiga.", | |
| "Para testar se o bolo está pronto, espete um palito no centro; se sair limpo, está assado.", | |
| "Pão de queijo mineiro tradicional é feito com polvilho azedo e queijo meia cura ralado.", | |
| "Bater as claras em neve antes de incorporar à massa deixa o bolo de chocolate ainda mais leve.", | |
| "Salada caprese leva apenas tomate, mussarela de búfala, manjericão fresco e azeite de oliva.", | |
| ]) | |
| gradio_examples = [ | |
| ["best budget laptop for programming", laptop_corpus], | |
| ["symptoms of vitamin D deficiency", health_corpus], | |
| ["how to reduce cloud infrastructure costs", devops_corpus], | |
| ["receita de bolo de chocolate", recipe_corpus_pt], | |
| ] | |
| app = gr.Interface( | |
| fn=rerank_and_deduplicate, | |
| inputs=[ | |
| gr.Textbox(label="Query", placeholder="Enter your query here"), | |
| gr.Textbox(label="Corpus", placeholder="Enter one sentence per line", lines=10) | |
| ], | |
| outputs=gr.Textbox(label="Top Ranked Results"), | |
| title="STRIVE: Semantic Tokenized Ranking via Vectorization & Embeddings", | |
| description="Enter a query and multiple sentences to test the reranking algorithm.", | |
| examples=gradio_examples, | |
| ) | |
| app.launch() | |