Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -17,14 +17,15 @@ model = AutoModelForSequenceClassification.from_pretrained(name)
|
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(name, normalization=True)
|
| 18 |
|
| 19 |
bench = Benchmark(model, tokenizer)
|
| 20 |
-
text = "hvað er maðurinn eiginlega að pæla ég fatta ekki??????????"
|
| 21 |
-
|
| 22 |
-
explanations_formality = bench.explain(text, target=0)
|
| 23 |
-
explanations_sentiment = bench.explain(text, target=1)
|
| 24 |
-
explanations_politeness = bench.explain(text, target=2)
|
| 25 |
-
explanations_toxicity = bench.explain(text, target=3)
|
| 26 |
|
| 27 |
def get_prediction(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
encoding = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=200)
|
| 29 |
encoding = {k: v.to(model.device) for k, v in encoding.items()}
|
| 30 |
|
|
@@ -72,23 +73,17 @@ def replace_encoding(tokens):
|
|
| 72 |
.replace('Ãļ', 'ý')
|
| 73 |
for token in tokens[1:-1]]
|
| 74 |
|
| 75 |
-
def predict(
|
| 76 |
-
explanations_formality = bench.explain(user_input_text, target=0)
|
| 77 |
-
explanations_sentiment = bench.explain(user_input_text, target=1)
|
| 78 |
-
explanations_politeness = bench.explain(user_input_text, target=2)
|
| 79 |
-
explanations_toxicity = bench.explain(user_input_text, target=3)
|
| 80 |
-
|
| 81 |
-
prediction_output, keywords, influential_keywords = get_prediction(user_input_text)
|
| 82 |
-
|
| 83 |
greeting_pattern = r"^(Halló|Hæ|Sæl|Góðan dag|Kær kveðja|Daginn|Kvöldið|Ágætis|Elsku)"
|
| 84 |
|
|
|
|
| 85 |
greeting_feedback = ""
|
| 86 |
|
| 87 |
-
modified_input =
|
| 88 |
for keyword, _ in keywords:
|
| 89 |
modified_input = modified_input.replace(keyword, f"<span style='color:green;'>{keyword}</span>")
|
| 90 |
|
| 91 |
-
if not re.match(greeting_pattern,
|
| 92 |
greeting_feedback = "OTHER FEEDBACK:<br>Heilsaðu dóninn þinn<br>"
|
| 93 |
|
| 94 |
response = f"INPUT:<br>{modified_input}<br><br>MY PREDICTION:<br>{prediction_output}<br>{influential_keywords}<br>{greeting_feedback}"
|
|
@@ -114,11 +109,10 @@ def predict(user_input_text):
|
|
| 114 |
for explanation in explanations:
|
| 115 |
if explanation.explainer == 'Partition SHAP':
|
| 116 |
sorted_scores = sorted(enumerate(explanation.scores), key=lambda x: abs(x[1]), reverse=True)[:2]
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
response += f"{formatted_output}<br>"
|
| 122 |
|
| 123 |
return response
|
| 124 |
|
|
|
|
| 17 |
tokenizer = AutoTokenizer.from_pretrained(name, normalization=True)
|
| 18 |
|
| 19 |
bench = Benchmark(model, tokenizer)
|
| 20 |
+
#text = "hvað er maðurinn eiginlega að pæla ég fatta ekki??????????"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
def get_prediction(text):
|
| 23 |
+
|
| 24 |
+
explanations_formality = bench.explain(text, target=0)
|
| 25 |
+
explanations_sentiment = bench.explain(text, target=1)
|
| 26 |
+
explanations_politeness = bench.explain(text, target=2)
|
| 27 |
+
explanations_toxicity = bench.explain(text, target=3)
|
| 28 |
+
|
| 29 |
encoding = tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=200)
|
| 30 |
encoding = {k: v.to(model.device) for k, v in encoding.items()}
|
| 31 |
|
|
|
|
| 73 |
.replace('Ãļ', 'ý')
|
| 74 |
for token in tokens[1:-1]]
|
| 75 |
|
| 76 |
+
def predict(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
greeting_pattern = r"^(Halló|Hæ|Sæl|Góðan dag|Kær kveðja|Daginn|Kvöldið|Ágætis|Elsku)"
|
| 78 |
|
| 79 |
+
prediction_output, keywords, influential_keywords = get_prediction(text)
|
| 80 |
greeting_feedback = ""
|
| 81 |
|
| 82 |
+
modified_input = text
|
| 83 |
for keyword, _ in keywords:
|
| 84 |
modified_input = modified_input.replace(keyword, f"<span style='color:green;'>{keyword}</span>")
|
| 85 |
|
| 86 |
+
if not re.match(greeting_pattern, text, re.IGNORECASE):
|
| 87 |
greeting_feedback = "OTHER FEEDBACK:<br>Heilsaðu dóninn þinn<br>"
|
| 88 |
|
| 89 |
response = f"INPUT:<br>{modified_input}<br><br>MY PREDICTION:<br>{prediction_output}<br>{influential_keywords}<br>{greeting_feedback}"
|
|
|
|
| 109 |
for explanation in explanations:
|
| 110 |
if explanation.explainer == 'Partition SHAP':
|
| 111 |
sorted_scores = sorted(enumerate(explanation.scores), key=lambda x: abs(x[1]), reverse=True)[:2]
|
| 112 |
+
tokens = replace_encoding(explanation.tokens)
|
| 113 |
+
tokens = [tokens[idx] for idx, _ in sorted_scores]
|
| 114 |
+
formatted_output = ' '.join(tokens)
|
| 115 |
+
response += f"{formatted_output}<br>"
|
|
|
|
| 116 |
|
| 117 |
return response
|
| 118 |
|