karalif commited on
Commit
c4a462b
·
verified ·
1 Parent(s): fde9527

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -32
app.py CHANGED
@@ -1,51 +1,46 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
  import re
 
4
 
5
- # Initialize the pipeline with your custom model
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
 
11
- greeting_feedback = ""
 
 
 
 
 
 
12
 
13
- # Splitting the input text into words
14
  words = text.split()
15
- # Highlighting the third word if it exists with a background color
16
  if len(words) >= 3:
17
- words[2] = f"<span style='background-color: yellow;'>{words[2]}</span>"
18
- highlighted_text = " ".join(words)
19
-
20
- # Adding the highlighted input text at the beginning of the response, followed by a new line
21
- response = f"Input: {highlighted_text}\n\n"
22
-
23
- results = text_pipe(text)
24
- all_scores = results[0]
25
-
26
- for result in all_scores:
27
- label = result['label']
28
- score = result['score']
29
- response += f"{label}: {score:.3f}\n"
30
 
31
- if not re.match(greeting_pattern, text, re.IGNORECASE):
32
- greeting_feedback = "\n- Heilsaðu dóninn þinn\n"
33
 
34
- response += greeting_feedback
35
-
36
- return response
37
-
38
- description_html = """
39
- <center>
40
- <img src='http://www.ru.is/media/HR_logo_vinstri_transparent.png' width='250' height='auto'>
41
- </center>
42
- """
43
 
 
44
  gr.Interface(
45
  fn=predict,
46
  inputs=gr.TextArea(label="Enter text here:"),
47
- outputs=gr.TextArea(label="Feedback on text input:"),
48
- description=description_html,
49
  examples=[
50
  ["Það voru vitni að árásinni sem tilkynntu málið til lögreglu sem kom skjótt á vettvang."],
51
  ["É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
+ 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
+ # Create an image with white background
10
+ image = Image.new("RGB", (800, 100), "white")
11
+ draw = ImageDraw.Draw(image)
12
 
13
+ # Define the font and size
14
+ try:
15
+ # Attempt to use a nicer font if available
16
+ font = ImageFont.truetype("arial.ttf", 24)
17
+ except IOError:
18
+ # Fallback to default font
19
+ font = ImageFont.load_default()
20
 
21
+ # Split the text to find the third word
22
  words = text.split()
 
23
  if len(words) >= 3:
24
+ # Highlight the third word with a different color
25
+ highlighted_text = " ".join(words[:2]) + " " + words[2] + " " + " ".join(words[3:])
26
+ draw.text((10, 10), highlighted_text, fill="black", font=font)
27
+ else:
28
+ # Draw the text as is if less than three words
29
+ draw.text((10, 10), text, fill="black", font=font)
 
 
 
 
 
 
 
30
 
31
+ return image
 
32
 
33
+ def predict(text):
34
+ # Your existing prediction logic here
35
+ # For demonstration, we'll just create an image with the highlighted third word
36
+ image = draw_text_with_highlight(text)
37
+ return image
 
 
 
 
38
 
39
+ # Update the Gradio interface to use the image output
40
  gr.Interface(
41
  fn=predict,
42
  inputs=gr.TextArea(label="Enter text here:"),
43
+ outputs="image", # Changed to image output
 
44
  examples=[
45
  ["Það voru vitni að árásinni sem tilkynntu málið til lögreglu sem kom skjótt á vettvang."],
46
  ["Ég held þetta sé ekki góður tími fara heimsókn."],