Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,35 +8,60 @@ formality_classifier = pipeline("text-classification", model="svanhvit/formality
|
|
8 |
detoxify_pipeline = pipeline('text-classification', model='unitary/toxic-bert', tokenizer='bert-base-uncased', function_to_apply='sigmoid', return_all_scores=True)
|
9 |
politeness_classifier = pipeline("text-classification", model="Genius1237/xlm-roberta-large-tydip")
|
10 |
|
11 |
-
def
|
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 |
-
title="Icelandic Text Analysis",
|
38 |
-
description="This app translates Icelandic text to English and analyzes its sentiment, formality, toxicity, and politeness."
|
39 |
-
)
|
40 |
|
41 |
if __name__ == "__main__":
|
42 |
-
|
|
|
8 |
detoxify_pipeline = pipeline('text-classification', model='unitary/toxic-bert', tokenizer='bert-base-uncased', function_to_apply='sigmoid', return_all_scores=True)
|
9 |
politeness_classifier = pipeline("text-classification", model="Genius1237/xlm-roberta-large-tydip")
|
10 |
|
11 |
+
def translate_text(text):
|
12 |
+
translation = translator(text, max_length=512)
|
13 |
+
return translation[0]['translation_text']
|
14 |
+
|
15 |
+
def analyze_toxicity(text):
|
16 |
+
toxicity_results = detoxify_pipeline(text)
|
17 |
+
return toxicity_results[0]
|
18 |
+
|
19 |
+
def analyze_politeness(text):
|
20 |
+
politeness_result = politeness_classifier(text)
|
21 |
+
return politeness_result[0]['label'], politeness_result[0]['score']
|
22 |
+
|
23 |
+
def analyze_formality(text):
|
24 |
+
formality_result = formality_classifier(text)
|
25 |
+
formality_label = formality_result[0]['label']
|
26 |
+
formality_score = formality_result[0]['score']
|
27 |
+
return formality_label, formality_score
|
28 |
+
|
29 |
+
def analyze_sentiment(text):
|
30 |
+
sentiment_result = sentiment_classifier(text)
|
31 |
+
sentiment_label = sentiment_result[0]['label']
|
32 |
+
sentiment_score = sentiment_result[0]['score']
|
33 |
+
return sentiment_label, sentiment_score
|
34 |
+
|
35 |
+
def analyze_text(icelandic_text):
|
36 |
+
formality_label, formality_score = analyze_formality(icelandic_text)
|
37 |
+
sentiment_label, sentiment_score = analyze_sentiment(icelandic_text)
|
38 |
+
|
39 |
+
translated_text = translate_text(icelandic_text)
|
40 |
+
|
41 |
+
toxicity_results = analyze_toxicity(translated_text)
|
42 |
+
if isinstance(toxicity_results, list):
|
43 |
+
toxicity_results = toxicity_results[0]
|
44 |
+
|
45 |
+
politeness_label, politeness_score = analyze_politeness(translated_text)
|
46 |
+
|
47 |
+
analysis_results = {
|
48 |
+
"Translated Text": translated_text,
|
49 |
+
"Sentiment": f"Label: {sentiment_label}, Score: {round(sentiment_score, 2)}",
|
50 |
+
"Formality": f"Label: {formality_label}, Score: {round(formality_score, 2)}",
|
51 |
+
"Toxicity": f"Score: {round(toxicity_results['score'], 2)}",
|
52 |
+
"Politeness": f"Label: {politeness_label}, Score: {round(politeness_score, 2)}"
|
53 |
}
|
54 |
+
return analysis_results
|
55 |
|
56 |
+
demo = gr.Interface(fn=analyze_text,
|
57 |
+
inputs="text",
|
58 |
+
outputs=[gr.outputs.Textbox(label="Translated Text"),
|
59 |
+
gr.outputs.Textbox(label="Sentiment"),
|
60 |
+
gr.outputs.Textbox(label="Formality"),
|
61 |
+
gr.outputs.Textbox(label="Toxicity"),
|
62 |
+
gr.outputs.Textbox(label="Politeness")],
|
63 |
+
title="Icelandic Text Analysis",
|
64 |
+
description="This app translates Icelandic text to English and performs sentiment, formality, toxicity, and politeness analysis.")
|
|
|
|
|
|
|
|
|
65 |
|
66 |
if __name__ == "__main__":
|
67 |
+
demo.launch()
|