sdfswefrwerwe commited on
Commit
3e0bc32
1 Parent(s): 6a3902b
Files changed (3) hide show
  1. app.py +81 -0
  2. packages.txt +2 -0
  3. requirements.txt +5 -0
app.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import TrOCRProcessor, VisionEncoderDecoderModel
3
+ import requests, re, base64, string, random
4
+ from PIL import Image, ImageEnhance
5
+ from io import BytesIO
6
+ import os
7
+ processor = TrOCRProcessor.from_pretrained("microsoft/trocr-small-printed")
8
+ model = VisionEncoderDecoderModel.from_pretrained("jonahgoldberg/bk_wht_8kun")
9
+
10
+ def random_string(string_length):
11
+ input = string.ascii_lowercase + string.digits
12
+ return ''.join(random.choice(input) for i in range(string_length))
13
+
14
+ # # load image examples
15
+ # urls = [
16
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nfcb5.png',
17
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/p57fn.png',
18
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w2yp7.png',
19
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/pme86.png',
20
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w4nfx.png',
21
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nf8b8.png'
22
+ # ]
23
+ # for idx, url in enumerate(urls):
24
+ # image = Image.open(requests.get(url, stream=True).raw)
25
+ # image.save(f"image_{idx}.png")
26
+ def execit(command):
27
+ return os.system(command)
28
+ ###git add *.txt && git add *.py && git commit -m "lol" && git push
29
+ def process_image(image):
30
+ # prepare image
31
+ image_data = re.sub('^data:image/.+;base64,', '', image)
32
+ im = Image.open(BytesIO(base64.b64decode(image_data))).convert("RGB")
33
+
34
+ filter = ImageEnhance.Color(im)
35
+ im = filter.enhance(0)
36
+
37
+ # input_location = f"{random_string(9)}.png"
38
+ # outputfile_tmp = f"{random_string(9)}.png"
39
+ # outputfile_usable = f"{random_string(9)}.png"
40
+ # execit("input_location="+input_location)
41
+ # execit("outputfile_tmp="+outputfile_tmp)
42
+ # execit("outputfile_usable="+outputfile_usable)
43
+ # im.save(input_location, "png")
44
+ # execit('''gegl -x "<?xml version='1.0' encoding='UTF-8'?> <gegl> <node operation='gegl:ripple'> <params> <param name='amplitude'>9.9</param> <param name='period'>125.0</param> <param name='sampler-type'>nearest</param> <param name='abyss-policy'>none</param> </params> </node> <node operation='gegl:brightness-contrast'> <params> <param name='contrast'>5</param> <param name='brightness'>-1.0</param> </params> </node> <node operation='gegl:c2g'/> <node operation='gegl:load'> <params> <param name='path'>"$input_location"</param> </params> </node> </gegl>" -o $outputfile_tmp''')
45
+ # execit('convert $outputfile_tmp -background white -alpha remove -alpha off $outputfile_usable')
46
+
47
+
48
+
49
+ #Take's the picture
50
+ pixel_values = processor(im, return_tensors="pt").pixel_values
51
+
52
+ # generate (no beam search)
53
+ generated_ids = model.generate(pixel_values)
54
+
55
+ # os.remove(input_location)
56
+ # os.remove(outputfile_tmp)
57
+ # os.remove(outputfile_usable)
58
+
59
+ # decode
60
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
61
+
62
+ return generated_text
63
+
64
+ title = "8kun captcha solver 1 in 8"
65
+ description = "Due to events. in 8chan staff moderation. I am attacking it. The gamergate shitposting days are over. and so is 8chan."
66
+ # article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.10282'>TrOCR: Transformer-based Optical Character Recognition with Pre-trained Models</a> | <a href='https://github.com/microsoft/unilm/tree/master/trocr'>Github Repo</a></p>"
67
+ # examples =[["image_0.png"], ["image_1.png"], ["image_2.png"], ["image_3.png"], ["image_4.png"], ["image_5.png"]]
68
+
69
+ #css = """.output_image, .input_image {height: 600px !important}"""
70
+
71
+ iface = gr.Interface(fn=process_image,
72
+ # inputs=gr.inputs.Image(type="pil"),
73
+ inputs=gr.Textbox(placeholder="base64 string (right-click => copy-link) ..."),
74
+ outputs=gr.outputs.Textbox(),
75
+
76
+ title=title,
77
+ description=description,
78
+ # article=article,
79
+ # examples=examples
80
+ )
81
+ iface.launch(debug=True)
packages.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ gegl
2
+ imagemagick
requirements.txt ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ gradio
2
+ torch
3
+ Pillow
4
+ sentencepiece
5
+ git+https://github.com/huggingface/transformers.git