Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,47 +3,66 @@ import torch
|
|
| 3 |
import pandas as pd
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
scores = torch.nn.functional.cosine_similarity(event_embedding, supplier_embeddings)
|
| 24 |
-
|
| 25 |
-
# Find top K matches
|
| 26 |
top_indices = torch.topk(scores, k=top_k).indices.tolist()
|
| 27 |
-
|
| 28 |
-
# Format results
|
| 29 |
results = []
|
| 30 |
for idx in top_indices:
|
| 31 |
results.append({
|
| 32 |
-
"Supplier Name":
|
| 33 |
"Match Score": round(scores[idx].item(), 4),
|
| 34 |
-
"Capabilities":
|
| 35 |
})
|
| 36 |
-
|
| 37 |
return pd.DataFrame(results)
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
if __name__ == "__main__":
|
| 49 |
demo.launch()
|
|
|
|
| 3 |
import pandas as pd
|
| 4 |
from sentence_transformers import SentenceTransformer
|
| 5 |
|
| 6 |
+
# Model
|
| 7 |
+
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 8 |
|
| 9 |
+
# Global state for reusability
|
| 10 |
+
supplier_df = None
|
| 11 |
+
supplier_embeddings = None
|
| 12 |
|
| 13 |
+
def load_and_process_supplier_data(file):
|
| 14 |
+
global supplier_df, supplier_embeddings
|
| 15 |
|
| 16 |
+
supplier_df = pd.read_excel(file)
|
| 17 |
+
supplier_df["Capability"] = supplier_df["Capability"].fillna("")
|
| 18 |
+
texts = supplier_df["Capability"].tolist()
|
| 19 |
|
| 20 |
+
# Compute embeddings
|
| 21 |
+
supplier_embeddings = model.encode(texts, convert_to_tensor=True, normalize_embeddings=True)
|
| 22 |
+
|
| 23 |
+
return f"{len(supplier_df)} suppliers loaded and processed."
|
| 24 |
+
|
| 25 |
+
def view_supplier_data():
|
| 26 |
+
return supplier_df if supplier_df is not None else pd.DataFrame()
|
| 27 |
+
|
| 28 |
+
def match_suppliers(event_description, top_k=5):
|
| 29 |
+
if supplier_embeddings is None:
|
| 30 |
+
return "Please upload and process supplier data first."
|
| 31 |
|
| 32 |
+
event_embedding = model.encode(event_description, convert_to_tensor=True, normalize_embeddings=True).unsqueeze(0)
|
| 33 |
scores = torch.nn.functional.cosine_similarity(event_embedding, supplier_embeddings)
|
|
|
|
|
|
|
| 34 |
top_indices = torch.topk(scores, k=top_k).indices.tolist()
|
| 35 |
+
|
|
|
|
| 36 |
results = []
|
| 37 |
for idx in top_indices:
|
| 38 |
results.append({
|
| 39 |
+
"Supplier Name": supplier_df.loc[idx, "Supplier Name"],
|
| 40 |
"Match Score": round(scores[idx].item(), 4),
|
| 41 |
+
"Capabilities": supplier_df.loc[idx, "Capability"]
|
| 42 |
})
|
| 43 |
+
|
| 44 |
return pd.DataFrame(results)
|
| 45 |
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("## Supplier Matching App with Full Pipeline")
|
| 48 |
+
|
| 49 |
+
with gr.Tab("1. Upload Supplier Data"):
|
| 50 |
+
file_input = gr.File(label="Upload Excel file", file_types=[".xlsx"])
|
| 51 |
+
load_button = gr.Button("Load and Process")
|
| 52 |
+
load_output = gr.Textbox(label="Status")
|
| 53 |
+
load_button.click(load_and_process_supplier_data, inputs=file_input, outputs=load_output)
|
| 54 |
+
|
| 55 |
+
with gr.Tab("2. View Supplier Data"):
|
| 56 |
+
view_button = gr.Button("Show Data")
|
| 57 |
+
data_output = gr.Dataframe()
|
| 58 |
+
view_button.click(view_supplier_data, outputs=data_output)
|
| 59 |
+
|
| 60 |
+
with gr.Tab("3. Match Use Case to Suppliers"):
|
| 61 |
+
input_text = gr.Textbox(lines=3, label="Describe Your Event/Use Case")
|
| 62 |
+
top_k_input = gr.Slider(minimum=1, maximum=10, value=5, label="Top K Matches")
|
| 63 |
+
match_button = gr.Button("Match Suppliers")
|
| 64 |
+
match_output = gr.Dataframe()
|
| 65 |
+
match_button.click(match_suppliers, inputs=[input_text, top_k_input], outputs=match_output)
|
| 66 |
|
| 67 |
if __name__ == "__main__":
|
| 68 |
demo.launch()
|