MLearning.ai
Published in

MLearning.ai

Generate Tweets with AI

Introducing your new creative publisher

Image generated with OpenAI’s DALL·E

Story outline

GPT and OpenAI

import openai

openai.api_key = <YOUR-API-KEY>
response = openai.Completion.create(
engine=<MODEL-NAME>,
prompt=<YOUR-TEXT-PROMPT>,
max_tokens=<MAXIMUM-TEXT-LENGTH>,
temperature=<RANDOMNESS-BETWEEN-0-and-1>,
)
generated_text = response["choices"][0]["text"]

engine

prompt

max_tokens

temperature

Hello World (the first Tweet)

Write a Tweet about {topic} in less than 120 characters:
response = openai.Completion.create(
engine="text-davinci-003",
prompt="Write a Tweet about AI in less than 120 characters:\n",
max_tokens=50,
temperature=0.9,
)
generated_text = response["choices"][0]["text"]

In the mood

Write a worried Tweet about AI in less than 120 characters:
Write an enthusiastic Tweet about AI in less than 120 characters:

Imitate styles

Write a {mood} Tweet about {topic} in less than 120 characters
and in the style of the following Tweets:\n\n{tweets}\n\n
import tweepy

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
response = api.user_timeline(
screen_name=account,
tweet_mode="extended", # returns full text
)
tweets = [r.full_text for r in response]

Hosted by Streamlit

Demo of tweets.streamlit.app

Conclusion

BONUS: Image generation

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store