###This is a script to avoid having an other huggingface-space. Nor having too much javascript code which gonna make things not look okay in my head import gradio as gr from svglib.svglib import svg2rlg from reportlab.graphics import renderPM, renderPDF import requests as killpedos from io import BytesIO, StringIO from PIL import Image import base64, time, threading, random, string, json from pdf2image import convert_from_bytes def random_string(string_length): input = string.ascii_lowercase + string.digits return ''.join(random.choice(input) for i in range(string_length)) def downscale(captcha_image): half = 0.4 # Actually with captcha_image as im: captcha_image = captcha_image.resize( [int(half * s) for s in captcha_image.size] ) return captcha_image def hf_ai_captcha(image_data): image_data_bytes = BytesIO() image_data.save(image_data_bytes, format="PNG") # image_data_bytes.seek(0) base64_data = base64.b64encode(image_data_bytes.getvalue()).decode('utf-8') # base64_data = base64.b64encode(image_data).decode('utf-8') # print(base64_data) while True: ############################################ session_hash=random_string(12) json_data = { 'fn_index': 0, 'data': [ f"data:image/png;base64,{base64_data}" ], 'action': 'predict', 'session_hash': session_hash, } # json_data['data'][0] = f"data:image/png;base64,{base64_data}" # print(json_data) ############################################################################### project_path=random.choice(["sneedium/captcha_pixelplanet"]) r = killpedos.post(f'https://hf.space/embed/{ project_path }/api/predict/', headers={ 'User-Agent': "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.134 Safari/537.36", }, data=json.dumps(json_data)) captcha = r.json()['data'][0] return captcha# break#Breaks because, we got a session! #Breaks to "break the while-loop", therefore it will stop the function. and continue with the bot. def step1(svg_data): svg_data = StringIO(svg_data) # Makes a SVG drwawing drawing = svg2rlg(svg_data) sneed_buffer_pdf = ( BytesIO() ) ##Fake memory buffer, so we can kindda - NOT SAVE INTO HOUR COPE-COMPUTERS SNEED DOPE DILATE renderPDF.drawToFile(drawing, sneed_buffer_pdf) sneed_buffer_pdf.seek(0) cope = sneed_buffer_pdf.read() captcha_image = convert_from_bytes( cope, fmt="jpeg", # open('/home/belval/example.pdf', 'rb').read() )[0] return captcha_image def step2(captcha_image): bytes_captcha_images = BytesIO() captcha_image = downscale(captcha_image) captcha_image.save( bytes_captcha_images, quality=20, format="JPEG" ) # compress images a bit captcha_image = Image.open(bytes_captcha_images) # Reopens it in a bytes-manner return captcha_image def step3(captcha_image): return hf_ai_captcha(captcha_image) def greet(svg_data): captcha_image = step1(svg_data) captcha_image = step2(captcha_image) code = step3(captcha_image) return code # svg_data = """""" # print(greet(svg_data)) iface = gr.Interface(fn=greet, inputs="text", outputs="text") iface.launch()