godlyjkrjjjcope commited on
Commit
e0b61f1
·
1 Parent(s): 7532447

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+
15
+ # # load image examples
16
+ # urls = [
17
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nfcb5.png',
18
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/p57fn.png',
19
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w2yp7.png',
20
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/pme86.png',
21
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/w4nfx.png',
22
+ # 'https://storage.googleapis.com/trocr-captcha.appspot.com/captcha_images_v2/nf8b8.png'
23
+ # ]
24
+ # for idx, url in enumerate(urls):
25
+ # image = Image.open(requests.get(url, stream=True).raw)
26
+ # image.save(f"image_{idx}.png")
27
+ def execit(command):
28
+ return os.system(command)
29
+ ###git add *.txt && git add *.py && git commit -m "lol" && git push
30
+ ###git add *.txt && git add *.py && git commit -m "lol" && git push
31
+ ###git add *.txt && git add *.py && git commit -m "lol" && git push
32
+ def process_image(image):
33
+ # prepare image
34
+ image_data = re.sub('^data:image/.+;base64,', '', image)
35
+ im = Image.open(BytesIO(base64.b64decode(image_data))).convert("RGB")
36
+
37
+ filter = ImageEnhance.Color(im)
38
+ im = filter.enhance(0)
39
+
40
+ # input_location = f"{random_string(9)}.png"
41
+ # outputfile_tmp = f"{random_string(9)}.png"
42
+ # outputfile_usable = f"{random_string(9)}.png"
43
+ # execit("input_location="+input_location)
44
+ # execit("outputfile_tmp="+outputfile_tmp)
45
+ # execit("outputfile_usable="+outputfile_usable)
46
+ # im.save(input_location, "png")
47
+ # 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''')
48
+ # execit('convert $outputfile_tmp -background white -alpha remove -alpha off $outputfile_usable')
49
+
50
+
51
+
52
+ #Take's the picture
53
+ pixel_values = processor(im, return_tensors="pt").pixel_values
54
+
55
+ # generate (no beam search)
56
+ generated_ids = model.generate(pixel_values)
57
+
58
+ # os.remove(input_location)
59
+ # os.remove(outputfile_tmp)
60
+ # os.remove(outputfile_usable)
61
+
62
+ # decode
63
+ generated_text = processor.batch_decode(generated_ids, skip_special_tokens=True)[0]
64
+
65
+ return generated_text
66
+
67
+ title = "8kun captcha solver 1 in 8"
68
+ description = "Due to events. in 8chan staff moderation. I am attacking it. The gamergate shitposting days are over. and so is 8chan."
69
+ # 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>"
70
+ # examples =[["image_0.png"], ["image_1.png"], ["image_2.png"], ["image_3.png"], ["image_4.png"], ["image_5.png"]]
71
+
72
+ #css = """.output_image, .input_image {height: 600px !important}"""
73
+
74
+ iface = gr.Interface(fn=process_image,
75
+ # inputs=gr.inputs.Image(type="pil"),
76
+ inputs=gr.Textbox(placeholder="base64 string (right-click => copy-link) ..."),
77
+ outputs=gr.outputs.Textbox(),
78
+
79
+ title=title,
80
+ description=description,
81
+ # article=article,
82
+ # examples=examples
83
+ )
84
+ iface.launch(debug=True)