karalif commited on
Commit
52094ea
·
verified ·
1 Parent(s): a0bb172

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -34
app.py CHANGED
@@ -1,49 +1,43 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  import re
4
- from PIL import Image, ImageDraw, ImageFont
5
 
6
  text_pipe = pipeline("text-classification", model="karalif/myTestModel", return_all_scores=True)
7
 
8
- def draw_text_with_highlight(text):
9
- font_path = "path/to/your/font.ttf" # Make sure this path is correct
10
- font_size = 24
11
-
12
- image = Image.new("RGB", (1000, 60), "white")
13
- draw = ImageDraw.Draw(image)
14
-
15
- # Check if the font file exists and fallback to default if not
16
- try:
17
- font = ImageFont.truetype(font_path, font_size)
18
- except IOError:
19
- print(f"Font file not found: {font_path}. Falling back to default font.")
20
- font = ImageFont.load_default()
21
-
22
- words = text.split()
23
- x, y = 10, 10
24
- if len(words) >= 3:
25
- for i, word in enumerate(words):
26
- if i == 2: # Highlight the third word
27
- width, height = draw.textsize(word, font=font)
28
- draw.rectangle((x, y, x + width, y + height), fill='yellow')
29
- draw.text((x, y), word, fill="black", font=font)
30
- x += draw.textsize(word + " ", font=font)[0] # Update x position
31
- else:
32
- draw.text((10, 10), text, fill="black", font=font)
33
-
34
- return image
35
 
36
  def predict(text):
37
- # Your existing prediction logic here
38
- # For demonstration, we'll just create an image with the highlighted third word
39
- image = draw_text_with_highlight(text)
40
- return image
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- # Update the Gradio interface to use the image output
43
  gr.Interface(
44
  fn=predict,
45
  inputs=gr.TextArea(label="Enter text here:"),
46
- outputs="image", # Changed to image output
 
 
 
 
 
 
 
47
  examples=[
48
  ["Það voru vitni að árásinni sem tilkynntu málið til lögreglu sem kom skjótt á vettvang."],
49
  ["Ég held þetta sé ekki góður tími fara heimsókn."],
 
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 mark_text(text, tag):
8
+ """Helper for marking the text, returns a tuple with the text and the tag"""
9
+ return (text, tag)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  def predict(text):
12
+ greeting_pattern = r"^(Halló|Hæ|Sæl|Góðan dag|Kær kveðja|Daginn|Kvöldið|Ágæt|Elsku)"
13
+ greeting_feedback = "Heilsaðu dóninn þinn"
14
+
15
+ results = text_pipe(text)
16
+ all_scores = results[0]
17
+ response = [mark_text(text, None)] # Original text without any tag
18
+
19
+ if not re.match(greeting_pattern, text, re.IGNORECASE):
20
+ response.append(mark_text(greeting_feedback, "breytt")) # Appending feedback with a tag
21
+
22
+ return response
23
+
24
+ description_html = """
25
+ <center>
26
+ <img src='http://www.ru.is/media/HR_logo_vinstri_transparent.png' width='250' height='auto'>
27
+ </center>
28
+ """
29
 
 
30
  gr.Interface(
31
  fn=predict,
32
  inputs=gr.TextArea(label="Enter text here:"),
33
+ outputs=gr.HighlightedText(
34
+ label="Feedback on text input:",
35
+ show_label=False,
36
+ show_legend=True,
37
+ combine_adjacent=True,
38
+ adjacent_separator=" ",
39
+ ).style(color_map={"breytt": "red"}), # "breytt" tagged text will be red
40
+ description=description_html,
41
  examples=[
42
  ["Það voru vitni að árásinni sem tilkynntu málið til lögreglu sem kom skjótt á vettvang."],
43
  ["Ég held þetta sé ekki góður tími fara heimsókn."],