karalif commited on
Commit
1a6e1ca
verified
1 Parent(s): 971337b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -28
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 analyze_icelandic_text(icelandic_text):
12
- analysis_result = analyze_text(icelandic_text)
13
-
14
- sentiment_label = "Positive" if analysis_result["sentiment"]["label"] == "LABEL_1" else "Negative"
15
- formality_label = "Formal" if analysis_result["formality"]["label"] == "formal" else "Informal"
16
- toxic_label = "Toxic" if analysis_result["toxicity"]["score"] > 0.5 else "Not Toxic"
17
- politeness_label = "Polite" if analysis_result["politeness"]["label"] == "polite" else "Impolite"
18
-
19
- return {
20
- "Translated Text": analysis_result["translated_text"],
21
- "Sentiment": f"{sentiment_label} (Score: {round(analysis_result['sentiment']['score'], 2)})",
22
- "Formality": f"{formality_label} (Score: {round(analysis_result['formality']['score'], 2)})",
23
- "Toxicity": f"{toxic_label} (Score: {round(analysis_result['toxicity']['score'], 2)})",
24
- "Politeness": f"{politeness_label} (Score: {round(analysis_result['politeness']['score'], 2)})"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  }
 
26
 
27
- iface = gr.Interface(
28
- fn=analyze_icelandic_text,
29
- inputs=gr.inputs.Textbox(lines=2, placeholder="Enter an Icelandic sentence here..."),
30
- outputs=[
31
- gr.outputs.Textbox(label="Translated Text"),
32
- gr.outputs.Textbox(label="Sentiment"),
33
- gr.outputs.Textbox(label="Formality"),
34
- gr.outputs.Textbox(label="Toxicity"),
35
- gr.outputs.Textbox(label="Politeness"),
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
- iface.launch()
 
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()