Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,29 +6,31 @@ from sentence_transformers import SentenceTransformer
|
|
| 6 |
# Model
|
| 7 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 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(
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
| 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 "
|
| 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()
|
|
@@ -43,21 +45,16 @@ def match_suppliers(event_description, top_k=5):
|
|
| 43 |
|
| 44 |
return pd.DataFrame(results)
|
| 45 |
|
|
|
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
-
gr.Markdown("## Supplier Matching App
|
| 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("
|
| 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("
|
| 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")
|
|
|
|
| 6 |
# Model
|
| 7 |
model = SentenceTransformer("all-MiniLM-L6-v2")
|
| 8 |
|
| 9 |
+
# Load and process supplier data on startup
|
| 10 |
+
def load_supplier_data():
|
|
|
|
|
|
|
|
|
|
| 11 |
global supplier_df, supplier_embeddings
|
| 12 |
|
| 13 |
+
supplier_df = pd.read_excel("SupplierList.xlsx")
|
| 14 |
supplier_df["Capability"] = supplier_df["Capability"].fillna("")
|
| 15 |
texts = supplier_df["Capability"].tolist()
|
| 16 |
|
| 17 |
+
# Compute and normalize embeddings
|
| 18 |
supplier_embeddings = model.encode(texts, convert_to_tensor=True, normalize_embeddings=True)
|
| 19 |
|
| 20 |
+
# Initial data load
|
| 21 |
+
supplier_df = None
|
| 22 |
+
supplier_embeddings = None
|
| 23 |
+
load_supplier_data()
|
| 24 |
|
| 25 |
+
# View loaded data
|
| 26 |
def view_supplier_data():
|
| 27 |
return supplier_df if supplier_df is not None else pd.DataFrame()
|
| 28 |
|
| 29 |
+
# Match function
|
| 30 |
def match_suppliers(event_description, top_k=5):
|
| 31 |
if supplier_embeddings is None:
|
| 32 |
+
return "Supplier data not loaded."
|
| 33 |
+
|
| 34 |
event_embedding = model.encode(event_description, convert_to_tensor=True, normalize_embeddings=True).unsqueeze(0)
|
| 35 |
scores = torch.nn.functional.cosine_similarity(event_embedding, supplier_embeddings)
|
| 36 |
top_indices = torch.topk(scores, k=top_k).indices.tolist()
|
|
|
|
| 45 |
|
| 46 |
return pd.DataFrame(results)
|
| 47 |
|
| 48 |
+
# Gradio Interface
|
| 49 |
with gr.Blocks() as demo:
|
| 50 |
+
gr.Markdown("## Supplier Matching App")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
with gr.Tab("1. View Supplier Data"):
|
| 53 |
+
view_button = gr.Button("Show Supplier Data")
|
| 54 |
data_output = gr.Dataframe()
|
| 55 |
view_button.click(view_supplier_data, outputs=data_output)
|
| 56 |
|
| 57 |
+
with gr.Tab("2. Match Use Case to Suppliers"):
|
| 58 |
input_text = gr.Textbox(lines=3, label="Describe Your Event/Use Case")
|
| 59 |
top_k_input = gr.Slider(minimum=1, maximum=10, value=5, label="Top K Matches")
|
| 60 |
match_button = gr.Button("Match Suppliers")
|