karalif commited on
Commit
8e43439
·
verified ·
1 Parent(s): 74fadfe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -64
app.py CHANGED
@@ -1,47 +1,8 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
- import tokenizer
4
- from difflib import Differ, SequenceMatcher
5
-
6
-
7
- title = "<center><img src='https://huggingface.co/spaces/mideind/README/resolve/main/logo-mideind.png' width='250'></center>Sjálfvirk textaleiðrétting"
8
- description = "Hér má leiðrétta íslenskan texta með hjálp tauganets. Netið reynir að „þýða“ textann yfir í læsilegra mál."
9
- article = "<h3>Leiðbeiningar</h3>Hægt er að setja inn eina eða fleiri málsgreinar, en bíða þarf lengur eftir leiðréttingu ef textinn er langur. Ekki er gert ráð fyrir stökum málsgreinum sem eru lengri en u.þ.b. 500 stafir. \n\nÞetta er prufuútgáfa sem hefur ekki lært að leiðrétta öll þau atriði sem upp geta komið í texta og er í stöðugri þróun. \nVerkefnið er unnið hjá [Miðeind](https://mideind.is) sem hluti af máltækniáætlun stjórnvalda. Það er þjálfað á bæði tilbúnum gögnum og textum úr villumálheildum sem safnað var hjá Háskóla Íslands."
10
-
11
- translator = pipeline(
12
- "translation", model="mideind/yfirlestur-icelandic-correction-byt5", max_length=512
13
- )
14
-
15
-
16
- def mark_text(text, tag):
17
- """Helper for the diff method, returns a tuple with the text and the tag"""
18
- return (text, tag)
19
-
20
-
21
- def mark_span(text, tag):
22
- """Helper for the diff method, returns the span as a list of (text, tag) tuples"""
23
- return [mark_text(token, tag) for token in text]
24
-
25
-
26
- def markup_diff(a, b, mark=mark_span, isjunk=None):
27
- """Obtains the diff for the sentence along with the opcodes (tags). Returns the corrected sentence tokens along with their correction tag (in Icelandic)"""
28
- seqmatcher = SequenceMatcher(isjunk=isjunk, a=a, b=b, autojunk=False)
29
- out_sentence_tokens = []
30
- # renaming tags
31
- tags = {"equal": None, "delete": "breytt", "replace": "breytt", "insert": "breytt"}
32
-
33
- for tag, a0, a1, b0, b1 in seqmatcher.get_opcodes():
34
- if tag == "delete":
35
- out_sentence_tokens += mark(" ", tags[tag])
36
- out_sentence_tokens += mark(b[b0:b1], tags[tag])
37
- return out_sentence_tokens
38
-
39
-
40
- def split_text(text):
41
- sentence_list = [i for i in tokenizer.split_into_sentences(text, original=True)]
42
- return sentence_list
43
-
44
 
 
45
 
46
  def predict(text):
47
  greeting_pattern = r"^(Halló|Hæ|Sæl|Góða|Kær|Daginn|Kvöldið|Ágæt|Elsku)"
@@ -54,34 +15,32 @@ def predict(text):
54
  for result in all_scores:
55
  label = result['label']
56
  score = result['score']
57
- # Wrap each line of response in HTML span with green color
58
  response += f"<span style='color:green;'>{label}: {score:.3f}</span><br>"
59
 
60
  if not re.match(greeting_pattern, text, re.IGNORECASE):
61
- greeting_feedback = "<br><span style='color:green;'>- Heilsaðu dóninn þinn</span>"
62
-
63
  response += greeting_feedback
64
 
65
  return response
66
-
67
- outputs=[
68
- gr.HighlightedText(
69
- label="Leiðrétt",
70
- show_label=False,
71
- show_legend=True,
72
- combine_adjacent=True,
73
- adjacent_separator=" ",
74
- ).style(color_map={"breytt": "green"})
75
- ],
76
- title=title,
77
- description=description,
78
- article=article,
79
  examples=[
80
- ["Kvitu fiðrildinn fljua firir utan gluggan."],
81
- ["Ég held þetta er ekki góður tími fara heimsókn."],
82
- ["Mer hlakar til jólana"],
83
- ["Kver a þenan bússtað ja eða nei"],
84
- ["Módernisma gætir í íslenkum prósaverkum fljótlega eftir Fyrri heimstyrjöld"],
85
  ],
86
- )
87
- demo.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)"
 
15
  for result in all_scores:
16
  label = result['label']
17
  score = result['score']
18
+ # Wrap output in HTML for green text
19
  response += f"<span style='color:green;'>{label}: {score:.3f}</span><br>"
20
 
21
  if not re.match(greeting_pattern, text, re.IGNORECASE):
22
+ greeting_feedback = "<span style='color:green;'>\n- Heilsaðu dóninn þinn\n</span>"
23
+
24
  response += greeting_feedback
25
 
26
  return response
27
+
28
+ description_html = """
29
+ <center>
30
+ <img src='http://www.ru.is/media/HR_logo_vinstri_transparent.png' width='250' height='auto'>
31
+ </center>
32
+ """
33
+
34
+ gr.Interface(
35
+ fn=predict,
36
+ inputs=gr.TextArea(label="Enter text here:"),
37
+ outputs=gr.HTML(label="Feedback on text input:"),
38
+ description=description_html,
 
39
  examples=[
40
+ ["Það voru vitni árásinni sem tilkynntu málið til lögreglu sem kom skjótt á vettvang."],
41
+ ["Ég held þetta ekki góður tími fara heimsókn."],
42
+ ["Sæl og blessuð Kristín, hvað er að frella af þér gamla??"],
43
+ ["Hver á þenan bússtað? eða nei."],
44
+ ["Hafi þau svo látið gólfið þorna vel og síðan flotað það til lagfæringar eftir motturnar."],
45
  ],
46
+ ).launch()