Spaces:
Runtime error
Runtime error
Multilingual example and description
Browse files
app.py
CHANGED
@@ -3,8 +3,19 @@ import gradio as gr
|
|
3 |
from sentence_transformers import SentenceTransformer
|
4 |
|
5 |
title = "Zero-Shot Text Classification with Hugging Face"
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
8 |
|
9 |
model = SentenceTransformer('sentence-transformers/LaBSE')
|
10 |
|
@@ -17,7 +28,7 @@ def zero_shot(doc, candidates):
|
|
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
|
@@ -29,6 +40,7 @@ input2 = gr.Textbox(label="Labels")
|
|
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"]
|
|
|
3 |
from sentence_transformers import SentenceTransformer
|
4 |
|
5 |
title = "Zero-Shot Text Classification with Hugging Face"
|
6 |
+
# Gradio interprets the description as markdown or html
|
7 |
+
description = """LaBSE - Language-agnostic BERT Sentence Embedding via sbert.net
|
8 |
+
|
9 |
+
Forked from https://huggingface.co/spaces/eldoraboo/zero-shot
|
10 |
+
|
11 |
+
LaBSE is trained on data from 109 languages but can also provide embeddings for unseen languages.
|
12 |
+
In the example below, we use LaBSE to classify a text into one of the given labels.
|
13 |
+
LaBSE correctly recognizes that "poisson" means fish and that the fish label is the most likely label.
|
14 |
+
|
15 |
+
The similarity between the text and the labels is measured using cosine similarity.
|
16 |
+
This means that smaller values indicate a higher similarity.
|
17 |
+
I'll have to find out how to change that at some point.
|
18 |
+
"""
|
19 |
|
20 |
model = SentenceTransformer('sentence-transformers/LaBSE')
|
21 |
|
|
|
28 |
candidate_embeddings = embeddings[1:]
|
29 |
distances = scipy.spatial.distance.cdist([query_embedding], candidate_embeddings, "cosine")[0]
|
30 |
results = zip(given_labels, distances)
|
31 |
+
results = sorted(results, key=lambda x: x[1], reverse=True)
|
32 |
return dict(results)
|
33 |
|
34 |
#create input and output objects
|
|
|
40 |
output = gr.Label(label="Output")
|
41 |
#example object
|
42 |
examples = [
|
43 |
+
["Poisson", "Jam, Milk, Fish, Eggs, Poison"],
|
44 |
["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"],
|
45 |
["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"],
|
46 |
["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"]
|