import numpy
import requests
import string
import sys
import tensorflow
from io import BytesIO
from PIL import Image


def decode(li) -> string:
    available_chars = "abcdefghijklmnopqrstuvwxyz"
    result = []
    for char in li:
        result.append(available_chars[char])
    return "".join(result)


def solve(url: string) -> string:
    interpreter = tensorflow.lite.Interpreter(
        model_content=open("./model.tflite", "rb").read())
    u = requests.get(url)
    raw_data = u.content
    img = Image.open(BytesIO(raw_data))
    img = numpy.asarray(img)
    # normalize to [0...1]
    img = (img / 255).astype(numpy.float32)
    # convert to grayscale
    r, g, b = img[:, :, 0], img[:, :, 1], img[:, :, 2]
    gray = 0.299 * r + 0.587 * g + 0.114 * b
    # input has nowof  shape (70, 175)
    # we modify dimensions to match model's input
    gray = numpy.expand_dims(gray, 0)
    gray = numpy.expand_dims(gray, -1)
    # input is now of shape (batch_size, 70, 175, 1)
    # output will have shape (batch_size, 4, 26)
    interpreter.allocate_tensors()
    input_details = interpreter.get_input_details()
    output_details = interpreter.get_output_details()
    interpreter.set_tensor(input_details[0]['index'], gray)
    interpreter.invoke()
    # predict and get the output
    output = interpreter.get_tensor(output_details[0]['index'])
    # now get labels
    labels_indices = numpy.argmax(output, axis=2)
    decoded_label = [decode(x) for x in labels_indices][0]
    return decoded_label


try:
    result = solve(sys.argv[1])
    print(result, end = "")
except:
	pass