nikud api test

This commit is contained in:
yair-mantis 2024-01-09 11:55:52 +02:00
parent 8a2970a09b
commit fff7f81e38
3 changed files with 71 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
/venv
/cats_and_dogs_filtered
log.log
.env
output.png

BIN
fonts/BonaNova-Regular.ttf Normal file

Binary file not shown.

69
nikud.py Normal file
View File

@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
import streamlit as st
import requests
from PIL import Image, ImageDraw, ImageFont
from dotenv import load_dotenv
import os
# Load environment variables from .env file
load_dotenv()
# Read API key from .env file
api_key = os.getenv("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'}
params = {
"task": "nakdan",
"useTokenization": True,
"genre": "modern", # or "rabbinic" or "premodern" based on user's need
"data": hebrew_text,
"addmorph": True,
"matchpartial": True,
"keepmetagim": False,
"keepqq": False,
"apiKey": api_key
}
response = requests.post(url, headers=headers, json=params)
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')
# Input fields for user
hebrew_text = st.text_area("Enter Hebrew Text:", "טקסט טקסט טקסט")
# Button to send request
if st.button("Process Text"):
response = get_nakdan_response(hebrew_text, api_key)
# st.json(response)
# Extract words
words = [option['w'] 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))
d = ImageDraw.Draw(img)
# Position for the first word
x, y = 10, 40
# 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
st.image(img)
if __name__ == "__main__":
main()