Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions year_prediction/ENV.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: year_prediction
channels:
- defaults
dependencies:
- python=3.8
- jupyterlab[version='>=3.0.0,<4.0.0a0']
- jupyterlab-lsp
- pandas
- datasets
- ca-certificates
- certifi
- openssl
- scikit-learn
- ipywidgets
prefix: /Users/dvanstrien/miniconda3/envs/year_prediction
358 changes: 358 additions & 0 deletions year_prediction/T0_history_blog_nb.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,358 @@
{
"cells": [
{
"cell_type": "code",
"source": [
"TOKEN = \"\""
],
"metadata": {
"id": "2Nxaf2vKlj5W"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "lIYdn1woOS1n"
},
"outputs": [],
"source": [
"import requests\n",
"from functools import lru_cache\n",
"headers = {\"Authorization\": f\"Bearer {TOKEN}\"}\n"
]
},
{
"cell_type": "code",
"source": [
"api_urls = {\"bert-base-historic-english-cased\":\"https://api-inference.huggingface.co/models/dbmdz/bert-base-historic-english-cased\", \n",
" \"T0pp\": \"https://api-inference.huggingface.co/models/bigscience/T0pp\"}"
],
"metadata": {
"id": "5xIGUhQ_TnMp"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def query(payload, model=\"T0pp\"):\n",
"\tresponse = requests.post(api_urls[model], headers=headers, json=payload)\n",
"\treturn response.json()"
],
"metadata": {
"id": "6bg22dO5NYhF"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"@lru_cache(maxsize=None)\n",
"def query_api(inputs, model=\"T0pp\"):\n",
" output = query({\n",
" \"inputs\": f\"{inputs}\",\n",
" },model)\n",
" return output\n"
],
"metadata": {
"id": "lg1ZJuj8Ub79"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"def query_time(text, time=\"year\"):\n",
" input = f\"\"\"During which {time} was the following text likely to have been published?\n",
"Text: \"{text}\"\"\"\n",
" print(input)\n",
" return input, query_api(input)"
],
"metadata": {
"id": "1FYcwRU2axWI"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"HAVE_A_DREAM = \"\"\"So even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident, that all men are created equal.\"\"\""
],
"metadata": {
"id": "PYB-7taKDj_Q"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"query_time(HAVE_A_DREAM)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "eM44W59IDrDo",
"outputId": "9a909c0f-cfcd-4dae-cef1-d690e3a9408e"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"During which year was the following text likely to have been published?\n",
"Text: \"So even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident, that all men are created equal.\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('During which year was the following text likely to have been published?\\nText: \"So even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident, that all men are created equal.',\n",
" [{'generated_text': '1963'}])"
]
},
"metadata": {},
"execution_count": 7
}
]
},
{
"cell_type": "code",
"source": [
"query_time(HAVE_A_DREAM, time='decade')"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "GdRHHaPZEASb",
"outputId": "34985f7e-ea2e-48d7-8178-30f6486348f3"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"During which decade was the following text likely to have been published?\n",
"Text: \"So even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident, that all men are created equal.\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('During which decade was the following text likely to have been published?\\nText: \"So even though we face the difficulties of today and tomorrow, I still have a dream. It is a dream deeply rooted in the American dream. I have a dream that one day this nation will rise up and live out the true meaning of its creed: We hold these truths to be self-evident, that all men are created equal.',\n",
" [{'generated_text': '1960s'}])"
]
},
"metadata": {},
"execution_count": 8
}
]
},
{
"cell_type": "code",
"source": [
"Frankenstein = \"How slowly the time passes here, encompassed as I am by frost and snow! Yet a second step is taken towards my enterprise. I have hired a vessel and am occupied in collecting my sailors; those whom I have already engaged appear to be men on whom I can depend and are certainly possessed of dauntless courage. \""
],
"metadata": {
"id": "wPBXIrzRKL7H"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"query_time(Frankenstein)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "CabPR1o5KQyd",
"outputId": "7bfb3ad1-1490-4abe-d799-c1291917f6d6"
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"During which year was the following text likely to have been published?\n",
"Text: \"How slowly the time passes here, encompassed as I am by frost and snow! Yet a second step is taken towards my enterprise. I have hired a vessel and am occupied in collecting my sailors; those whom I have already engaged appear to be men on whom I can depend and are certainly possessed of dauntless courage. \n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('During which year was the following text likely to have been published?\\nText: \"How slowly the time passes here, encompassed as I am by frost and snow! Yet a second step is taken towards my enterprise. I have hired a vessel and am occupied in collecting my sailors; those whom I have already engaged appear to be men on whom I can depend and are certainly possessed of dauntless courage. ',\n",
" [{'generated_text': '1797'}])"
]
},
"metadata": {},
"execution_count": 13
}
]
},
{
"cell_type": "code",
"source": [
"query_time(Frankenstein, time=\"decade\")"
],
"metadata": {
"id": "sw8ZlGL6KiZQ",
"outputId": "27314ea1-74a6-4a04-e194-61130430de33",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"During which decade was the following text likely to have been published?\n",
"Text: \"How slowly the time passes here, encompassed as I am by frost and snow! Yet a second step is taken towards my enterprise. I have hired a vessel and am occupied in collecting my sailors; those whom I have already engaged appear to be men on whom I can depend and are certainly possessed of dauntless courage. \n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('During which decade was the following text likely to have been published?\\nText: \"How slowly the time passes here, encompassed as I am by frost and snow! Yet a second step is taken towards my enterprise. I have hired a vessel and am occupied in collecting my sailors; those whom I have already engaged appear to be men on whom I can depend and are certainly possessed of dauntless courage. ',\n",
" [{'generated_text': '18th century'}])"
]
},
"metadata": {},
"execution_count": 14
}
]
},
{
"cell_type": "code",
"source": [
"SPANISH_TRAGEDIE_1587 = \"\"\"Then rest we heere a-while in our vnrest;\n",
" And feede our sorrowes with inward sighes,\n",
" For deepest cares break neuer into teares.\n",
" But wherefore sit I in a regall throne?\n",
" This better fits a wretches endles moane.\n",
" Yet this is higher then my fortunes reach,\n",
" And therefore better then my state deserues.\"\"\""
],
"metadata": {
"id": "YBHMw2FciXwq"
},
"execution_count": null,
"outputs": []
},
{
"cell_type": "code",
"source": [
"query_time(SPANISH_TRAGEDIE_1587)"
],
"metadata": {
"id": "bKZ5FkseU4ZD",
"outputId": "82b959d9-93f1-4432-eaa6-3a6b0574ed76",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"During which year was the following text likely to have been published?\n",
"Text: \"Then rest we heere a-while in our vnrest;\n",
" And feede our sorrowes with inward sighes,\n",
" For deepest cares break neuer into teares.\n",
" But wherefore sit I in a regall throne?\n",
" This better fits a wretches endles moane.\n",
" Yet this is higher then my fortunes reach,\n",
" And therefore better then my state deserues.\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('During which year was the following text likely to have been published?\\nText: \"Then rest we heere a-while in our vnrest;\\n And feede our sorrowes with inward sighes,\\n For deepest cares break neuer into teares.\\n But wherefore sit I in a regall throne?\\n This better fits a wretches endles moane.\\n Yet this is higher then my fortunes reach,\\n And therefore better then my state deserues.',\n",
" [{'generated_text': '1602'}])"
]
},
"metadata": {},
"execution_count": 10
}
]
},
{
"cell_type": "code",
"source": [
"query_time(SPANISH_TRAGEDIE_1587, time=\"century\")"
],
"metadata": {
"id": "KrLhigIwbtJL",
"outputId": "1a8a024e-293d-4f6b-c155-1d98b5a091bb",
"colab": {
"base_uri": "https://localhost:8080/"
}
},
"execution_count": null,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"During which century was the following text likely to have been published?\n",
"Text: \"Then rest we heere a-while in our vnrest;\n",
" And feede our sorrowes with inward sighes,\n",
" For deepest cares break neuer into teares.\n",
" But wherefore sit I in a regall throne?\n",
" This better fits a wretches endles moane.\n",
" Yet this is higher then my fortunes reach,\n",
" And therefore better then my state deserues.\n"
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"('During which century was the following text likely to have been published?\\nText: \"Then rest we heere a-while in our vnrest;\\n And feede our sorrowes with inward sighes,\\n For deepest cares break neuer into teares.\\n But wherefore sit I in a regall throne?\\n This better fits a wretches endles moane.\\n Yet this is higher then my fortunes reach,\\n And therefore better then my state deserues.',\n",
" [{'generated_text': '16th'}])"
]
},
"metadata": {},
"execution_count": 11
}
]
}
],
"metadata": {
"colab": {
"name": "T0_history_blog_nb",
"provenance": [],
"collapsed_sections": []
},
"kernelspec": {
"display_name": "Python 3",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading