ferdaous commited on
Commit
2a52f59
·
verified ·
1 Parent(s): 793a7c9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -27
app.py CHANGED
@@ -3,47 +3,66 @@ import torch
3
  import pandas as pd
4
  from sentence_transformers import SentenceTransformer
5
 
6
- # embedding model
7
- model = SentenceTransformer("all-MiniLM-L6-v2")
8
 
9
- #supplier data
10
- df = pd.read_excel("SupplierList.xlsx")
 
11
 
12
- supplier_names = df["Supplier Name"].tolist()
13
- supplier_texts = df["Capability"].fillna("").tolist()
14
 
15
- # Precompute supplier embeddings
16
- supplier_embeddings = model.encode(supplier_texts, convert_to_tensor=True, normalize_embeddings=True)
 
17
 
18
- def get_top_supplier(event_description, top_k=5):
19
- # Get embedding for the input event
20
- event_embedding = model.encode(event_description, convert_to_tensor=True, normalize_embeddings=True).unsqueeze(0)
 
 
 
 
 
 
 
 
21
 
22
- # Compute cosine similarity
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": supplier_names[idx],
33
  "Match Score": round(scores[idx].item(), 4),
34
- "Capabilities": supplier_texts[idx]
35
  })
36
-
37
  return pd.DataFrame(results)
38
 
39
- # Gradio UI
40
- demo = gr.Interface(
41
- fn=get_top_supplier,
42
- inputs=gr.Textbox(lines=4, placeholder="Describe your use case here...", label="Use Case Description"),
43
- outputs=gr.Dataframe(label="Top Matching Suppliers"),
44
- title="Supplier Matching App",
45
- description="Enter a use case or event description to find the most relevant suppliers based on their capabilities."
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()