diff --git a/nikud.py b/nikud.py index 0d7f0b2..16d6884 100644 --- a/nikud.py +++ b/nikud.py @@ -5,11 +5,14 @@ import requests 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 api_key = os.getenv("API_KEY") +font = ImageFont.truetype("fonts/BonaNova-Regular.ttf", 40) + def get_nakdan_response(hebrew_text, api_key): url = "https://nakdan-5-3.loadbalancer.dicta.org.il/addnikud" @@ -29,14 +32,6 @@ def get_nakdan_response(hebrew_text, api_key): return response.json() -# Create an image -img = Image.new('RGB', (300, 100), color=(255, 255, 255)) -d = ImageDraw.Draw(img) -ttfont = "fonts/xbmc-hebrew-fonts/Roboto-Bold-xbmc-il.ttf" -ttfont = "fonts/BonaNova-Regular.ttf" -font = ImageFont.truetype(ttfont, 10) - - def main(): st.title('Dicta Nakdan API Interface') @@ -46,17 +41,17 @@ def main(): # Button to send request if st.button("Process Text"): response = get_nakdan_response(hebrew_text, api_key) - # st.json(response) + st.json(response) # Extract words words = [ - option['w'] + option['w'].replace("|", "") for item in response['data'] if 'nakdan' in item for option in item['nakdan'].get('options', []) ] st.text(words) # Create an image - img = Image.new('RGB', (300, 100), color=(255, 255, 255)) + img = Image.new('RGB', (300, 400), color=(255, 255, 255)) d = ImageDraw.Draw(img) # Position for the first word @@ -64,10 +59,11 @@ def main(): # Write words on the image for word in words: - d.text((x, y), word, fill=(0, 0, 0), font=font) - y += 15 # Move to the next line + bidi_word = get_display(word) # Convert word to RTL format + d.text((x, y), bidi_word, fill=(0, 0, 0), font=font) + y += 45 # Move to the next line st.image(img) if __name__ == "__main__": - main() + main() \ No newline at end of file