karalif commited on
Commit
6c4f7e8
·
verified ·
1 Parent(s): 346d947

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -14
app.py CHANGED
@@ -17,10 +17,8 @@ politeness_classifier = pipeline("text-classification", model="Genius1237/xlm-ro
17
  def analyze_sentiment_with_influence(icelandic_text):
18
  sentiment_label, sentiment_score = analyze_sentiment(icelandic_text)
19
 
20
- # Convert sentiment label
21
  sentiment_label = sentiment_label.replace("LABEL_", "")
22
 
23
- # Use Ferret to analyze influential words for sentiment
24
  explanations_sentiment = sentiment_bench.explain(icelandic_text, target=1) # Adjust target as necessary
25
 
26
  influential_words = []
@@ -30,7 +28,6 @@ def analyze_sentiment_with_influence(icelandic_text):
30
  token_score_pairs = zip(tokens, explanation.scores)
31
  influential_words.extend([(token, score) for token, score in token_score_pairs])
32
 
33
- # Format your response to include influential words
34
  influential_words_str = "; ".join([f"{token} ({score:.2f})" for token, score in influential_words])
35
 
36
  analysis_results = (
@@ -39,6 +36,27 @@ def analyze_sentiment_with_influence(icelandic_text):
39
  )
40
  return analysis_results
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def translate_text(text):
43
  translation = translator(text, max_length=512)
44
  return translation[0]['translation_text']
@@ -65,33 +83,26 @@ def analyze_sentiment(text):
65
 
66
  def analyze_text(icelandic_text):
67
  formality_label, formality_score = analyze_formality(icelandic_text)
68
- sentiment_label, sentiment_score = analyze_sentiment(icelandic_text)
69
-
70
- # Convert sentiment label
71
- sentiment_label = sentiment_label.replace("LABEL_", "")
72
-
73
  translated_text = translate_text(icelandic_text)
74
-
75
  toxicity_results = analyze_toxicity(translated_text)
76
  if isinstance(toxicity_results, list):
77
  toxicity_results = toxicity_results[0]
78
-
79
- # Determine toxicity label based on score
80
  toxicity_label = '1' if toxicity_results['score'] >= 0.5 else '0'
81
-
82
  politeness_label, politeness_score = analyze_politeness(translated_text)
83
- # Convert politeness label to binary
84
  politeness_label = '1' if politeness_label.lower() == 'polite' else '0'
85
 
 
 
86
  analysis_results = (
87
  f"Translated Text: {translated_text}\n\n"
88
- f"Sentiment: Label: {sentiment_label}, Score: {round(sentiment_score, 2)}\n"
89
  f"Formality: Label: {formality_label}, Score: {round(formality_score, 2)}\n"
90
  f"Toxicity: Label: {toxicity_label}, Score: {round(toxicity_results['score'], 2)}\n"
91
  f"Politeness: Label: {politeness_label}, Score: {round(politeness_score, 2)}"
92
  )
93
  return analysis_results
94
 
 
95
  demo = gr.Interface(fn=analyze_text,
96
  inputs=gr.Textbox(lines=2, placeholder="Enter Icelandic Text Here..."),
97
  outputs=gr.Textbox(label="Analysis Results"),
 
17
  def analyze_sentiment_with_influence(icelandic_text):
18
  sentiment_label, sentiment_score = analyze_sentiment(icelandic_text)
19
 
 
20
  sentiment_label = sentiment_label.replace("LABEL_", "")
21
 
 
22
  explanations_sentiment = sentiment_bench.explain(icelandic_text, target=1) # Adjust target as necessary
23
 
24
  influential_words = []
 
28
  token_score_pairs = zip(tokens, explanation.scores)
29
  influential_words.extend([(token, score) for token, score in token_score_pairs])
30
 
 
31
  influential_words_str = "; ".join([f"{token} ({score:.2f})" for token, score in influential_words])
32
 
33
  analysis_results = (
 
36
  )
37
  return analysis_results
38
 
39
+ def replace_encoding(tokens):
40
+ return [token.replace('Ġ', ' ')
41
+ .replace('ð', 'ð')
42
+ .replace('é', 'é')
43
+ .replace('æ', 'æ')
44
+ .replace('ý', 'ý')
45
+ .replace('á', 'á')
46
+ .replace('ú', 'ú')
47
+ .replace('ÃŃ', 'í')
48
+ .replace('Ãö', 'ö')
49
+ .replace('þ', 'þ')
50
+ .replace('Ãģ', 'Á')
51
+ .replace('Ãį', 'Ú')
52
+ .replace('Ãĵ', 'Ó')
53
+ .replace('ÃĨ', 'Æ')
54
+ .replace('ÃIJ', 'Ð')
55
+ .replace('Ãĸ', 'Ö')
56
+ .replace('Ãī', 'É')
57
+ .replace('Ãļ', 'ý')
58
+ for token in tokens[1:-1]]
59
+
60
  def translate_text(text):
61
  translation = translator(text, max_length=512)
62
  return translation[0]['translation_text']
 
83
 
84
  def analyze_text(icelandic_text):
85
  formality_label, formality_score = analyze_formality(icelandic_text)
 
 
 
 
 
86
  translated_text = translate_text(icelandic_text)
 
87
  toxicity_results = analyze_toxicity(translated_text)
88
  if isinstance(toxicity_results, list):
89
  toxicity_results = toxicity_results[0]
 
 
90
  toxicity_label = '1' if toxicity_results['score'] >= 0.5 else '0'
 
91
  politeness_label, politeness_score = analyze_politeness(translated_text)
 
92
  politeness_label = '1' if politeness_label.lower() == 'polite' else '0'
93
 
94
+ sentiment_analysis_with_influence = analyze_sentiment_with_influence(icelandic_text)
95
+
96
  analysis_results = (
97
  f"Translated Text: {translated_text}\n\n"
98
+ f"{sentiment_analysis_with_influence}\n"
99
  f"Formality: Label: {formality_label}, Score: {round(formality_score, 2)}\n"
100
  f"Toxicity: Label: {toxicity_label}, Score: {round(toxicity_results['score'], 2)}\n"
101
  f"Politeness: Label: {politeness_label}, Score: {round(politeness_score, 2)}"
102
  )
103
  return analysis_results
104
 
105
+
106
  demo = gr.Interface(fn=analyze_text,
107
  inputs=gr.Textbox(lines=2, placeholder="Enter Icelandic Text Here..."),
108
  outputs=gr.Textbox(label="Analysis Results"),