Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/amaiya/ktrain into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
amaiya committed Apr 15, 2023
2 parents f689da4 + b864741 commit ca40637
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 46 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from ktrain.text.generative_ai import GenerativeAI
model = GenerativeAI() # needs at least 16GB of GPU memory
prompt = """Extract the names of people in the supplied sentences. Here is an example:
Sentence:Paul Newman is a great actor.
Sentence:
Paul Newman is a great actor.
People:
Paul Newman
Sentence:
Expand Down
109 changes: 64 additions & 45 deletions examples/text/generative_ai_example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -27,7 +27,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -43,7 +43,7 @@
"\n",
"**Tip**: Due to the way the model was trained, you should only use newlines to separate cohesive units of information fed to the model. This is illustrated through various examples below. If the model encounters a misplaced newline character it doesn't like, it may output nothing.\n",
"\n",
"We also caution that the model has no guard rails against producing content that is potentially biased or offensive."
"We also caution that the model has no guard rails against producing content that is potentially biased, offensive, or factually-incorrect."
]
},
{
Expand All @@ -62,7 +62,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -90,7 +90,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -141,14 +141,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Positive\n",
"positive\n",
"\n"
]
}
Expand All @@ -163,12 +163,12 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Entity Extraction"
"#### Information Extraction"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -206,7 +206,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -239,14 +239,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"After a 20-year war, Trump and Biden's decisions to withdraw American troops from Afghanistan resulted in Kabul falling to the Taliban without resistance.\n",
"After a 20-year war, Trump and Biden's decision to withdraw American troops from Afghanistan resulted in the fall of Kabul to the Taliban, without resistance.\n",
"\n"
]
}
Expand All @@ -266,7 +266,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -317,14 +317,14 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"A t-shirt for men, with a message of strength and power at $39.\n",
"A t-shirt for men, with a great design and color options, at $39.\n",
"\n"
]
}
Expand Down Expand Up @@ -356,69 +356,76 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Text Generation"
"#### Tweet Generation"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Once upon a time, there was a young scientist named Harold who was working on a grand experiment to control time. He had spent years collecting data and researching the past, in hopes of one day rewriting history. But one day, he stumbled upon a mysterious paper that suggested his experiment was not just possible, but also likely to happen. Harold was determined to find out the truth, and so he decided to embark on a daring adventure full of excitement and possibility. What will happen when Harold attempts to alter the future? Will the changes be for the better or the worse? Time will tell what destiny awaits beneath the stars!\n",
"Climate Change is real and not a distant threat--it's happening now and will only get worse unless we take action.\n",
"\n"
]
}
],
"source": [
"prompt = \"Write a short story about time travel.\"\n",
"print(model.execute(prompt)) "
"prompt = \"\"\"Generate a tweet based on the supplied Keyword. Here are some examples.\n",
"[Keyword]:\n",
"markets\n",
"[Tweet]:\n",
"Take feedback from nature and markets, not from people\n",
"###\n",
"[Keyword]:\n",
"children\n",
"[Tweet]:\n",
"Maybe we die so we can come back as children.\n",
"###\n",
"[Keyword]:\n",
"startups\n",
"[Tweet]: \n",
"Startups should not worry about how to put out fires, they should worry about how to start them.\n",
"###\n",
"[Keyword]:\n",
"climate change\n",
"[Tweet]:\"\"\"\n",
"print(model.execute(prompt))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Tweet Generation"
"#### Email Generation"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Climate change is real and not a distant threat--it's happening now and will affect everyone.\n",
"Dear [Shareholder],\n",
"\n",
"Tesla is taking the world by storm and we'd like to share our exciting news with you. We are committed to creating a future that is powered by renewable energy and we want to make sure that we provide our shareholders with the most innovative and exciting products and services. \n",
"\n",
"We invite you to join us in our mission to make electric cars and energy services accessible to all. Join us as a shareholder of Tesla and receive groundbreaking news and updates about our company and products.\n",
"\n",
"Sincerely, \n",
"Tesla Team\n",
"\n"
]
}
],
"source": [
"prompt = \"\"\"Generate a tweet based on the supplied Keyword. Here are some examples.\n",
"[Keyword]:\n",
"markets\n",
"[Tweet]:\n",
"Take feedback from nature and markets, not from people\n",
"###\n",
"[Keyword]:\n",
"children\n",
"[Tweet]:\n",
"Maybe we die so we can come back as children.\n",
"###\n",
"[Keyword]:\n",
"startups\n",
"[Tweet]: \n",
"Startups should not worry about how to put out fires, they should worry about how to start them.\n",
"###\n",
"[Keyword]:\n",
"climate change\n",
"[Tweet]:\"\"\"\n",
"prompt = \"\"\"Generate an email introducing Tesla to shareholders.\"\"\"\n",
"print(model.execute(prompt))"
]
},
Expand All @@ -427,7 +434,7 @@
"metadata": {},
"source": [
"## Final Comments\n",
"The constructor for `GenerativeAI` accepts parameters that are fed directly to the `generate` method of the underlying model. You can change them as necessary. The default value for `max_new_tokens` (the upper limt on generated answers) has been set to 512."
"The constructor for `GenerativeAI` accepts parameters that are fed directly to the `generate` method of the underlying model. You can change them as necessary. The default value for `max_new_tokens` (the upper limt on generated answers) has been set to 512 and `do_sample=True`. All other paramters (e.g., `num_beams`) have been left at their defaults in the `transformers` library."
]
},
{
Expand All @@ -440,11 +447,23 @@
],
"metadata": {
"kernelspec": {
"display_name": "python3",
"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.16"
}
},
"nbformat": 4,
"nbformat_minor": 2
"nbformat_minor": 4
}

0 comments on commit ca40637

Please sign in to comment.