File size: 2,232 Bytes
34475ca
3c8522e
 
 
 
 
 
 
318e9b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pkg_resources
from transformers import pipeline
import gradio as gr

translator = pipeline("translation", model="Helsinki-NLP/opus-mt-is-en")
sentiment_classifier = pipeline("text-classification", model="Birkir/electra-base-igc-is-sentiment-analysis")
formality_classifier = pipeline("text-classification", model="svanhvit/formality-classification-icebert")
detoxify_pipeline = pipeline('text-classification', model='unitary/toxic-bert', tokenizer='bert-base-uncased', function_to_apply='sigmoid', return_all_scores=True)
politeness_classifier = pipeline("text-classification", model="Genius1237/xlm-roberta-large-tydip")

def analyze_icelandic_text(icelandic_text):
    analysis_result = analyze_text(icelandic_text)

    sentiment_label = "Positive" if analysis_result["sentiment"]["label"] == "LABEL_1" else "Negative"
    formality_label = "Formal" if analysis_result["formality"]["label"] == "formal" else "Informal"
    toxic_label = "Toxic" if analysis_result["toxicity"]["score"] > 0.5 else "Not Toxic"
    politeness_label = "Polite" if analysis_result["politeness"]["label"] == "polite" else "Impolite"
    
    return {
        "Translated Text": analysis_result["translated_text"],
        "Sentiment": f"{sentiment_label} (Score: {round(analysis_result['sentiment']['score'], 2)})",
        "Formality": f"{formality_label} (Score: {round(analysis_result['formality']['score'], 2)})",
        "Toxicity": f"{toxic_label} (Score: {round(analysis_result['toxicity']['score'], 2)})",
        "Politeness": f"{politeness_label} (Score: {round(analysis_result['politeness']['score'], 2)})"
    }

iface = gr.Interface(
    fn=analyze_icelandic_text,
    inputs=gr.inputs.Textbox(lines=2, placeholder="Enter an Icelandic sentence here..."),
    outputs=[
        gr.outputs.Textbox(label="Translated Text"),
        gr.outputs.Textbox(label="Sentiment"),
        gr.outputs.Textbox(label="Formality"),
        gr.outputs.Textbox(label="Toxicity"),
        gr.outputs.Textbox(label="Politeness"),
    ],
    title="Icelandic Text Analysis",
    description="This app translates Icelandic text to English and analyzes its sentiment, formality, toxicity, and politeness."
)

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