Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,63 +0,0 @@
|
|
| 1 |
-
# Necessary imports
|
| 2 |
-
import gradio as gr
|
| 3 |
-
from transformers import pipeline
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
# Load the zero-shot classification model
|
| 7 |
-
classifier = pipeline(
|
| 8 |
-
"zero-shot-classification", model="MoritzLaurer/ModernBERT-large-zeroshot-v2.0"
|
| 9 |
-
)
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
# Function to perform zero-shot classification
|
| 13 |
-
def ZeroShotTextClassification(text_input, candidate_labels):
|
| 14 |
-
"""
|
| 15 |
-
Performs zero-shot classification on the given text input using the provided candidate labels.
|
| 16 |
-
|
| 17 |
-
Args:
|
| 18 |
-
- text_input (str): The input text to classify.
|
| 19 |
-
- candidate_labels (str): A comma-separated string of candidate labels.
|
| 20 |
-
|
| 21 |
-
Returns:
|
| 22 |
-
dict: A dictionary containing the predicted labels as keys and their corresponding scores as values.
|
| 23 |
-
"""
|
| 24 |
-
# Split the candidate labels
|
| 25 |
-
labels = [label.strip(" ") for label in candidate_labels.split(",")]
|
| 26 |
-
|
| 27 |
-
# Perform zero-shot classification
|
| 28 |
-
prediction = classifier(text_input, labels)
|
| 29 |
-
|
| 30 |
-
return {
|
| 31 |
-
prediction["labels"][i]: prediction["scores"][i]
|
| 32 |
-
for i in range(len(prediction["labels"]))
|
| 33 |
-
}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Examples to display in the interface
|
| 37 |
-
examples = [
|
| 38 |
-
["I love to play the guitar", "music, artist, food, travel"],
|
| 39 |
-
["I am a software engineer at Google", "technology, engineering, art, science"],
|
| 40 |
-
["I am a professional basketball player", "sports, athlete, chef, politics"],
|
| 41 |
-
]
|
| 42 |
-
|
| 43 |
-
# Title and description and article for the interface
|
| 44 |
-
title = "Zero Shot Text Classification"
|
| 45 |
-
description = "Classify text using zero-shot classification with ModernBERT-large zeroshot model! Provide a text input and a list of candidate labels separated by commas. Read more at the links below."
|
| 46 |
-
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2412.13663' target='_blank'>Smarter, Better, Faster, Longer: A Modern Bidirectional Encoder for Fast, Memory Efficient, and Long Context Finetuning and Inference</a> | <a href='https://huggingface.co/MoritzLaurer/ModernBERT-large-zeroshot-v2.0' target='_blank'>Model Page</a></p>"
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
# Launch the interface
|
| 50 |
-
demo = gr.Interface(
|
| 51 |
-
fn=ZeroShotTextClassification,
|
| 52 |
-
inputs=[gr.Textbox(label="Input"), gr.Textbox(label="Candidate Labels")],
|
| 53 |
-
outputs=gr.Label(label="Classification"),
|
| 54 |
-
title=title,
|
| 55 |
-
description=description,
|
| 56 |
-
article=article,
|
| 57 |
-
examples=examples,
|
| 58 |
-
cache_examples=True,
|
| 59 |
-
cache_mode="lazy",
|
| 60 |
-
theme="Soft",
|
| 61 |
-
flagging_mode="never",
|
| 62 |
-
)
|
| 63 |
-
demo.launch(debug=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|