Update app.py
Browse files
app.py
CHANGED
@@ -1,5 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
|
|
3 |
|
4 |
# Available models for zero-shot classification
|
5 |
AVAILABLE_MODELS = [
|
@@ -10,13 +11,28 @@ AVAILABLE_MODELS = [
|
|
10 |
"mjwong/mcontriever-xnli"
|
11 |
]
|
12 |
|
13 |
-
def classify_text(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
classifier = pipeline("zero-shot-classification", model=model_name)
|
15 |
labels_list = [label.strip() for label in labels.split(",")]
|
16 |
result = classifier(text, candidate_labels=labels_list)
|
17 |
return {label: score for label, score in zip(result["labels"], result["scores"])}
|
18 |
|
19 |
-
# Example Input
|
20 |
examples = [
|
21 |
[
|
22 |
"The government announced a new economic policy today aimed at reducing inflation and stabilizing the currency market.",
|
@@ -70,21 +86,27 @@ footer {display:none !important}
|
|
70 |
}
|
71 |
"""
|
72 |
|
|
|
73 |
with gr.Blocks(css=css) as iface:
|
74 |
gr.Markdown("# Zero-Shot Text Classifier")
|
75 |
-
gr.Markdown("Select a model, enter text, and a set of labels to classify
|
76 |
-
|
|
|
77 |
model_dropdown = gr.Dropdown(AVAILABLE_MODELS, label="Choose Model")
|
78 |
-
|
|
|
79 |
with gr.Row():
|
80 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type or paste text here...")
|
81 |
label_input = gr.Textbox(label="Enter Labels (comma-separated)", placeholder="e.g., sports, politics, technology")
|
82 |
-
|
|
|
83 |
output_label = gr.Label(label="Classification Scores")
|
84 |
-
|
|
|
85 |
submit_button = gr.Button("Classify")
|
86 |
submit_button.click(fn=classify_text, inputs=[model_dropdown, text_input, label_input], outputs=output_label)
|
87 |
-
|
|
|
88 |
gr.Examples(examples, inputs=[text_input, label_input])
|
89 |
|
90 |
# Launch the app
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
+
from typing import Dict, List
|
4 |
|
5 |
# Available models for zero-shot classification
|
6 |
AVAILABLE_MODELS = [
|
|
|
11 |
"mjwong/mcontriever-xnli"
|
12 |
]
|
13 |
|
14 |
+
def classify_text(
|
15 |
+
model_name: str,
|
16 |
+
text: str,
|
17 |
+
labels: str
|
18 |
+
) -> Dict[str, float]:
|
19 |
+
"""
|
20 |
+
Classifies the input text into one of the provided labels using a zero-shot classification model.
|
21 |
+
|
22 |
+
Args:
|
23 |
+
model_name: The name of the Hugging Face model to use.
|
24 |
+
text: The input text to classify.
|
25 |
+
labels: A comma-separated string of candidate labels.
|
26 |
+
|
27 |
+
Returns:
|
28 |
+
Dict[str, float]: A dictionary mapping each label to its classification score.
|
29 |
+
"""
|
30 |
classifier = pipeline("zero-shot-classification", model=model_name)
|
31 |
labels_list = [label.strip() for label in labels.split(",")]
|
32 |
result = classifier(text, candidate_labels=labels_list)
|
33 |
return {label: score for label, score in zip(result["labels"], result["scores"])}
|
34 |
|
35 |
+
# Example Input with Mutually Exclusive Labels from News Articles
|
36 |
examples = [
|
37 |
[
|
38 |
"The government announced a new economic policy today aimed at reducing inflation and stabilizing the currency market.",
|
|
|
86 |
}
|
87 |
"""
|
88 |
|
89 |
+
# Initialize Gradio interface
|
90 |
with gr.Blocks(css=css) as iface:
|
91 |
gr.Markdown("# Zero-Shot Text Classifier")
|
92 |
+
gr.Markdown("Select a model, enter text, and a set of labels to classify the text using a zero-shot classification model.")
|
93 |
+
|
94 |
+
# Dropdown to select a model
|
95 |
model_dropdown = gr.Dropdown(AVAILABLE_MODELS, label="Choose Model")
|
96 |
+
|
97 |
+
# Input fields for text and labels
|
98 |
with gr.Row():
|
99 |
text_input = gr.Textbox(label="Enter Text", placeholder="Type or paste text here...")
|
100 |
label_input = gr.Textbox(label="Enter Labels (comma-separated)", placeholder="e.g., sports, politics, technology")
|
101 |
+
|
102 |
+
# Output display
|
103 |
output_label = gr.Label(label="Classification Scores")
|
104 |
+
|
105 |
+
# Classification button
|
106 |
submit_button = gr.Button("Classify")
|
107 |
submit_button.click(fn=classify_text, inputs=[model_dropdown, text_input, label_input], outputs=output_label)
|
108 |
+
|
109 |
+
# Example input/output pairs
|
110 |
gr.Examples(examples, inputs=[text_input, label_input])
|
111 |
|
112 |
# Launch the app
|