About

source

Webring

This site participates in a webring, you can find more sites here, and read join it here

Go to the next site in the ring

Go to the previous site in the ring

Information about the content on this site

Big Disclaimer

This website was made as a personal code exercise in Python. It is not politically or otherwise motivated. All information and posts created on this website are fake, and generated by AI/Machine Learning--as such, have no bearing on reality. With that said, here's how it's made. Once again, the information and text on the site is COMPLETELY FAKE and should not be read by anybody, or if you're going to read it, just be prepared for some nonsense.

The technology

The motivation

A learning experience, touching a few bits of tech I haven't really dealt with.

How it works

Firstly, the application grabs an RSS feed, and collates the titles, then randomly selects one.

def loadTitle(get_args):
    feed = feedparser.parse(get_args.feed)
    with open('tmp/feeds', 'w') as f:
        for item in feed.entries:
            print(item[ "title" ], file=f)

    Title = random.choice(list(open('tmp/feeds')))
    return Title

Then it hits the Pexels API and selects a result from an image query, using the default category flag, or a provided one.

def getImage(get_args):
    # Setup requests.get resiliancy
    retry_strategy = Retry(
        total=3,
        status_forcelist=[429, 500, 502, 503, 504],
        allowed_methods=["HEAD", "GET", "OPTIONS"]
    )
    adapter = HTTPAdapter(max_retries=retry_strategy)
    http = requests.Session()
    http.mount("https://", adapter)
    http.mount("http://", adapter)

    # Connect PEXELS API
    api = API(PEXELS_API_KEY)
    api.search(get_args.category, page=random.randint(0,20), results_per_page=1)
    photos = api.get_entries()
    # Grab landscape image
    for photo in photos:
        response = http.get(photo.landscape, allow_redirects=True, timeout=10)
        open('tmp/temp_img.png', 'wb').write(response.content)

Then we move onto text generation

def createGPT2Text(loadTitle):
    # GPT2 Stuff
    tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
    GPT2 = TFGPT2LMHeadModel.from_pretrained("gpt2", pad_token_id=tokenizer.eos_token_id)
    # Input is random title from above, generate text using tensorflow
    input_sequence = loadTitle
    input_ids = tokenizer.encode(input_sequence, return_tensors='tf')
    sample_output = GPT2.generate(
                             input_ids,
                             do_sample = True,
                             max_length = GPT_MAX_LEN,
                             top_k = 0,
                             temperature = 0.8
    )
    # Output here
    GPT2Output = tokenizer.decode(sample_output[0], skip_special_tokens = True)

    return GPT2Output

Taking a title from the list of titles grabbed earlier, is the basis or start of the text generation. Ensures its mostly different every time it's ran.

Finally, we construct the email, this section does a few things, gets all the different elements of the email sorted, and ready to attach, and then actually sends it all using the smtplib Python library

# Function to construct the email, attach the article and image, and then send it to
# post to email. Has a few hard coded shortcodes that could be ripped out for userargs
# and sensible defaults.
def constructAndSendEmail(get_args,loadTitle,createGPT2Text):
    # Basic email properties
    message = MIMEMultipart("alternative")
    message["Subject"] = loadTitle
    message["From"] = EMAIL_SENDER
    message["To"] = EMAIL_RECEIVER

    # Create the plain-text version of the message
    text = (createGPT2Text + f"""\n\n
            [tags post, Daily, News, Fake, {get_args.category}] [category {get_args.category}]
            ⚠ ALL TEXT IN THIS POST IS COMPLETELY FAKE AND AI GENERATED ⚠\n\n<a href="about-us">Read more about how it's done here.</a>\n\n
            """)

    # Get the image and create it
    fp = open('tmp/temp_img.png', 'rb')
    msgImage = MIMEImage(fp.read())
    fp.close()
    msgImage.add_header('Content-ID', '<image1>')

    # Turn these into plain/html MIMEText objects
    messageText = MIMEText(text, "plain")

    # Add HTML/plain-text parts to MIMEMultipart message
    # The email client will try to render the last part first
    message.attach(messageText)
    message.attach(msgImage)

    # Create secure connection with server and send email
    context = ssl.create_default_context()
    with smtplib.SMTP(EMAIL_SMTP_SERVER, EMAIL_SMTP_PORT) as server:
            server.starttls(context=context)
            server.login(EMAIL_SENDER, EMAIL_PASSWORD)
            server.sendmail(
                EMAIL_SENDER, EMAIL_RECEIVER, message.as_string()
            )

Then we combine it all together and run it, the application can be ran on its own with some sane defaults, or you can feed in any Category or RSS feed and it'll just make an article from what is provided.

The full source code and installation steps can be found on GitHub here.

I'm not exactly a great Python dev, and this was kind of the first real thing I've made so my format, structure and types are probably going to make the rusted Python developers recoil in disgust, however the application functions, so that's something.

C