Files
awesome-notebooks/OpenAI/OpenAI_Write_a_social_media_post.ipynb
2024-02-26 15:43:08 +00:00

337 lines
9.9 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "0d72eeb7-f77f-49e6-ab11-5b367329255e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"8%\" alt=\"OpenAI.png\" src=\"https://raw.githubusercontent.com/jupyter-naas/awesome-notebooks/master/.github/assets/logos/OpenAI.png\" style=\"border-radius: 15%\">"
]
},
{
"cell_type": "markdown",
"id": "67dd7282-24aa-4c80-aa3b-de71cf270309",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# OpenAI - Write a social media post\n",
"<a href=\"https://bit.ly/3JyWIk6\">Give Feedback</a> | <a href=\"https://github.com/jupyter-naas/awesome-notebooks/issues/new?assignees=&labels=bug&template=bug_report.md&title=OpenAI+-+Write+a+social+media+post:+Error+short+description\">Bug report</a>"
]
},
{
"cell_type": "markdown",
"id": "752505be-92fd-4a6d-9799-ec8458090158",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #openai #socialmedia #post #prompt #tone #platform"
]
},
{
"cell_type": "markdown",
"id": "6b27deb2-4f79-4314-914e-aa77615a9473",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel/)"
]
},
{
"cell_type": "markdown",
"id": "88e4d58c-b45c-415c-b533-90493b6913f5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-04-12 (Created: 2023-02-24)"
]
},
{
"cell_type": "markdown",
"id": "8e9e0abb-5a5b-4598-8c28-80ce88b85949",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook will create a prompt to write a social media post and be able to set the topic, the tone and the platform."
]
},
{
"cell_type": "markdown",
"id": "be53d612-3a13-45cd-9ef8-ee3d61782e97",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n",
"- [OpenAI](https://openai.com/)\n",
"- [Social Media Post](https://en.wikipedia.org/wiki/Social_media_post)"
]
},
{
"cell_type": "markdown",
"id": "2177434d-3b04-42e2-b1ad-2988122b7c53",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "90f2f911-b1b6-41d5-9d29-e5e507e33ce5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "6f9fefca-9ad3-467c-a555-223436ce3a5e",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"try:\n",
" import openai\n",
"except:\n",
" !pip install openai --user\n",
" import openai\n",
"import naas"
]
},
{
"cell_type": "markdown",
"id": "956fe229-f3db-4945-969d-ecff8db73e33",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup Variables\n",
"- `api_key`: OpenAI API key, to obtain an OpenAI API key, please refer to the [OpenAI Documentation](https://openai.com/docs/).\n",
"- `topic`: the topic of the blog post\n",
"- `tone`: the tone of the blog post\n",
"- `platform`: social media platform to publish your post on\n",
"\n",
"\n",
"<u>About \"tone\"</u>\n",
"\n",
"Here are some different tones you can use for a blog post:\n",
"- Formal: Use proper language, professional terms, and formal sentence structures. This tone is best used for academic or business-related blog posts.\n",
"- Informative: Stick to facts and provide helpful information. Avoid adding your opinion and present the content in a straightforward and objective manner.\n",
"- Conversational: Write as if you're talking to a friend. Use casual language, contractions, and personal anecdotes. This tone is best suited for lifestyle or personal blogs.\n",
"- Inspirational: Write in a way that motivates and encourages readers. Use positive language, inspiring stories, and uplifting messages.\n",
"- Humorous: Add some humor and make readers laugh. Use witty remarks, funny anecdotes, and sarcastic comments to engage readers.\n",
"- Persuasive: Try to persuade readers to take action or change their mind about a topic. Use strong arguments, statistics, and emotional appeals to convince readers.\n",
"- Opinionated: Share your personal opinion on a topic. Use strong language, personal experiences, and bold statements to express your views.\n",
"- Educational: Teach readers something new. Use step-by-step instructions, infographics, and helpful tips to educate readers on a topic.\n",
"- Critical: Analyze a topic critically and provide a detailed analysis. Use logic, reasoning, and evidence to support your arguments.\n",
"- Empathetic: Write in a way that connects with readers on an emotional level. Use empathy, compassion, and understanding to create a sense of community and support.\n",
"\n",
"<u>About \"social media platforms\"</u>\n",
"\n",
"Here's a list of social media platforms where you can write articles:\n",
"- LinkedIn: LinkedIn is a professional social media platform where you can publish articles on industry-related topics. It's a great place to establish yourself as a thought leader and connect with other professionals in your field.\n",
"- Facebook Notes: Facebook Notes is a feature on Facebook that allows users to write long-form posts and publish them to their profile or page. It's a great way to share personal stories or thoughts with your friends and followers.\n",
"- Twitter Threads: Twitter Threads are a series of connected tweets that can be used to share longer thoughts or stories. It's a great way to break up longer articles into bite-sized pieces and share them with your Twitter followers.\n",
"- Instagram Stories: Instagram Stories are a feature on Instagram that allows users to share short videos or images that disappear after 24 hours. It can be a great way to share personal stories or behind-the-scenes glimpses into your life or business.\n",
"- Reddit: Reddit is a social news aggregation and discussion platform where users can post articles, stories, and links to other websites. It's a great platform for niche communities and can be a good place to share your writing with like-minded individuals."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e80e9a0c-13c8-4936-80b6-b1217120d6d8",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"# Inputs\n",
"api_key = naas.secret.get(name=\"OPENAI_API_KEY\") or \"ENTER_YOUR_OPENAI_API_KEY\"\n",
"topic = \"The benefits of meditation\"\n",
"tone = \"Provocative\"\n",
"platform = \"Twitter\"\n",
"prompt = f\"Write a {tone.lower()} social media post about '{topic}' for {platform}.\"\n",
"\n",
"# Outputs\n",
"file_path = f\"Social_Media_Post_{topic.replace(' ', '_')}_{tone}_on_{platform}_characters.txt\""
]
},
{
"cell_type": "markdown",
"id": "e256e42f-c499-4b36-a0a5-cddb017c31c0",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "2548b6d2-fc88-4e6c-9b4f-85c80b0b06c9",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Send requests to OpenAI API"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "804ec893-6bb3-4ef7-9316-fe62dcc89e5e",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"openai.api_key = api_key\n",
"response = openai.Completion.create(\n",
" engine=\"text-davinci-003\",\n",
" prompt=prompt,\n",
" max_tokens=2084,\n",
" temperature=0.7,\n",
" top_p=0.9\n",
")"
]
},
{
"cell_type": "markdown",
"id": "61e9029b-f46e-416a-a582-98d9ef3b32dd",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Get text"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "aab37c9a-f7e3-472b-9962-51498e4c8bd8",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"text = response['choices'][0]['text']\n",
"print(text)"
]
},
{
"cell_type": "markdown",
"id": "0d09764b-4788-4e5c-b355-5bcb7aec2e91",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "6758fa30-bf30-4063-9803-2339810d33c7",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Save text to txt file"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "c7ed5e66-d833-4823-808f-2cff3982e6a8",
"metadata": {
"papermill": {},
"tags": []
},
"outputs": [],
"source": [
"text_file = open(file_path, \"w\")\n",
"text_file.write(text)\n",
"text_file.close()"
]
},
{
"cell_type": "markdown",
"id": "545114af-6a0d-4045-bf02-f8717cb4a94f",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"naas": {
"notebook_id": "96e1ff47c1c4011d1a31e25cba022771284016208eec7cdb625e49d2a326f65e",
"notebook_path": "OpenAI/OpenAI_Write_a_social_media_post.ipynb"
},
"papermill": {
"default_parameters": {},
"environment_variables": {},
"parameters": {},
"version": "2.4.0"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}