karalif commited on
Commit
1f8139d
verified
1 Parent(s): e5a9d0d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -1
app.py CHANGED
@@ -1,6 +1,12 @@
1
  import pkg_resources
2
- from transformers import pipeline
3
  import gradio as gr
 
 
 
 
 
 
4
 
5
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-is-en")
6
  sentiment_classifier = pipeline("text-classification", model="Birkir/electra-base-igc-is-sentiment-analysis")
@@ -8,6 +14,31 @@ 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', top_k=None)
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']
 
1
  import pkg_resources
2
+ from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
3
  import gradio as gr
4
+ from ferret import Benchmark
5
+
6
+ sentiment_tokenizer = AutoTokenizer.from_pretrained("Birkir/electra-base-igc-is-sentiment-analysis")
7
+ sentiment_model = AutoModelForSequenceClassification.from_pretrained("Birkir/electra-base-igc-is-sentiment-analysis")
8
+
9
+ sentiment_bench = Benchmark(sentiment_model, sentiment_tokenizer)
10
 
11
  translator = pipeline("translation", model="Helsinki-NLP/opus-mt-is-en")
12
  sentiment_classifier = pipeline("text-classification", model="Birkir/electra-base-igc-is-sentiment-analysis")
 
14
  detoxify_pipeline = pipeline('text-classification', model='unitary/toxic-bert', tokenizer='bert-base-uncased', function_to_apply='sigmoid', top_k=None)
15
  politeness_classifier = pipeline("text-classification", model="Genius1237/xlm-roberta-large-tydip")
16
 
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 = []
27
+ for explanation in explanations_sentiment:
28
+ if explanation.explainer == 'Partition SHAP':
29
+ tokens = replace_encoding(explanation.tokens)
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 = (
37
+ f"Sentiment: Label: {sentiment_label}, Score: {round(sentiment_score, 2)}\n"
38
+ f"Influential Words: {influential_words_str}"
39
+ )
40
+ return analysis_results
41
+
42
  def translate_text(text):
43
  translation = translator(text, max_length=512)
44
  return translation[0]['translation_text']