ferdaous commited on
Commit
a4e0176
·
verified ·
1 Parent(s): be0548e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -0
app.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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_csv("suppliers.csv")
11
+
12
+ supplier_names = df["Supplier Name"].tolist()
13
+ supplier_texts = df["Capabilities"].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()