first commit
Browse files
README.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
emoji: π
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: red
|
|
@@ -10,4 +10,53 @@ pinned: false
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: zstc
|
| 3 |
emoji: π
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: red
|
|
|
|
| 10 |
license: mit
|
| 11 |
---
|
| 12 |
|
| 13 |
+
# Zero-Shot Text Classification with BART
|
| 14 |
+
|
| 15 |
+
This project demonstrates a web application built with Gradio that utilizes the `facebook/bart-large-mnli` model for zero-shot text classification. Users can input text and specify candidate labels to see how the model classifies the input without having been directly trained on those labels.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- **Zero-Shot Classification:** Classify text into user-specified categories without direct training on those categories.
|
| 20 |
+
- **User-Friendly Interface:** Easy-to-use web interface built with Gradio.
|
| 21 |
+
- **Multi-Label Support:** Option for multi-label classification, allowing a single piece of text to belong to multiple categories.
|
| 22 |
+
|
| 23 |
+
## Installation
|
| 24 |
+
|
| 25 |
+
To run this project, you will need Python and pip. First, clone this repository and navigate to the project directory. Then, install the required dependencies:
|
| 26 |
+
|
| 27 |
+
```bash
|
| 28 |
+
pip install gradio transformers
|
| 29 |
+
```
|
| 30 |
+
|
| 31 |
+
## Usage
|
| 32 |
+
|
| 33 |
+
To start the application, run the Python script:
|
| 34 |
+
|
| 35 |
+
```bash
|
| 36 |
+
python app.py
|
| 37 |
+
```
|
| 38 |
+
|
| 39 |
+
Navigate to the URL provided by Gradio in your terminal to access the web interface.
|
| 40 |
+
|
| 41 |
+
## Examples
|
| 42 |
+
|
| 43 |
+
The application includes predefined examples that demonstrate how to use the interface:
|
| 44 |
+
|
| 45 |
+
- "The market has been incredibly volatile this year, with tech stocks leading the charge." with labels "finance, technology, sports, education"
|
| 46 |
+
- "LeBron James scores 30 points to lead the Lakers to a Game 7 victory over the Celtics." with labels "sports, technology, finance, entertainment"
|
| 47 |
+
- And more...
|
| 48 |
+
|
| 49 |
+
## Customization
|
| 50 |
+
|
| 51 |
+
You can customize the candidate labels and select whether the classification should be multi-label directly in the interface.
|
| 52 |
+
|
| 53 |
+
## Technology
|
| 54 |
+
|
| 55 |
+
This project is built using the following technologies:
|
| 56 |
+
|
| 57 |
+
- **Gradio:** An open-source library to build ML-powered web apps.
|
| 58 |
+
- **Transformers:** A state-of-the-art natural language processing library.
|
| 59 |
+
|
| 60 |
+
## Author
|
| 61 |
+
|
| 62 |
+
- [Lucian BLETAN](https://github.com/exaluc)
|
app.py
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Initialize the zero-shot classification pipeline with the BART model
|
| 5 |
+
classifier = pipeline("zero-shot-classification", model="facebook/bart-large-mnli")
|
| 6 |
+
|
| 7 |
+
def classify_text(sequence, candidate_labels, multi_label):
|
| 8 |
+
# Split candidate labels entered by the user
|
| 9 |
+
labels = [label.strip() for label in candidate_labels.split(',')]
|
| 10 |
+
# Perform classification
|
| 11 |
+
results = classifier(sequence, labels, multi_label=multi_label)
|
| 12 |
+
# Format the results
|
| 13 |
+
formatted_results = {label: score for label, score in zip(results['labels'], results['scores'])}
|
| 14 |
+
return formatted_results
|
| 15 |
+
|
| 16 |
+
# Examples for the interface
|
| 17 |
+
examples = [
|
| 18 |
+
["The market has been incredibly volatile this year, with tech stocks leading the charge.", "finance, technology, sports, education", False],
|
| 19 |
+
["LeBron James scores 30 points to lead the Lakers to a Game 7 victory over the Celtics.", "sports, technology, finance, entertainment", False],
|
| 20 |
+
["Tesla's new battery technology could revolutionize the electric vehicle industry.", "technology, finance, environment, education", False],
|
| 21 |
+
["The local school district has announced a new STEM initiative to better prepare students for careers in technology.", "education, technology, politics, finance", False],
|
| 22 |
+
]
|
| 23 |
+
|
| 24 |
+
# Define Gradio interface components
|
| 25 |
+
iface = gr.Interface(fn=classify_text,
|
| 26 |
+
inputs=[gr.Textbox(label="Text to classify"),
|
| 27 |
+
gr.Textbox(label="Candidate labels (comma-separated)"),
|
| 28 |
+
gr.Checkbox(label="Multi-label classification", value=False)],
|
| 29 |
+
outputs=gr.JSON(label="Classification Results"),
|
| 30 |
+
title="Zero-Shot Text Classification with BART",
|
| 31 |
+
description="This model uses 'bart-large-mnli' for zero-shot text classification. Enter text to classify, provide candidate labels separated by commas, and select whether it's multi-label classification.",
|
| 32 |
+
examples=examples,
|
| 33 |
+
css="footer{display:none !important}",
|
| 34 |
+
allow_flagging="never")
|
| 35 |
+
|
| 36 |
+
if __name__ == "__main__":
|
| 37 |
+
iface.launch()
|