Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,52 @@
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import re
|
|
|
|
|
4 |
|
5 |
text_pipe = pipeline("text-classification", model="karalif/myTestModel", return_all_scores=True)
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def predict(text):
|
8 |
greeting_pattern = r"^(Halló|Hæ|Sæl|Góðan dag|Kær kveðja|Daginn|Kvöldið|Ágætis|Elsku)"
|
9 |
|
|
|
10 |
greeting_feedback = ""
|
11 |
|
12 |
-
results = text_pipe(text)
|
13 |
-
all_scores = results[0]
|
14 |
-
response = ""
|
15 |
-
for result in all_scores:
|
16 |
-
label = result['label']
|
17 |
-
score = result['score']
|
18 |
-
if label == "Politeness":
|
19 |
-
response += f"<span style='background-color:#b8e994; color:black;'>{label}</span>: {score:.3f}<br>" # Light Green
|
20 |
-
elif label == "Sentiment":
|
21 |
-
response += f"<span style='background-color:#fff3cd; color:black;'>{label}</span>: {score:.3f}<br>" # Light Yellow
|
22 |
-
elif label == "Formality":
|
23 |
-
response += f"<span style='background-color:#bee5eb; color:black;'>{label}</span>: {score:.3f}<br>" # Light Blue
|
24 |
-
elif label == "Toxicity":
|
25 |
-
response += f"<span style='background-color:#f8d7da; color:black;'>{label}</span>: {score:.3f}<br>" # Light Red
|
26 |
-
|
27 |
if not re.match(greeting_pattern, text, re.IGNORECASE):
|
28 |
greeting_feedback = "Heilsaðu dóninn þinn<br>"
|
29 |
|
30 |
-
response
|
31 |
|
32 |
return response
|
33 |
|
34 |
-
|
35 |
description_html = """
|
36 |
<center>
|
37 |
<img src='http://www.ru.is/media/HR_logo_vinstri_transparent.png' width='250' height='auto'>
|
|
|
1 |
import gradio as gr
|
2 |
from transformers import pipeline
|
3 |
import re
|
4 |
+
from keybert import KeyBERT
|
5 |
+
import torch
|
6 |
|
7 |
text_pipe = pipeline("text-classification", model="karalif/myTestModel", return_all_scores=True)
|
8 |
|
9 |
+
def get_prediction(text):
|
10 |
+
# Tokenize the input text
|
11 |
+
encoding = new_tokenizer(text, return_tensors="pt", padding="max_length", truncation=True, max_length=200)
|
12 |
+
encoding = {k: v.to(new_model.device) for k, v in encoding.items()}
|
13 |
+
|
14 |
+
with torch.no_grad():
|
15 |
+
outputs = new_model(**encoding)
|
16 |
+
|
17 |
+
logits = outputs.logits
|
18 |
+
sigmoid = torch.nn.Sigmoid()
|
19 |
+
probs = sigmoid(logits.squeeze().cpu()).numpy()
|
20 |
+
|
21 |
+
# Initialize KeyBERT
|
22 |
+
kw_model = KeyBERT()
|
23 |
+
keywords = kw_model.extract_keywords(text, keyphrase_ngram_range=(1, 1), stop_words='english', use_maxsum=True, nr_candidates=20, top_n=5)
|
24 |
+
|
25 |
+
response = "MY PREDICTION:<br>"
|
26 |
+
labels = ['Politeness', 'Toxicity', 'Sentiment', 'Formality']
|
27 |
+
|
28 |
+
for i, label in enumerate(labels):
|
29 |
+
response += f"{label}: {probs[i]*100:.1f}%<br>"
|
30 |
+
|
31 |
+
response += "<br>INFLUENTIAL KEYWORDS:<br>"
|
32 |
+
for keyword, score in keywords:
|
33 |
+
response += f"{keyword} (Score: {score:.2f})<br>"
|
34 |
+
|
35 |
+
return response
|
36 |
+
|
37 |
def predict(text):
|
38 |
greeting_pattern = r"^(Halló|Hæ|Sæl|Góðan dag|Kær kveðja|Daginn|Kvöldið|Ágætis|Elsku)"
|
39 |
|
40 |
+
prediction_output = get_prediction(text)
|
41 |
greeting_feedback = ""
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
if not re.match(greeting_pattern, text, re.IGNORECASE):
|
44 |
greeting_feedback = "Heilsaðu dóninn þinn<br>"
|
45 |
|
46 |
+
response = prediction_output + "<br>" + greeting_feedback
|
47 |
|
48 |
return response
|
49 |
|
|
|
50 |
description_html = """
|
51 |
<center>
|
52 |
<img src='http://www.ru.is/media/HR_logo_vinstri_transparent.png' width='250' height='auto'>
|