grofte commited on
Commit
f3ee873
·
1 Parent(s): 27653e4

Switch to sbert and LaBSE

Browse files
Files changed (2) hide show
  1. app.py +45 -0
  2. requirements.txt +2 -0
app.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import scipy
2
+ import gradio as gr
3
+ from sentence_transformers import SentenceTransformer
4
+
5
+ title = "Zero-Shot Text Classification with Hugging Face"
6
+ description = """LaBSE - Language-agnostic BERT Sentence Embedding via sbert.net
7
+ Forked from https://huggingface.co/spaces/eldoraboo/zero-shot"""
8
+
9
+ model = SentenceTransformer('sentence-transformers/LaBSE')
10
+
11
+ #define a function to process your input and output
12
+ def zero_shot(doc, candidates):
13
+ given_labels = candidates.split(", ")
14
+ texts = [doc] + given_labels
15
+ embeddings = model.encode(texts)
16
+ query_embedding = embeddings[0]
17
+ candidate_embeddings = embeddings[1:]
18
+ distances = scipy.spatial.distance.cdist([query_embedding], candidate_embeddings, "cosine")[0]
19
+ results = zip(given_labels, distances)
20
+ results = sorted(results, key=lambda x: x[1])
21
+ return dict(results)
22
+
23
+ #create input and output objects
24
+ #input object1
25
+ input1 = gr.Textbox(label="Text")
26
+ #input object 2
27
+ input2 = gr.Textbox(label="Labels")
28
+ #output object
29
+ output = gr.Label(label="Output")
30
+ #example object
31
+ examples = [
32
+ ["TDC A/S provides communications and entertainment solutions in Denmark. It operates through Nuuday and TDC NET segments. The company designs, builds, and operates broadband and mobile networks; and provides technical support to customers and networks. It offers services, such as landline voice, TV and streaming, broadband, Internet and network, mobility, and other services. The company provides its products and services under the YouSee, Hiper, Telmore, Blockbuster, TDC Business, TDC Erhverv, Fullrate, NetDesign, and Relatel brands. It serves consumer and business customers. The company was founded in 1882 and is based in Copenhagen, Denmark. TDC A/S is a subsidiary of DK Telekommunikation ApS.", "Diversified Telecommunication Services, Wireless Telecommunication Services, Media, Entertainment, Interactive Media and Services"],
33
+ ["Giddy Inc., doing business as Boxed Wholesale, offers online wholesale and retailing services. The company provides cleaning and laundry, kitchen, paper, skin care, hair care, and grocery products. Additionally, it offers diapers and organic products. Giddy Inc. was founded in 2013 and is based in Edison, New Jersey.", "Food and Staples Retailing, Beverages, Food Products, Household Products, Personal Products, Tobacco"],
34
+ ["United Iron And Steel Manufacturing Company (P.L.C.) produces and sells iron and steel products in Jordan. It is also involved in trading scrap iron. The company was incorporated in 1992 and is headquartered in Amman, Jordan. United Iron And Steel Manufacturing Company (P.L.C.) is a subsidiary of Manaseer Group Corporation.", "Chemicals, Construction Materials, Containers and Packaging, Metals and Mining, Paper and Forest Products"]
35
+ ]
36
+ #create interface
37
+ gui = gr.Interface(title=title,
38
+ description=description,
39
+ fn=zero_shot,
40
+ inputs=[input1, input2],
41
+ outputs=[output],
42
+ examples=examples)
43
+
44
+ #display the interface
45
+ gui.launch()
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ sentence-transformers
2
+ scipy