Update app.py
Browse files14 unmodified lines1516171819202117 unmodified lines3940414243444556 unmodified lines1021031041051061071081095 unmodified lines11511611711811912012114 unmodified linesdef 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.17 unmodified linesclassifier = pipeline("zero-shot-classification", model=model_name, device=device)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"])}except Exception as _:return "Error: An unexpected error occurred. Please try again later."56 unmodified linesgr.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():5 unmodified lines# Classification buttonsubmit_button = gr.Button("Classify", elem_classes=["classify-button"])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])14 unmodified lines151617181920212217 unmodified lines4041424344454656 unmodified lines1031041051061071081091101111121135 unmodified lines11912012112212312412514 unmodified linesdef classify_text(model_name: str,text: str,labels: str,multi_label: bool = False,) -> Dict[str, float]:"""Classifies the input text into one of the provided labels using a zero-shot classification model.17 unmodified linesclassifier = pipeline("zero-shot-classification", model=model_name, device=device)labels_list = [label.strip() for label in labels.split(",")]result = classifier(text, candidate_labels=labels_list, multi_label=multi_label)return {label: score for label, score in zip(result["labels"], result["scores"])}except Exception as _:return "Error: An unexpected error occurred. Please try again later."56 unmodified linesgr.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.")with gr.Row():# Dropdown to select a modelmodel_dropdown = gr.Dropdown(AVAILABLE_MODELS, label="Choose Model")# Checkbox for multi-label classificationmulti_label = gr.Checkbox(label="True", value=False, info="Check for multi-label classification, uncheck for single-label (multi-class).")# Input fields for text and labelswith gr.Row():5 unmodified lines# Classification buttonsubmit_button = gr.Button("Classify", elem_classes=["classify-button"])submit_button.click(fn=classify_text, inputs=[model_dropdown, text_input, label_input, multi_label], outputs=output_label)# Example input/output pairsgr.Examples(examples, inputs=[text_input, label_input])
app.py+9-5