File size: 2,264 Bytes
9fa836d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from transformers import pipeline
import gradio as gr

# Initialize text classification pipeline
classifier = pipeline("text-classification", model="facebook/bart-large-mnli")

def classify_text(text):
    if not text.strip():
        return "Please enter some text to classify"
    
    try:
        # Get classification results
        results = classifier(text)
        
        # Format results
        output = "## Classification Results:\n\n"
        for result in results:
            label = result['label']
            score = result['score'] * 100
            output += f"- **{label}**: {score:.2f}%\n"
        
        return output
    except Exception as e:
        return f"Error during classification: {str(e)}"

# Gradio interface
with gr.Blocks(title="Text Classifier") as demo:
    gr.Markdown("# 📝 Text Classification AI")
    gr.Markdown("Classify text using Hugging Face's BART model")
    
    with gr.Row():
        with gr.Column():
            input_text = gr.Textbox(
                lines=8,
                placeholder="Enter text to classify...",
                label="Input Text"
            )
            classify_btn = gr.Button("Classify Text", variant="primary")
            
        with gr.Column():
            output_text = gr.Markdown(label="Classification Results")
    
    classify_btn.click(
        classify_text,
        inputs=input_text,
        outputs=output_text
    )
    
    gr.Examples(
        [
            ["I love this movie, it's fantastic!"],
            ["This product is terrible and broke after one day"],
            ["The weather today is sunny and warm"],
            ["Machine learning is a subset of artificial intelligence"],
            ["I'm feeling sad and disappointed about the results"]
        ],
        inputs=input_text
    )
    
    gr.Markdown("### About This Model")
    gr.Markdown("- **Model**: [facebook/bart-large-mnli](https://huggingface.co/facebook/bart-large-mnli)")
    gr.Markdown("- **Task**: Zero-shot text classification")
    gr.Markdown("- **Capabilities**: Classifies text into various categories without specific training")
    gr.Markdown("- **Note**: First classification may take 10-15 seconds (model loading)")

if __name__ == "__main__":
    demo.launch()