Skip to content

Instantly share code, notes, and snippets.

@wupeixian
Created April 3, 2023 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wupeixian/23f05d3386d0d36c4c8e063a4ff75d3b to your computer and use it in GitHub Desktop.
Save wupeixian/23f05d3386d0d36c4c8e063a4ff75d3b to your computer and use it in GitHub Desktop.
Assignment 5
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"id": "89d95a1c",
"metadata": {},
"outputs": [],
"source": [
"from transformers import pipeline, AutoModelForCausalLM, AutoTokenizer\n",
"tokenizer = AutoTokenizer.from_pretrained('distilgpt2')\n",
"model = AutoModelForCausalLM.from_pretrained('distilgpt2')\n",
"generator = pipeline('text-generation', model=model, tokenizer=tokenizer)"
]
},
{
"cell_type": "code",
"execution_count": 82,
"id": "345d2fe4",
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"import markovify"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "1e110c4f",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/wupeixian/opt/anaconda3/lib/python3.9/site-packages/transformers/generation/utils.py:1201: UserWarning: You have modified the pretrained model configuration to control generation. This is a deprecated strategy to control generation and will be removed soon, in a future version. Please use a generation configuration file (see https://huggingface.co/docs/transformers/main_classes/text_generation)\n",
" warnings.warn(\n",
"Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n",
"/Users/wupeixian/opt/anaconda3/lib/python3.9/site-packages/transformers/generation/utils.py:1288: UserWarning: Using `max_length`'s default (50) to control the generation length. This behaviour is deprecated and will be removed from the config in v5 of Transformers -- we recommend using `max_new_tokens` to control the maximum length of the generation.\n",
" warnings.warn(\n"
]
},
{
"data": {
"text/plain": [
"'Two roads diverged in a yellow wood, and all of a sudden the trees were about 200 yards apart.\\n\\n\\n\\n\"We stopped at the center of my own house just before 12:00 a.m.,\" said D\\'Ou'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generator(\"Two roads diverged in a yellow wood, and\")[0]['generated_text']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "c76216a1",
"metadata": {},
"outputs": [],
"source": [
"text_a = open(\"NotesfromtheUnderground.txt\").read()\n",
"text_b = open(\"OntheHeightsofDespair.txt\").read()"
]
},
{
"cell_type": "code",
"execution_count": 75,
"id": "d9c514cd",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" There was a great deal of noise, even shouting, in a room further\n",
"away; one could hear the laughter of a crowd of people, and nasty little\n",
"shrieks in French: there were ladies at the dinner\n"
]
}
],
"source": [
"sentences_a = text_a.split(\".\")\n",
"print(random.choice(sentences_a))"
]
},
{
"cell_type": "code",
"execution_count": 81,
"id": "5bb2dbc9",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" Let the pathos and drama of infinity come to us once more in the loneliness of death so that our passing away into nothingness will resemble an illumination amplifying the mystery and the meaninglessness of this world\n"
]
}
],
"source": [
"sentences_b = text_b.split(\".\")\n",
"print(random.choice(sentences_b))"
]
},
{
"cell_type": "code",
"execution_count": 165,
"id": "5b0c4e9c",
"metadata": {},
"outputs": [],
"source": [
"generator_a = markovify.Text(text_a)\n",
"generator_b = markovify.Text(text_b)\n",
"length_a = 50\n",
"length_b = 50\n",
"length_combo = 50\n",
"weight_a = 0.5\n",
"weight_b = 0.5\n",
"combo = markovify.combine([generator_a, generator_b], [weight_a, weight_b])"
]
},
{
"cell_type": "code",
"execution_count": 124,
"id": "ab8f7440",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Though she did not even imagine she would do so.\n"
]
}
],
"source": [
"print(generator_a.make_short_sentence(length_a))"
]
},
{
"cell_type": "code",
"execution_count": 125,
"id": "1ea00759",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Let us live our inner fire will ravish all.\n"
]
}
],
"source": [
"print(generator_b.make_short_sentence(length_b))"
]
},
{
"cell_type": "code",
"execution_count": 126,
"id": "ffc7dd8c",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stay, let me die of love?\n"
]
}
],
"source": [
"print(combo.make_short_sentence(length_combo))"
]
},
{
"cell_type": "code",
"execution_count": 130,
"id": "8f161033",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
]
},
{
"data": {
"text/plain": [
"'She did not know how to begin. He had not understood why he was in the same situation.”\\n\\n\"Hang on, I\\'m not a good person,\" he told her.\\n\"I\\'m going to help you, I'"
]
},
"execution_count": 130,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"generator(generator_a.make_short_sentence(length_a))[0]['generated_text']"
]
},
{
"cell_type": "code",
"execution_count": 131,
"id": "b1e33889",
"metadata": {
"scrolled": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"How is enjoyment in the room.\n",
"None\n",
"As I was going away or not going away.\n",
"\n",
"\n",
"And so let it be a kind of remorse.\n",
"Not living under the sign of death.\n",
"Let everything vibrate in me only now.\n",
"\n",
"\n",
"How I hated her if she comes?\n",
"None\n",
"Time, in this turmoil.\n",
"\n",
"\n"
]
}
],
"source": [
"for stanza in range(3):\n",
" print(generator_a.make_short_sentence(length_a))\n",
" print(generator_b.make_short_sentence(length_b))\n",
" print(combo.make_short_sentence(length_combo))\n",
" \n",
" length_a -= 10\n",
" length_b -= 10\n",
" length_combo -= 10\n",
" weight_a -= 0.2\n",
" weight_b += 0.2\n",
" combo = markovify.combine([generator_a, generator_b], [weight_a, weight_b])\n",
" \n",
" print('\\n')"
]
},
{
"cell_type": "code",
"execution_count": 166,
"id": "63a5a689",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n",
"Input length of input_ids is 72, but `max_length` is set to 50. This can lead to unexpected behavior. You should consider increasing `max_new_tokens`.\n",
"Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"I had my underground hole.\n",
" (\n",
"I love someone, but since she is the son of God.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n",
"Input length of input_ids is 50, but `max_length` is set to 50. This can lead to unexpected behavior. You should consider increasing `max_new_tokens`.\n",
"Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" destiny. Man's consciousness is a self-created consciousness, one or none of the other, that is the most fundamental notion of existence. The essence is the human consciousness. In\n",
"Everything then has a monopoly on suffering.\n",
"\n",
"\n",
"Anyway, one can keep it!\n",
" or\n",
"None\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" that never breaks into its final form. When\n",
"If only for the truth.\n",
"\n",
"\n",
"There was a nightmare.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Setting `pad_token_id` to `eos_token_id`:50256 for open-end generation.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
" of a local library, and your punishment is handed back. The library will be replaced by your own.\n",
"\n",
"\n",
"\n",
"As a result, when you get kicked out of the library you don't have to use your\n",
"None\n",
" and remain more distinct than when drawn within these, but even in the midst of one's own great passions or, the more numerous of them can be said as well, the\n",
"I am afraid of lights in it.\n",
"\n",
"\n"
]
}
],
"source": [
"for stanza in range(3):\n",
" print(generator_a.make_short_sentence(length_a))\n",
" s_a = random.choice(sentences_a)\n",
" print(generator(s_a)[0]['generated_text'][len(s_a):])\n",
" \n",
" print(generator_b.make_short_sentence(length_b))\n",
" s_b = random.choice(sentences_b)\n",
" print(generator(s_b)[0]['generated_text'][len(s_b):])\n",
" \n",
" print(combo.make_short_sentence(length_combo))\n",
" \n",
" \n",
" length_a -= 10\n",
" length_b -= 10\n",
" length_combo -= 10\n",
" weight_a -= 0.2\n",
" weight_b += 0.2\n",
" combo = markovify.combine([generator_a, generator_b], [weight_a, weight_b])\n",
" \n",
" print('\\n')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.13"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment