-
Notifications
You must be signed in to change notification settings - Fork 453
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
284 additions
and
0 deletions.
There are no files selected for viewing
284 changes: 284 additions & 0 deletions
284
Google Gemini/Google_Gemini_Write_a_social_media_post.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,284 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"id": "88c104cc-bf08-4242-821b-b3a40908152a", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"<img width=\"8%\" alt=\"Google Gemini.png\" src=\"https://github.com/jupyter-naas/workspace/assets/81156510/1e9d75ba-84bc-47cb-97ea-5bd6b4ace417\" style=\"border-radius: 15%\">" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "compressed-wilson", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"# Gemini - Write a social media post" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "religious-programmer", | ||
"metadata": {}, | ||
"source": [ | ||
"**Tags**: #google #gemini #socialmedia #post #prompt #tone #platform" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "1fe9f56e-561c-4f52-aef8-b861c9462107", | ||
"metadata": {}, | ||
"source": [ | ||
"**Author:** [Sriniketh Jayasendil](https://www.linkedin.com/in/sriniketh-jayasendil)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "0de144be-594d-463e-9849-696fb2f6d1a8", | ||
"metadata": {}, | ||
"source": [ | ||
"**Last update:** YYYY-MM-DD (Created: YYYY-MM-DD) -> The last update date refers to when the notebook was last edited, while the created date corresponds to when the notebook was initially merged." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "31ea7cdb-e10d-43fc-b026-f69249a59736", | ||
"metadata": {}, | ||
"source": [ | ||
"**Description:** This notebook will help us write a social media post and configure based on the topic, the tone and the platform." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "d4b89388-5cd5-409a-8169-c53cc8dfab96", | ||
"metadata": {}, | ||
"source": [ | ||
"**References:** list of references and websites utilized in the creation of this notebook\n", | ||
"- [Naas Documentation](https://site.naas.ai/)\n", | ||
"- [Google AI Dev](https://ai.google.dev)\n", | ||
"- [Gemini Python Docs](https://ai.google.dev/tutorials/python_quickstart)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "distinguished-truth", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"source": [ | ||
"## Input" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "numeric-mediterranean", | ||
"metadata": {}, | ||
"source": [ | ||
"### Import libraries" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "potential-surfing", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"import naas\n", | ||
"try:\n", | ||
" import google.generativeai as genai\n", | ||
"except ModuleNotFoundError:\n", | ||
" !pip install google-generativeai --user\n", | ||
" import google.generativeai as genai" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "aggressive-trustee", | ||
"metadata": {}, | ||
"source": [ | ||
"### Setup Variables\n", | ||
"\n", | ||
"- `api_key`: Google Gemini API. Get one from [here](https://makersuite.google.com/app/apikey)\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\n", | ||
"\n", | ||
"#### About \"tone\"\n", | ||
"\n", | ||
"Here are some different tones you can use for a blog post:\n", | ||
"\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", | ||
"#### About \"social media platforms\"\n", | ||
"\n", | ||
"Here's a list of social media platforms where you can write articles:\n", | ||
"\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": null, | ||
"id": "continuous-melbourne", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"google_api_key = naas.secret.get(\"GOOGLE_API_KEY\") or \"Enter your API key\"\n", | ||
"topic = \"What are the benefits of being consistent?\"\n", | ||
"tone = \"Inspirational\"\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.md\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "registered-showcase", | ||
"metadata": {}, | ||
"source": [ | ||
"## Model\n", | ||
"### Send requests to OpenAI API" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "crude-louisville", | ||
"metadata": { | ||
"papermill": {}, | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"try:\n", | ||
" genai.configure(api_key=google_api_key)\n", | ||
" model = genai.GenerativeModel('gemini-pro')\n", | ||
" response = model.generate_content(prompt)\n", | ||
"except:\n", | ||
" print(\"Cannot run code in your region. Check here for valid regions: https://ai.google.dev/available_regions#available_regions\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "472ed28e-e11f-4820-8aee-73006c25c28c", | ||
"metadata": {}, | ||
"source": [ | ||
"### Get text" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "3f14b302-c1b6-4f30-8097-2b2baef8e089", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"text = response.text\n", | ||
"print(text)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"id": "lonely-pacific", | ||
"metadata": { | ||
"execution": { | ||
"iopub.execute_input": "2021-07-02T23:32:10.789097Z", | ||
"iopub.status.busy": "2021-07-02T23:32:10.788829Z", | ||
"iopub.status.idle": "2021-07-02T23:32:10.796900Z", | ||
"shell.execute_reply": "2021-07-02T23:32:10.796358Z", | ||
"shell.execute_reply.started": "2021-07-02T23:32:10.789033Z" | ||
} | ||
}, | ||
"source": [ | ||
"## Output\n", | ||
"### Save text to txt file" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "9c4e3b7b-6440-4844-8054-265f1aec65eb", | ||
"metadata": { | ||
"tags": [] | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"text_file = open(file_path, \"w\")\n", | ||
"text_file.write(text)\n", | ||
"text_file.close()\n", | ||
"print('Done...')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "a5d441ee-af04-4438-9d6f-316e998ad62a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"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" | ||
}, | ||
"papermill": { | ||
"default_parameters": {}, | ||
"environment_variables": {}, | ||
"parameters": {}, | ||
"version": "2.3.3" | ||
}, | ||
"widgets": { | ||
"application/vnd.jupyter.widget-state+json": { | ||
"state": {}, | ||
"version_major": 2, | ||
"version_minor": 0 | ||
} | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |