Spaces:
Sleeping
Sleeping
Upload the requirements file and app.py file
Browse files- app.py +59 -0
- requirements.txt +5 -0
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Necessary imports
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Load the zero-shot classification model
|
| 6 |
+
classifier = pipeline(
|
| 7 |
+
"zero-shot-classification", model="MoritzLaurer/deberta-v3-large-zeroshot-v2.0"
|
| 8 |
+
)
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
# Function to perform zero-shot classification
|
| 12 |
+
def ZeroShotTextClassification(text_input, candidate_labels):
|
| 13 |
+
"""
|
| 14 |
+
Performs zero-shot classification on the given text input using the provided candidate labels.
|
| 15 |
+
|
| 16 |
+
Args:
|
| 17 |
+
text_input (str): The input text to classify.
|
| 18 |
+
candidate_labels (str): A comma-separated string of candidate labels.
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
dict: A dictionary containing the predicted labels as keys and their corresponding scores as values.
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
# Split the candidate labels
|
| 25 |
+
labels = [label.strip(" ") for label in candidate_labels.split(",")]
|
| 26 |
+
|
| 27 |
+
# Output dictionary to store the predicted labels and their scores
|
| 28 |
+
output = {}
|
| 29 |
+
|
| 30 |
+
# Perform zero-shot classification
|
| 31 |
+
prediction = classifier(text_input, labels)
|
| 32 |
+
|
| 33 |
+
# Create a dictionary with the predicted labels and their corresponding scores
|
| 34 |
+
for i in range(len(prediction["labels"])):
|
| 35 |
+
output[prediction["labels"][i]] = prediction["scores"][i]
|
| 36 |
+
|
| 37 |
+
# Return the output
|
| 38 |
+
return output
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# Examples to display in the interface
|
| 42 |
+
examples = [
|
| 43 |
+
["I love to play the guitar", "music, artist, food, travel"],
|
| 44 |
+
["I am a software engineer at Google", "technology, engineering, art, science"],
|
| 45 |
+
["I am a professional basketball player", "sports, athlete, chef, politics"],
|
| 46 |
+
]
|
| 47 |
+
|
| 48 |
+
# Launch the interface
|
| 49 |
+
demo = gr.Interface(
|
| 50 |
+
fn=ZeroShotTextClassification,
|
| 51 |
+
inputs=[gr.Textbox(label="Input"), gr.Textbox(label="Candidate Labels")],
|
| 52 |
+
outputs=gr.Label(label="Classification"),
|
| 53 |
+
title="Zero Shot Text Classification",
|
| 54 |
+
description="Classify text using zero-shot classification with DeBERTa-v3-large-zeroshot model! Provide a text input and a list of candidate labels separated by commas.",
|
| 55 |
+
examples=examples,
|
| 56 |
+
theme="Soft",
|
| 57 |
+
allow_flagging="never",
|
| 58 |
+
)
|
| 59 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
torch
|
| 3 |
+
timm
|
| 4 |
+
sentencepiece
|
| 5 |
+
transformers
|