Update app.py
Browse files123454 unmodified lines1011121314151617181920212247 unmodified lines707172737475767778798081828384858687888990import gradio as grfrom transformers import pipeline# Available models for zero-shot classificationAVAILABLE_MODELS = [4 unmodified lines"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 Inputexamples = [["The government announced a new economic policy today aimed at reducing inflation and stabilizing the currency market.",47 unmodified lines}"""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 app1234564 unmodified lines1112131415161718192021222324252627282930313233343536373847 unmodified lines8687888990919293949596979899100101102103104105106107108109110111112import gradio as grfrom transformers import pipelinefrom typing import Dict, List# Available models for zero-shot classificationAVAILABLE_MODELS = [4 unmodified lines"mjwong/mcontriever-xnli"]def classify_text(model_name: str,text: str,labels: str) -> Dict[str, float]:"""Classifies the input text into one of the provided labels using a zero-shot classification model.Args:model_name: The name of the Hugging Face model to use.text: The input text to classify.labels: A comma-separated string of candidate labels.Returns:Dict[str, float]: A dictionary mapping each label to its classification score."""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 with Mutually Exclusive Labels from News Articlesexamples = [["The government announced a new economic policy today aimed at reducing inflation and stabilizing the currency market.",47 unmodified lines}"""# Initialize Gradio interfacewith 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 the text using a zero-shot classification model.")# Dropdown to select a modelmodel_dropdown = gr.Dropdown(AVAILABLE_MODELS, label="Choose Model")# Input fields for text and labelswith 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 displayoutput_label = gr.Label(label="Classification Scores")# Classification buttonsubmit_button = gr.Button("Classify")submit_button.click(fn=classify_text, inputs=[model_dropdown, text_input, label_input], outputs=output_label)# Example input/output pairsgr.Examples(examples, inputs=[text_input, label_input])# Launch the app
app.py+30-8