karalif commited on
Commit
cafaa0d
·
verified ·
1 Parent(s): 605fefb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -40
app.py CHANGED
@@ -1,66 +1,34 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import re
4
- from difflib import SequenceMatcher
5
 
6
  text_pipe = pipeline("text-classification", model="karalif/myTestModel", return_all_scores=True)
7
 
8
  def predict(text):
9
  greeting_pattern = r"^(Halló|Hæ|Sæl|Góða|Kær|Daginn|Kvöldið|Ágæt|Elsku)"
 
10
  greeting_feedback = ""
11
 
12
  results = text_pipe(text)
13
  all_scores = results[0]
14
  response = ""
15
-
16
- # Helper function to mark text with a specific tag
17
- def mark_text(text, tag):
18
- return (text, tag)
19
-
20
- # Helper function to mark spans of text with a specific tag
21
- def mark_span(text, tag):
22
- return [mark_text(token, tag) for token in text]
23
-
24
- # Helper function to markup differences between two texts
25
- def markup_diff(a, b, mark=mark_span, default_mark=lambda x: x, isjunk=None):
26
- """Returns a and b with any differences processed by mark
27
- Junk is ignored by the differ
28
- """
29
- seqmatcher = SequenceMatcher(isjunk=isjunk, a=a, b=b, autojunk=False)
30
- out_a, out_b = [], []
31
- for tag, a0, a1, b0, b1 in seqmatcher.get_opcodes():
32
- markup = mark
33
- out_a += markup(a[a0:a1], tag)
34
- out_b += markup(b[b0:b1], tag)
35
- return out_a, out_b
36
-
37
- # Generating scores and markup
38
  for result in all_scores:
39
  label = result['label']
40
  score = result['score']
41
  response += f"{label}: {score:.3f}\n"
42
-
43
- # Apply markup to class names only
44
- original_text = text.split()
45
- processed_text = response.split()
46
- markup_response, _ = markup_diff(original_text, processed_text, mark=lambda x, tag: mark_text(x, tag) if x.lower() in ['politeness', 'toxicity', 'sentiment', 'formality'] else x)
47
-
48
- # Convert marked text back to string
49
- marked_response = " ".join([token[0] for token in markup_response])
50
-
51
- # Check if the input text matches the greeting pattern
52
  if not re.match(greeting_pattern, text, re.IGNORECASE):
53
  greeting_feedback = "\n- Heilsaðu dóninn þinn\n"
54
 
55
- # Add greeting feedback and return the marked response
56
- marked_response += greeting_feedback
57
- return marked_response
58
 
59
  description_html = """
60
  <center>
61
  <img src='http://www.ru.is/media/HR_logo_vinstri_transparent.png' width='250' height='auto'>
62
  </center>
63
- """
64
 
65
  gr.Interface(
66
  fn=predict,
@@ -74,4 +42,4 @@ gr.Interface(
74
  ["Hver á þenan bússtað? já eða nei."],
75
  ["Hafi þau svo látið gólfið þorna vel og síðan flotað það til lagfæringar eftir motturnar."],
76
  ],
77
- ).launch()
 
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óða|Kær|Daginn|Kvöldið|Ágæt|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
  response += f"{label}: {score:.3f}\n"
19
+
 
 
 
 
 
 
 
 
 
20
  if not re.match(greeting_pattern, text, re.IGNORECASE):
21
  greeting_feedback = "\n- Heilsaðu dóninn þinn\n"
22
 
23
+ response += greeting_feedback
24
+
25
+ return response
26
 
27
  description_html = """
28
  <center>
29
  <img src='http://www.ru.is/media/HR_logo_vinstri_transparent.png' width='250' height='auto'>
30
  </center>
31
+ """
32
 
33
  gr.Interface(
34
  fn=predict,
 
42
  ["Hver á þenan bússtað? já eða nei."],
43
  ["Hafi þau svo látið gólfið þorna vel og síðan flotað það til lagfæringar eftir motturnar."],
44
  ],
45
+ ).launch()