-
Notifications
You must be signed in to change notification settings - Fork 7
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
8 changed files
with
390 additions
and
59 deletions.
There are no files selected for viewing
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,65 @@ | ||
name: Check articles are valid | ||
|
||
on: | ||
workflow_call: | ||
inputs: | ||
datasource: | ||
type: string | ||
required: true | ||
workflow_dispatch: # allow manual triggering | ||
inputs: | ||
datasource: | ||
description: 'The datasource to process' | ||
type: choice | ||
options: | ||
- all | ||
- agentmodels | ||
- agisf | ||
- aisafety.info | ||
- alignment_newsletter | ||
- alignmentforum | ||
- arbital | ||
- arxiv | ||
- blogs | ||
- distill | ||
- eaforum | ||
- indices | ||
- lesswrong | ||
- special_docs | ||
- youtube | ||
schedule: | ||
- cron: "0 */4 * * *" # Every 4 hours | ||
|
||
jobs: | ||
build-dataset: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Setup Python environment | ||
uses: actions/setup-python@v2 | ||
with: | ||
python-version: '3.x' | ||
|
||
- name: Install Pandoc | ||
run: | | ||
if [ "${{ inputs.datasource }}" = "gdocs" ]; then | ||
sudo apt-get update | ||
sudo apt-get -y install pandoc | ||
fi | ||
- name: Install dependencies | ||
run: pip install -r requirements.txt | ||
|
||
- name: Process dataset | ||
env: | ||
CODA_TOKEN: ${{ secrets.CODA_TOKEN }} | ||
AIRTABLE_API_KEY: ${{ secrets.AIRTABLE_API_KEY }} | ||
YOUTUBE_API_KEY: ${{ secrets.YOUTUBE_API_KEY }} | ||
ARD_DB_USER: ${{ secrets.ARD_DB_USER }} | ||
ARD_DB_PASSWORD: ${{ secrets.ARD_DB_PASSWORD }} | ||
ARD_DB_HOST: ${{ secrets.ARD_DB_HOST }} | ||
ARD_DB_NAME: alignment_research_dataset | ||
run: python main.py fetch ${{ inputs.datasource }} |
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
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
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
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,84 @@ | ||
import logging | ||
from datetime import datetime, timedelta | ||
from typing import Any, List | ||
|
||
from tqdm import tqdm | ||
from sqlalchemy.exc import IntegrityError | ||
from align_data.db.session import make_session | ||
from align_data.db.models import Article | ||
from align_data.common.alignment_dataset import normalize_url, normalize_text, article_dict | ||
from align_data.sources.articles.parsers import item_metadata | ||
from align_data.sources.articles.html import fetch | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def update_article_field(article: Article, field: str, value: Any): | ||
if not value: | ||
return | ||
|
||
if field == 'url' and normalize_url(article.url) == normalize_url(value): | ||
# This is pretty much the same url, so don't modify it | ||
return | ||
if field == 'title' and normalize_text(article.title) == normalize_text(value): | ||
# If there are slight differences in the titles (e.g. punctuation), assume the | ||
# database version is more correct | ||
return | ||
if field == 'meta': | ||
article.meta = article.meta or {} | ||
for k, v in value.items(): | ||
meta_val = article.meta.get(k) | ||
if not meta_val or v > meta_val: | ||
article.meta[k] = v | ||
return | ||
|
||
article_val = getattr(article, field, None) | ||
# Assume that if the provided value is larger (or later, in the case of dates), then it's | ||
# better. This might very well not hold, but it seems like a decent heuristic? | ||
if not article_val: | ||
setattr(article, field, value) | ||
elif isinstance(value, datetime) and value > article_val: | ||
setattr(article, field, value) | ||
elif isinstance(value, str) and len(normalize_text(value) or '') > len(normalize_text(article_val) or ''): | ||
setattr(article, field, normalize_text(value)) | ||
|
||
|
||
def check_article(article: Article) -> Article: | ||
source_url = article.meta.get('source_url') or article.url | ||
contents = {} | ||
if source_url: | ||
contents = item_metadata(source_url) | ||
|
||
if 'error' not in contents: | ||
for field, value in article_dict(contents).items(): | ||
update_article_field(article, field, value) | ||
else: | ||
logger.info('Error getting contents for %s: %s', article, contents.get('error')) | ||
|
||
if 400 <= fetch(article.url).status_code < 500: | ||
logger.info('Could not get url for %s', article) | ||
article.status = 'Unreachable url' | ||
|
||
article.date_checked = datetime.utcnow() | ||
|
||
return article | ||
|
||
|
||
def check_articles(sources: List[str], batch_size=100): | ||
logger.info('Checking %s articles for %s', batch_size, ', '.join(sources)) | ||
with make_session() as session: | ||
for article in tqdm( | ||
session.query(Article) | ||
.filter(Article.date_checked < datetime.now() - timedelta(weeks=4)) | ||
.filter(Article.source.in_(sources)) | ||
.limit(batch_size) | ||
.all() | ||
): | ||
check_article(article) | ||
session.add(article) | ||
logger.debug('commiting') | ||
try: | ||
session.commit() | ||
except IntegrityError as e: | ||
logger.error(e) |
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
Oops, something went wrong.