Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -25,33 +25,35 @@ def get_prediction(text):
|
|
25 |
kw_model = KeyBERT()
|
26 |
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 1), stop_words='english', use_maxsum=True, nr_candidates=20, top_n=5)
|
27 |
|
28 |
-
#
|
29 |
-
response = "
|
30 |
labels = ['Politeness', 'Toxicity', 'Sentiment', 'Formality']
|
31 |
colors = ['#b8e994', '#f8d7da', '#fff3cd', '#bee5eb'] # Corresponding colors for labels
|
32 |
|
33 |
for i, label in enumerate(labels):
|
34 |
response += f"<span style='background-color:{colors[i]}; color:black;'>{label}</span>: {probs[i]*100:.1f}%<br>"
|
35 |
|
36 |
-
|
37 |
for keyword, score in keywords:
|
38 |
-
|
39 |
-
|
40 |
-
return response
|
41 |
|
42 |
def predict(text):
|
43 |
greeting_pattern = r"^(Halló|Hæ|Sæl|Góðan dag|Kær kveðja|Daginn|Kvöldið|Ágætis|Elsku)"
|
44 |
|
45 |
-
prediction_output = get_prediction(text)
|
46 |
greeting_feedback = ""
|
47 |
|
|
|
|
|
|
|
|
|
|
|
48 |
if not re.match(greeting_pattern, text, re.IGNORECASE):
|
49 |
greeting_feedback = "OTHER FEEDBACK:<br>Heilsaðu dóninn þinn<br>"
|
50 |
|
51 |
-
|
52 |
-
input_text_display = f"INPUT:<br>{text}<br><br>"
|
53 |
-
|
54 |
-
response = input_text_display + prediction_output + "<br>" + greeting_feedback
|
55 |
|
56 |
return response
|
57 |
|
|
|
25 |
kw_model = KeyBERT()
|
26 |
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 1), stop_words='english', use_maxsum=True, nr_candidates=20, top_n=5)
|
27 |
|
28 |
+
# Prepare the HTML output with labels and their probabilities
|
29 |
+
response = ""
|
30 |
labels = ['Politeness', 'Toxicity', 'Sentiment', 'Formality']
|
31 |
colors = ['#b8e994', '#f8d7da', '#fff3cd', '#bee5eb'] # Corresponding colors for labels
|
32 |
|
33 |
for i, label in enumerate(labels):
|
34 |
response += f"<span style='background-color:{colors[i]}; color:black;'>{label}</span>: {probs[i]*100:.1f}%<br>"
|
35 |
|
36 |
+
influential_keywords = "<br>INFLUENTIAL KEYWORDS:<br>"
|
37 |
for keyword, score in keywords:
|
38 |
+
influential_keywords += f"{keyword} (Score: {score:.2f})<br>"
|
39 |
+
|
40 |
+
return response, keywords, influential_keywords
|
41 |
|
42 |
def predict(text):
|
43 |
greeting_pattern = r"^(Halló|Hæ|Sæl|Góðan dag|Kær kveðja|Daginn|Kvöldið|Ágætis|Elsku)"
|
44 |
|
45 |
+
prediction_output, keywords, influential_keywords = get_prediction(text)
|
46 |
greeting_feedback = ""
|
47 |
|
48 |
+
# Highlight the keywords in the input text
|
49 |
+
modified_input = text
|
50 |
+
for keyword, _ in keywords:
|
51 |
+
modified_input = modified_input.replace(keyword, f"<span style='color:green;'>{keyword}</span>")
|
52 |
+
|
53 |
if not re.match(greeting_pattern, text, re.IGNORECASE):
|
54 |
greeting_feedback = "OTHER FEEDBACK:<br>Heilsaðu dóninn þinn<br>"
|
55 |
|
56 |
+
response = f"INPUT:<br>{modified_input}<br><br>MY PREDICTION:<br>{prediction_output}<br>{influential_keywords}<br>{greeting_feedback}"
|
|
|
|
|
|
|
57 |
|
58 |
return response
|
59 |
|