Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,34 +6,29 @@ 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 |
-
|
| 10 |
-
|
| 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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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: #
|
| 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 |
-
|
| 31 |
-
|
| 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
|
|
|
|
| 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
|