Spaces:
Running on Zero
Running on Zero
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import Any
|
| 2 |
+
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import spaces
|
| 5 |
+
from sentence_transformers import CrossEncoder
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
MODEL_ID = "ramitha2002/genieai-product-reranker"
|
| 9 |
+
|
| 10 |
+
# ZeroGPU requires CUDA placement at module level.
|
| 11 |
+
model = CrossEncoder(
|
| 12 |
+
MODEL_ID,
|
| 13 |
+
device="cuda",
|
| 14 |
+
max_length=384,
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def format_value(value: Any) -> str:
|
| 19 |
+
if isinstance(value, list):
|
| 20 |
+
return ", ".join(str(item) for item in value)
|
| 21 |
+
return str(value)
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
def build_product_text(product: dict[str, Any]) -> str:
|
| 25 |
+
fields = [
|
| 26 |
+
("Title", product.get("title") or product.get("name")),
|
| 27 |
+
("Description", product.get("description") or product.get("summary")),
|
| 28 |
+
("Features", product.get("features")),
|
| 29 |
+
("Brand", product.get("brand")),
|
| 30 |
+
("Color", product.get("color")),
|
| 31 |
+
("Category", product.get("category")),
|
| 32 |
+
]
|
| 33 |
+
|
| 34 |
+
return "\n".join(
|
| 35 |
+
f"{label}: {format_value(value)}"
|
| 36 |
+
for label, value in fields
|
| 37 |
+
if value is not None and value != ""
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@spaces.GPU(duration=30)
|
| 42 |
+
def rerank(
|
| 43 |
+
query: str,
|
| 44 |
+
products: list[dict[str, Any]],
|
| 45 |
+
top_n: int,
|
| 46 |
+
) -> dict[str, Any]:
|
| 47 |
+
query = query.strip()
|
| 48 |
+
|
| 49 |
+
if not query:
|
| 50 |
+
raise gr.Error("Query is required.")
|
| 51 |
+
|
| 52 |
+
if not isinstance(products, list) or not products:
|
| 53 |
+
raise gr.Error("Products must be a non-empty JSON array.")
|
| 54 |
+
|
| 55 |
+
if len(products) > 30:
|
| 56 |
+
raise gr.Error("Maximum 30 products per request.")
|
| 57 |
+
|
| 58 |
+
pairs = [
|
| 59 |
+
(query, build_product_text(product))
|
| 60 |
+
for product in products
|
| 61 |
+
]
|
| 62 |
+
|
| 63 |
+
scores = model.predict(
|
| 64 |
+
pairs,
|
| 65 |
+
batch_size=min(16, len(pairs)),
|
| 66 |
+
show_progress_bar=False,
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
ranked = sorted(
|
| 70 |
+
[
|
| 71 |
+
{
|
| 72 |
+
**product,
|
| 73 |
+
"rerankerScore": float(score),
|
| 74 |
+
}
|
| 75 |
+
for product, score in zip(products, scores)
|
| 76 |
+
],
|
| 77 |
+
key=lambda product: product["rerankerScore"],
|
| 78 |
+
reverse=True,
|
| 79 |
+
)
|
| 80 |
+
|
| 81 |
+
return {
|
| 82 |
+
"results": ranked[:max(1, min(int(top_n), len(ranked)))]
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
sample_products = [
|
| 87 |
+
{
|
| 88 |
+
"id": "flowers-1",
|
| 89 |
+
"name": "Pink Rose Bouquet",
|
| 90 |
+
"description": "Fresh roses arranged for birthdays",
|
| 91 |
+
"brand": "Bloom House",
|
| 92 |
+
"color": "Pink"
|
| 93 |
+
},
|
| 94 |
+
{
|
| 95 |
+
"id": "mouse-1",
|
| 96 |
+
"name": "Wireless Gaming Mouse",
|
| 97 |
+
"description": "RGB computer mouse",
|
| 98 |
+
"brand": "GamePoint",
|
| 99 |
+
"color": "Black"
|
| 100 |
+
}
|
| 101 |
+
]
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
with gr.Blocks(title="GenieAI Product Reranker") as demo:
|
| 105 |
+
gr.Markdown("# GenieAI Product Reranker")
|
| 106 |
+
|
| 107 |
+
query_input = gr.Textbox(
|
| 108 |
+
label="Search query",
|
| 109 |
+
value="birthday flowers for mother",
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
products_input = gr.JSON(
|
| 113 |
+
label="Products",
|
| 114 |
+
value=sample_products,
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
top_n_input = gr.Slider(
|
| 118 |
+
minimum=1,
|
| 119 |
+
maximum=30,
|
| 120 |
+
value=4,
|
| 121 |
+
step=1,
|
| 122 |
+
label="Number of results",
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
rerank_button = gr.Button("Rerank", variant="primary")
|
| 126 |
+
output = gr.JSON(label="Ranked products")
|
| 127 |
+
|
| 128 |
+
rerank_button.click(
|
| 129 |
+
fn=rerank,
|
| 130 |
+
inputs=[query_input, products_input, top_n_input],
|
| 131 |
+
outputs=output,
|
| 132 |
+
api_name="rerank",
|
| 133 |
+
)
|
| 134 |
+
|
| 135 |
+
demo.queue(default_concurrency_limit=2).launch()
|