RTL works
This commit is contained in:
parent
be0eebd98e
commit
671238b49e
BIN
fonts/DejaVuSans.ttf
Normal file
BIN
fonts/DejaVuSans.ttf
Normal file
Binary file not shown.
55
nikud.py
55
nikud.py
|
@ -6,6 +6,7 @@ from PIL import Image, ImageDraw, ImageFont
|
|||
from dotenv import load_dotenv
|
||||
import os
|
||||
from bidi.algorithm import get_display
|
||||
|
||||
# Load environment variables from .env file
|
||||
load_dotenv()
|
||||
# Read API key from .env file
|
||||
|
@ -13,9 +14,12 @@ api_key = os.getenv("API_KEY")
|
|||
|
||||
# Specify the font size
|
||||
font_size = 40
|
||||
font = ImageFont.truetype("fonts/BonaNova-Regular.ttf", font_size)
|
||||
# font = ImageFont.truetype("fonts/BonaNova-Regular.ttf", font_size, encoding='unic')
|
||||
font = ImageFont.truetype("fonts/DejaVuSans.ttf", font_size) # only one that works
|
||||
|
||||
|
||||
# place the api_key in the .env file
|
||||
# such as API_KEY=your_api_key
|
||||
def get_nakdan_response(hebrew_text, api_key):
|
||||
url = "https://nakdan-5-3.loadbalancer.dicta.org.il/addnikud"
|
||||
headers = {'Content-Type': 'text/plain;charset=utf-8'}
|
||||
|
@ -30,8 +34,19 @@ def get_nakdan_response(hebrew_text, api_key):
|
|||
"keepqq": False,
|
||||
"apiKey": api_key
|
||||
}
|
||||
response = requests.post(url, headers=headers, json=params)
|
||||
return response.json()
|
||||
try:
|
||||
response = requests.post(url, headers=headers, json=params)
|
||||
response.raise_for_status() # Raises stored HTTPError, if one occurred.
|
||||
except requests.exceptions.HTTPError as errh:
|
||||
print("Http Error:", errh)
|
||||
except requests.exceptions.ConnectionError as errc:
|
||||
print("Error Connecting:", errc)
|
||||
except requests.exceptions.Timeout as errt:
|
||||
print("Timeout Error:", errt)
|
||||
except requests.exceptions.RequestException as err:
|
||||
print("Something went wrong", err)
|
||||
else:
|
||||
return response.json()
|
||||
|
||||
|
||||
def main():
|
||||
|
@ -43,6 +58,8 @@ def main():
|
|||
# Button to send request
|
||||
if st.button("Process Text"):
|
||||
response = get_nakdan_response(hebrew_text, api_key)
|
||||
if not isinstance(response, dict):
|
||||
response = {'data': []}
|
||||
# st.json(response)
|
||||
# Extract words
|
||||
words = [
|
||||
|
@ -51,7 +68,7 @@ def main():
|
|||
if 'nakdan' in item
|
||||
for option in item['nakdan'].get('options', [])
|
||||
]
|
||||
st.text(" ".join(words)) # Print words after each other
|
||||
st.text(" ".join(words)) # printwords after each other
|
||||
|
||||
# Create an image
|
||||
img_width = 696
|
||||
|
@ -62,11 +79,35 @@ def main():
|
|||
# Position for the first word
|
||||
x, y = 10, 0
|
||||
|
||||
max_width = img_width - 20 # Maximum width for each line
|
||||
line_width = 0 # Current line width
|
||||
|
||||
for word in words:
|
||||
bidi_word = get_display(word) # Convert word to RTL format
|
||||
d.text((x, y), bidi_word, fill=(0, 0, 0), font=font)
|
||||
st.text(f"Word: '{bidi_word}', Position: (x={x}, y={y}), {len(bidi_word)} characters")
|
||||
y += 40 # Move to the next line
|
||||
word_width = font.getbbox(bidi_word)[2] - font.getbbox(bidi_word)[0] # Width of the word
|
||||
|
||||
# Check if the word fits in the current line
|
||||
if line_width + word_width <= img_width - 20:
|
||||
# Render the word in the current line
|
||||
d.text((img_width - (x + line_width + word_width), y), bidi_word, fill=(0, 0, 0), font=font)
|
||||
line_width += word_width + 10 # Add word width and spacing
|
||||
else:
|
||||
# Move to the next line
|
||||
y += 40 # Move to the next line
|
||||
line_width = 0 # Reset line width
|
||||
|
||||
# Check if the word fits in the new line
|
||||
if word_width <= img_width - 20:
|
||||
# Render the word in the new line
|
||||
d.text((img_width - (x + line_width + word_width), y), bidi_word, fill=(0, 0, 0), font=font)
|
||||
line_width += word_width + 10 # Add word width and spacing
|
||||
else:
|
||||
# Word is too long for a single line, truncate it
|
||||
truncated_word = bidi_word[:max_width // font_size] + "..."
|
||||
d.text((img_width - (x + line_width + font.getsize(truncated_word)[0]), y), truncated_word, fill=(0, 0, 0), font=font)
|
||||
line_width += font.getsize(truncated_word)[0] + 10 # Add truncated word width and spacing
|
||||
|
||||
# st.text(f"Word: '{bidi_word}', Position: (x={x + line_width - word_width}, y={y}), {len(bidi_word)} characters")
|
||||
|
||||
st.image(img)
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user