Spaces:
Runtime error
Runtime error
Commit
·
41bb40c
1
Parent(s):
efd38a2
feat: Update app
Browse files
app.py
CHANGED
|
@@ -11,6 +11,15 @@ classifier = pipeline(
|
|
| 11 |
)
|
| 12 |
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
def classification(task: str, doc: str) -> str:
|
| 15 |
"""Classify text into categories.
|
| 16 |
|
|
@@ -93,40 +102,55 @@ def classification(task: str, doc: str) -> str:
|
|
| 93 |
"sport",
|
| 94 |
]
|
| 95 |
|
| 96 |
-
#
|
| 97 |
-
|
| 98 |
-
elif task == "Offensive text detection":
|
| 99 |
if language == "sv":
|
| 100 |
-
hypothesis_template = "
|
| 101 |
-
candidate_labels =
|
|
|
|
|
|
|
|
|
|
| 102 |
elif language == "no":
|
| 103 |
-
hypothesis_template = "
|
| 104 |
-
candidate_labels =
|
|
|
|
|
|
|
|
|
|
| 105 |
else:
|
| 106 |
-
hypothesis_template = "
|
| 107 |
-
candidate_labels =
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
# Else the task is not supported, so raise an error
|
| 110 |
else:
|
| 111 |
raise ValueError(f"Task {task} not supported.")
|
| 112 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
# Run the classifier on the text
|
| 114 |
result = classifier(
|
| 115 |
-
doc,
|
|
|
|
|
|
|
| 116 |
)
|
| 117 |
|
| 118 |
print(result)
|
| 119 |
|
| 120 |
# Return the predicted label
|
| 121 |
return (
|
| 122 |
-
f"{result['labels'][0]
|
| 123 |
f"({confidence_str}: {result['scores'][0]:.0%})"
|
| 124 |
)
|
| 125 |
|
| 126 |
# Create a dropdown menu for the task
|
| 127 |
dropdown = gr.inputs.Dropdown(
|
| 128 |
label="Task",
|
| 129 |
-
choices=["Sentiment classification", "News topic classification", "
|
| 130 |
default="Sentiment classification",
|
| 131 |
)
|
| 132 |
|
|
@@ -136,7 +160,7 @@ interface = gr.Interface(
|
|
| 136 |
inputs=[dropdown, gr.inputs.Textbox(label="Text")],
|
| 137 |
outputs=gr.outputs.Label(type="text"),
|
| 138 |
title="Scandinavian zero-shot text classification",
|
| 139 |
-
description=
|
| 140 |
)
|
| 141 |
|
| 142 |
# Run the app
|
|
|
|
| 11 |
)
|
| 12 |
|
| 13 |
|
| 14 |
+
# Set the description for the interface
|
| 15 |
+
DESCRIPTION = """Classify text in Danish, Swedish or Norwegian into categories, without
|
| 16 |
+
any training data!
|
| 17 |
+
|
| 18 |
+
Note that the models will most likely *not* work as well as a finetuned model on your
|
| 19 |
+
specific data, but they can be used as a starting point for your own classification
|
| 20 |
+
task ✨"""
|
| 21 |
+
|
| 22 |
+
|
| 23 |
def classification(task: str, doc: str) -> str:
|
| 24 |
"""Classify text into categories.
|
| 25 |
|
|
|
|
| 102 |
"sport",
|
| 103 |
]
|
| 104 |
|
| 105 |
+
# Else if the task is spam detection, classify the text into spam or not spam
|
| 106 |
+
elif task == "Spam detection":
|
|
|
|
| 107 |
if language == "sv":
|
| 108 |
+
hypothesis_template = "Det här e-postmeddelandet ser {}"
|
| 109 |
+
candidate_labels = {
|
| 110 |
+
"ut som ett skräppostmeddelande": "Spam",
|
| 111 |
+
"inte ut som ett skräppostmeddelande": "Inte spam",
|
| 112 |
+
}
|
| 113 |
elif language == "no":
|
| 114 |
+
hypothesis_template = "Denne e-posten ser {}"
|
| 115 |
+
candidate_labels = {
|
| 116 |
+
"ut som en spam-e-post": "Spam",
|
| 117 |
+
"ikke ut som en spam-e-post": "Ikke spam",
|
| 118 |
+
}
|
| 119 |
else:
|
| 120 |
+
hypothesis_template = "Denne e-mail ligner {}"
|
| 121 |
+
candidate_labels = {
|
| 122 |
+
"en spam e-mail": "Spam",
|
| 123 |
+
"ikke en spam e-mail": "Ikke spam",
|
| 124 |
+
}
|
| 125 |
|
| 126 |
# Else the task is not supported, so raise an error
|
| 127 |
else:
|
| 128 |
raise ValueError(f"Task {task} not supported.")
|
| 129 |
|
| 130 |
+
# If `candidate_labels` is a list then convert it to a dictionary, where the keys
|
| 131 |
+
# are the entries in the list and the values are the keys capitalized
|
| 132 |
+
if isinstance(candidate_labels, list):
|
| 133 |
+
candidate_labels = {label: label.capitalize() for label in candidate_labels}
|
| 134 |
+
|
| 135 |
# Run the classifier on the text
|
| 136 |
result = classifier(
|
| 137 |
+
doc,
|
| 138 |
+
candidate_labels=list(candidate_labels.keys()),
|
| 139 |
+
hypothesis_template=hypothesis_template,
|
| 140 |
)
|
| 141 |
|
| 142 |
print(result)
|
| 143 |
|
| 144 |
# Return the predicted label
|
| 145 |
return (
|
| 146 |
+
f"{candidate_labels[result['labels'][0]]}\n"
|
| 147 |
f"({confidence_str}: {result['scores'][0]:.0%})"
|
| 148 |
)
|
| 149 |
|
| 150 |
# Create a dropdown menu for the task
|
| 151 |
dropdown = gr.inputs.Dropdown(
|
| 152 |
label="Task",
|
| 153 |
+
choices=["Sentiment classification", "News topic classification", "Spam detection"],
|
| 154 |
default="Sentiment classification",
|
| 155 |
)
|
| 156 |
|
|
|
|
| 160 |
inputs=[dropdown, gr.inputs.Textbox(label="Text")],
|
| 161 |
outputs=gr.outputs.Label(type="text"),
|
| 162 |
title="Scandinavian zero-shot text classification",
|
| 163 |
+
description=DESCRIPTION,
|
| 164 |
)
|
| 165 |
|
| 166 |
# Run the app
|