This commit is contained in:
yair-mantis 2024-01-09 12:39:15 +02:00
parent 95ce964fac
commit bdef75118b

View File

@ -5,11 +5,14 @@ import requests
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os
from bidi.algorithm import get_display
# Load environment variables from .env file # Load environment variables from .env file
load_dotenv() load_dotenv()
# Read API key from .env file # Read API key from .env file
api_key = os.getenv("API_KEY") api_key = os.getenv("API_KEY")
font = ImageFont.truetype("fonts/BonaNova-Regular.ttf", 40)
def get_nakdan_response(hebrew_text, api_key): def get_nakdan_response(hebrew_text, api_key):
url = "https://nakdan-5-3.loadbalancer.dicta.org.il/addnikud" 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() 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(): def main():
st.title('Dicta Nakdan API Interface') st.title('Dicta Nakdan API Interface')
@ -46,17 +41,17 @@ def main():
# Button to send request # Button to send request
if st.button("Process Text"): if st.button("Process Text"):
response = get_nakdan_response(hebrew_text, api_key) response = get_nakdan_response(hebrew_text, api_key)
# st.json(response) st.json(response)
# Extract words # Extract words
words = [ words = [
option['w'] option['w'].replace("|", "")
for item in response['data'] for item in response['data']
if 'nakdan' in item if 'nakdan' in item
for option in item['nakdan'].get('options', []) for option in item['nakdan'].get('options', [])
] ]
st.text(words) st.text(words)
# Create an image # 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) d = ImageDraw.Draw(img)
# Position for the first word # Position for the first word
@ -64,10 +59,11 @@ def main():
# Write words on the image # Write words on the image
for word in words: for word in words:
d.text((x, y), word, fill=(0, 0, 0), font=font) bidi_word = get_display(word) # Convert word to RTL format
y += 15 # Move to the next line d.text((x, y), bidi_word, fill=(0, 0, 0), font=font)
y += 45 # Move to the next line
st.image(img) st.image(img)
if __name__ == "__main__": if __name__ == "__main__":
main() main()