mjwong's picture
Create app.py
7535af8 verified
raw
history blame
2.73 kB
import gradio as gr
from transformers import pipeline
# Available models for zero-shot classification
AVAILABLE_MODELS = [
"mjwong/multilingual-e5-large-instruct-xnli-anli",
"mjwong/multilingual-e5-base-xnli-anli",
"mjwong/multilingual-e5-large-xnli-anli",
"mjwong/mcontriever-msmarco-xnli",
"mjwong/mcontriever-xnli"
]
def classify_text(model_name, text, labels):
classifier = pipeline("zero-shot-classification", model=model_name)
labels_list = [label.strip() for label in labels.split(",")]
result = classifier(text, candidate_labels=labels_list)
return {label: score for label, score in zip(result["labels"], result["scores"])}
# Example Input
examples = [["One day I will see the world", "travel, live, die, future"]]
# Define the Gradio interface
css = """
footer {display:none !important}
.output-markdown{display:none !important}
.gr-button-primary {
z-index: 14;
height: 43px;
width: 130px;
left: 0px;
top: 0px;
padding: 0px;
cursor: pointer !important;
background: none rgb(17, 20, 45) !important;
border: none !important;
text-align: center !important;
font-family: Poppins !important;
font-size: 14px !important;
font-weight: 500 !important;
color: rgb(255, 255, 255) !important;
line-height: 1 !important;
border-radius: 12px !important;
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
box-shadow: none !important;
}
.gr-button-primary:hover{
z-index: 14;
height: 43px;
width: 130px;
left: 0px;
top: 0px;
padding: 0px;
cursor: pointer !important;
background: none rgb(66, 133, 244) !important;
border: none !important;
text-align: center !important;
font-family: Poppins !important;
font-size: 14px !important;
font-weight: 500 !important;
color: rgb(255, 255, 255) !important;
line-height: 1 !important;
border-radius: 12px !important;
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
}
"""
iface = gr.Interface(
fn=classify_text,
inputs=[
gr.Dropdown(AVAILABLE_MODELS, label="Choose Model"),
gr.Textbox(label="Enter Text", placeholder="Type or paste text here..."),
gr.Textbox(label="Enter Labels (comma-separated)", placeholder="e.g., sports, politics, technology")
],
outputs=gr.Label(label="Classification Scores"),
title="Zero-Shot Text Classifier",
description="Select a model, enter text, and a set of labels to classify it using a zero-shot classification model.",
examples=examples,
css=css
)
# Launch the app
if __name__ == "__main__":
iface.launch()