mjwong's picture
Update app.py
14ff620 verified
raw
history blame
4.01 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 = [
[
"The government announced a new economic policy today aimed at reducing inflation and stabilizing the currency market.",
"economy, politics, finance, policy, inflation, government, currency"
],
[
"中国的科技公司在人工智能领域取得了重大突破,这可能会影响全球市场。",
"科技, 经济, 创新, 市场, 人工智能, 全球"
],
[
"นักวิจัยค้นพบวิธีใหม่ในการรักษาโรคมะเร็ง ซึ่งอาจช่วยชีวิตผู้ป่วยหลายล้านคนทั่วโลก",
"การแพทย์, วิทยาศาสตร์, นวัตกรรม, สุขภาพ, โรคมะเร็ง, การรักษา"
],
[
"La conférence des Nations Unies sur le climat a abouti à un nouvel accord pour réduire les émissions de carbone d'ici 2030.",
"environnement, climat, politique, énergie, carbone, écologie, ONU"
],
[
"सरकार ने आज एक नई आर्थिक नीति की घोषणा की, जिसका उद्देश्य मुद्रास्फीति को कम करना और मुद्रा बाजार को स्थिर करना है।",
"अर्थव्यवस्था, राजनीति, वित्त, नीति, मुद्रास्फीति, सरकार, मुद्रा"
]
]
# 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{
background: none rgb(66, 133, 244) !important;
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
}
"""
with gr.Blocks(css=css) as iface:
gr.Markdown("# Zero-Shot Text Classifier")
gr.Markdown("Select a model, enter text, and a set of labels to classify it using a zero-shot classification model.")
model_dropdown = gr.Dropdown(AVAILABLE_MODELS, label="Choose Model")
with gr.Row():
text_input = gr.Textbox(label="Enter Text", placeholder="Type or paste text here...")
label_input = gr.Textbox(label="Enter Labels (comma-separated)", placeholder="e.g., sports, politics, technology")
output_label = gr.Label(label="Classification Scores")
submit_button = gr.Button("Classify")
submit_button.click(fn=classify_text, inputs=[model_dropdown, text_input, label_input], outputs=output_label)
gr.Examples(examples, inputs=[text_input, label_input])
# Launch the app
if __name__ == "__main__":
iface.launch()