File size: 1,717 Bytes
7420aa9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Gradio app that showcases Scandinavian zero-shot text classification models."""

import gradio as gr
from transformers import pipeline
from luga import language as detect_language


# Load the zero-shot classification pipeline
classifier = pipeline(
    "zero-shot-classification", model="alexandrainst/scandi-nli-large"
)


def sentiment_classification(doc: str) -> str:
    """Classify text into sentiment categories.

    Args:
        doc (str):
            Text to classify.

    Returns:
        str:
            The predicted sentiment category.
    """
    # Detect the language of the text
    language = detect_language(doc).name

    # Get hypothesis template and candidate labels depending on the language
    if language == "da":
        hypothesis_template = "Dette eksempel er {}."
        candidate_labels = ["positivt", "negativt", "neutralt"]
    elif language == "sv":
        hypothesis_template = "Detta exempel är {}."
        candidate_labels = ["positivt", "negativt", "neutralt"]
    elif language == "no":
        hypothesis_template = "Dette eksemplet er {}."
        candidate_labels = ["positivt", "negativt", "nøytralt"]

    # Run the classifier on the text
    result = classifier(
        doc, candidate_labels=candidate_labels, hypothesis_template=hypothesis_template
    )

    # Return the predicted label
    return result["labels"][0]


# Create the Gradio interface
interface = gr.Interface(
    fn=sentiment_classification,
    inputs=gr.inputs.Textbox(lines=5, label="Text"),
    outputs=gr.outputs.Label(type="text"),
    title="Scandinavian Zero-Shot Text Classification",
    description="Classify text into sentiment categories.",
)

# Run the app
interface.launch()