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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -14
app.py CHANGED
@@ -6,28 +6,36 @@ from PIL import Image, ImageDraw, ImageFont
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):
 
6
  text_pipe = pipeline("text-classification", model="karalif/myTestModel", return_all_scores=True)
7
 
8
  def draw_text_with_highlight(text):
9
+ # Assuming a comprehensive font file is in the same directory as the script
10
+ font_path = "path/to/your/font.ttf" # Update this to the path of your font file
11
+ font_size = 24 # Adjust font size as needed
12
+
13
  # Create an image with white background
14
+ image = Image.new("RGB", (1000, 60), "white")
15
  draw = ImageDraw.Draw(image)
16
+ font = ImageFont.truetype(font_path, font_size)
17
+
 
 
 
 
 
 
 
18
  # Split the text to find the third word
19
  words = text.split()
20
+
21
+ # Initialize starting position
22
+ x, y = 10, 10
23
  if len(words) >= 3:
24
+ # Process each word to either draw normally or highlight
25
+ for i, word in enumerate(words):
26
+ if i == 2: # The third word is at index 2
27
+ # Draw background for highlight
28
+ width, height = draw.textsize(word, font=font)
29
+ draw.rectangle((x, y, x + width, y + height), fill='yellow')
30
+ draw.text((x, y), word, fill="black", font=font)
31
+ else:
32
+ draw.text((x, y), word, fill="black", font=font)
33
+ # Update x to draw next word. Adding a space width.
34
+ x += draw.textsize(word + " ", font=font)[0]
35
  else:
36
  # Draw the text as is if less than three words
37
  draw.text((10, 10), text, fill="black", font=font)
38
+
39
  return image
40
 
41
  def predict(text):