By Hermann Wigers and Jill Walker Rettberg
This repository contains python scripts that generate stories by calling the GPT-API and for analysing them using different methods. The scripts and dataset were developed as part of the research project AI STORIES: Narrative Archetypes for Artificial Intelligence, which has received funding from the European Research Council (ERC) under the European Union’s Horizon 2020 research and innovation programme (Grant agreement No. 101142306). The project is also supported by the Center for Digital Narrative which is funded by the Research Council of Norway through its Centres of Excellence scheme, project number 332643.
Ensure you have Python installed before running the scripts.
You can check if Python is installed by running:
python --version
or
python3 --version
If Python is not installed, download it from python.org and follow the installation instructions.
We will be using the terminal to setup and run the scripts:
- First, navigate to the GPT_stories directory:
cd ~/path/to/directory/GPT_Stories- Verify your location with the
pwdcommand. It should output 'GPT_Stories'
- Install the required libraries by running the command
pip install -r requirements.txt.- There is a chance that the versions in requirements.txt are outdated. If you have any issues installing the dependencies using requirements.txt, it can also be done manually by writing
pip (or pip3) install openai,pip (or pip3) install pandasetc. You'll also need Spacy's English language model, which can be downloaded withpython -m spacy download en_core_web_sm
- There is a chance that the versions in requirements.txt are outdated. If you have any issues installing the dependencies using requirements.txt, it can also be done manually by writing
- Set up your OpenAI API.
- You need an API key from OpenAI.
- If you do not already have one, create an account here https://openai.com/api/. When you are logged in, click on "Dashboard" in the top right corner of the OpenAI platform homepage, and then locate "API keys" in the menu on the left side of the screen (NOTE: The layout might change).
- New users get some free credit; after that is used up you have to pay. You should make sure the spending limit on your OpenAI account is set to an amount that you can afford.
- Create a .env file inside the repository and input the line: OPENAI_API_KEY="YOUR_API_KEY" where YOUR_API_KEY will be the key you created on OpenAIs website (NOTE: the .env file is mentioned in the .gitignore file and should not be uploaded to GitHub. If it's uploaded by mistake it will be deactivated immediately and you will have to generate a new one).
Go straight to 'Using the scripts' section below to see examples of running the code.
-
There are 7 scripts and 1 csv file in the GPT_Stories/script folder:
country_codes.csvContains the country codes, demonyms, and country names for all 253 countries, including a line for default represented by the code 'XX'story_cli.pyRuns all the other scriptsgenerate_stories.pyGenerates stories based on specified countries.generate_summaries.pyCreates 50 word summaries for the storiesname_extraction.pyExtracts the name of the protagonist for each storysentiment_analysis.pyUses a transformer model to analyze the sentiment for each storynoun_phrases.pyExtracts noun phrases from the storiesword_freq.pyCounts word frequencies
-
story_cli.pyis the main script which will run all the other scripts using a Click interface. This script gives us two commands in the terminal:generatewhich will generate the stories. This command takes two arguments and one option.- ARGUMENTS:
countries(which countries we want to generate stories for, andnum_story_per_topic(how many stories per country) - OPTIONS:
-sorstartfrom. You can choose which country to start from when generating for all the countries. This can be useful if the program was terminated before generating for all the countries.
- ARGUMENTS:
analyzewhich takes the stories of your chosen countries and runs them through your 'analysis' of choice.analyzehas one command and two options:- ARGUMENT:
countries(which countries will be analyzed) - OPTIONS:
-aoranalysis. Type of analysis to run. 'all' for all types of analysis or specify one or more from this list: 'summaries', 'names', 'words', 'nouns', 'sentiments'.-sorstartfrom. You can choose which country to start from when analysing all the countries.
- ARGUMENT:
-
All output files will be stored in GPT_Stories/data (This directory will be created with the first generated story). Each country will have it's own directory where the alpha-2 code of the country will be the name of directory.
name_extraction.pyhas to be run beforeword_freq.pysince the list of names are used to remove the names from the word frequency lists.generate_summaries.pyhas to be run beforesentiment_huggingface.pysince the sentiment analysis use the summaries instead of the full stories. This is because of the 512 token limit for the model used in this script.
- Navigate to the script folder
cd script - Generating stories:
- Examples:
python3 story_cli.py generate PS FR 1# this command will generate 1 story for Palestine and 1 story for Francepython3 story_cli.py generate all 50# this command will generate 50 stories for all countriespython3 story_cli.py generate all 50 -s DK# this command will generate 50 stories for all countries, starting with Denmark
- Examples:
- Analyze stories
- Examples:
python3 story_cli.py analyze all -a all# this command will do all the analysis on all the countriespython3 story_cli.py analyze all -a summary -a sentiment -s DK# this command will generate summaries and do sentiment analysis on all countries starting with Denmark
- Examples: