{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "private_outputs": true, "provenance": [], "authorship_tag": "ABX9TyMD+lkkzQAyUSVoHB/gb9Th", "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" }, "language_info": { "name": "python" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "source": [ "Step 1: Install required libraries" ], "metadata": { "id": "gle3BE4NKJXv" } }, { "cell_type": "code", "execution_count": null, "metadata": { "id": "Hy5U0sHGyHnj" }, "outputs": [], "source": [ "!pip install openai\n", "!pip install newspaper3k\n", "!pip install praw" ] }, { "cell_type": "markdown", "source": [ "Step 2: Run this cell after adding your OpenAI credentials and Reddit Credentials. You can get the reddit credentials you need here: https://www.reddit.com/prefs/apps" ], "metadata": { "id": "purWEZZsKUFF" } }, { "cell_type": "code", "source": [ "import openai\n", "import newspaper\n", "import praw\n", "import pandas as pd\n", "\n", "# Set up OpenAI API credentials\n", "openai.api_key = 'Your API key'\n", "\n", "# Set up Reddit API credentials\n", "reddit = praw.Reddit(\n", " client_id='Enter client ID',\n", " client_secret='Enter client secret',\n", " user_agent='Enter user agent',\n", ")\n", "\n", "\n", "# Define a function to generate potential titles using the OpenAI API\n", "def generate_titles(article_text, subreddit_name):\n", " prompt = f'You are an expert at Reddit and deeply understand each subreddit community. Their likes, dislikes, what they find compelling, what they find controversial, etc. Your goal is to generate a Reddit post title for an article about {article_text} that would perform well on {subreddit_name}. Make sure the title is highly customized to fit the tone/style/voice of the given subreddit to make it as interesting as possible to that specific audience and adheres to that subreddits rules. The titles should be varied and highly customized for the audience of the subreddit. \\n Title:'\n", " response = openai.Completion.create(\n", " engine='text-davinci-003',\n", " prompt=prompt,\n", " max_tokens=120,\n", " n=5,\n", " stop=None,\n", " temperature=0.7,\n", " )\n", " title = response.choices[0].text.strip()\n", " return title\n", "\n", "\n", "\n", "def generate_summary(article_text):\n", " prompt = f'Generate a long and comprehensive summary for an article about the following article that highlights the parts that are most interesting, newsworthy or shocking. Also provide a readout of 20 specific audiences that would be most likely to find the article compelling and vote it up on Reddit. \\n Article Text: {article_text} \\n Summary: '\n", " response = openai.Completion.create(\n", " engine='text-davinci-003',\n", " prompt=prompt,\n", " max_tokens=1500,\n", " n=1,\n", " stop=None,\n", " temperature=0.7,\n", " )\n", " summary = response.choices[0].text.strip()\n", " return summary\n", "\n", "\n", "# Scrape the article text using newspaper3k\n", "article_url = 'Your Article URL'\n", "article = newspaper.Article(article_url)\n", "article.download()\n", "article.parse()\n", "article_text = article.text\n", "\n", "# Truncate the article if it's longer than 4,096 tokens\n", "if len(article.text) > 3096:\n", " article.text = article.text[:3096]\n", "\n", "# Generate a summary for the article using the OpenAI API\n", "article_summary = generate_summary(article.text)\n", "print(article_summary)\n", "\n", "\n", "# Use the OpenAI API to generate a sorted list of potential subreddits for the article\n", "prompt = f'You are an all knowing AI Reddit power user and an expert at understanding subreddit audiences. Given this article summary and associated list of the most appropriate audiences that would be likely to upvote the article, provide a list of 25 real and well targeted subreddits sorted by most popular first. Consider all subreddits where it might find success. Only suggest subreddits that accept link submissions. Think step by step. Before answering, think deeply about what an audience might like. Please provide as wide a variety of results as possible and prioritize high volume subreddits. Do not suggest image, gif, or video only subreddits unless the article contains this content. \\n Article Summary and Audience Readout: \\n ### {article_summary} ### \\n Suggested Subreddits for Submission: \\n'\n", "response = openai.Completion.create(\n", " engine='text-davinci-003',\n", " prompt=prompt,\n", " max_tokens=2000,\n", " n=5,\n", " stop=None,\n", " temperature=0.5,\n", ")\n", "potential_subreddits = response.choices[0].text.strip().split('\\n')\n", "\n", "\n", "# Generate potential titles for the article tailored to each recommended subreddit\n", "potential_titles = {subreddit_name: generate_titles(article_summary, subreddit_name) for subreddit_name in potential_subreddits}\n", "\n", "# Create a Pandas DataFrame to store the results\n", "df = pd.DataFrame({\n", " 'Subreddit': potential_subreddits,\n", " 'Title': list(potential_titles.values()),\n", "})\n", "\n", "print(df)\n", "df.to_csv(\"reddit_recommendations.csv\")" ], "metadata": { "id": "-UxP6j4c2de4" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "nHCb__1840_H" }, "execution_count": null, "outputs": [] } ] }