Spaces:
Runtime error
Runtime error
| import onnxruntime as onr | |
| import numpy as np | |
| import gradio as gr | |
| import glob, io | |
| from PIL import Image | |
| from cairosvg import svg2png | |
| #----- CONFIG ------ | |
| img_height = 300 | |
| img_width = 500 | |
| max_length = 4 | |
| characters = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', | |
| 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', | |
| 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', | |
| 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', | |
| 'y', 'z' | |
| ] | |
| model_path = 'model.onnx' | |
| #===== some code init ===== | |
| Model = onr.InferenceSession(model_path) | |
| #===== some funcs ===== | |
| def get_result(pred): | |
| accuracy = 1 | |
| last = None | |
| ans = [] | |
| for item in pred[0]: | |
| char_ind = item.argmax() | |
| if char_ind != last and char_ind != 0 and char_ind != len(characters) + 1: | |
| ans.append(characters[char_ind - 1]) | |
| accuracy *= item[char_ind] | |
| last = char_ind | |
| answ = "".join(ans)[:max_length] | |
| return answ, accuracy | |
| def predict(svgdata): | |
| img = Image.open(io.BytesIO(svg2png(svgdata))) | |
| img = img.convert('L') | |
| img = img.resize((img_width, img_height)) | |
| img = np.array(img) | |
| img = np.expand_dims(img, axis=1) | |
| img = np.expand_dims(img, axis=-1) | |
| img = img.transpose([1,2,0,3]) | |
| img = img.astype(np.float32) / 255. | |
| result_tensor = Model.run(None, {'image': img, 'label': np.random.default_rng().random((28, 28), dtype=np.float32)})[0] | |
| return (get_result(result_tensor)[0]) | |
| title = "Made with love❤️❤️❤️❤️\nby Vermei (for userscript)" | |
| description = "thanks to HF for allowing captcha solve userscirpts (code 2)" | |
| iface = gr.Interface(fn=predict, | |
| inputs=gr.inputs.Textbox(), | |
| outputs=gr.outputs.Textbox(), | |
| title=title, | |
| #examples=glob.glob('examples/*.png'), | |
| description=description) | |
| iface.launch() |