From 642b0e330327e92192060836a58295a441bc1f37 Mon Sep 17 00:00:00 2001 From: jghanaim04 Date: Fri, 30 May 2025 09:17:04 -0400 Subject: [PATCH 1/2] Create genai.py file --- AWS/util/genai.py | 56 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 AWS/util/genai.py diff --git a/AWS/util/genai.py b/AWS/util/genai.py new file mode 100644 index 0000000..c368ab6 --- /dev/null +++ b/AWS/util/genai.py @@ -0,0 +1,56 @@ +import boto3 +import json +import ipywidgets as widgets +from IPython.display import display + +# Setup Model +my_session = boto3.session.Session() +my_region = my_session.region_name +bedrock = boto3.client( + service_name='bedrock-runtime', + region_name=my_region, + ) +def genai(prompt): + # Define model ID + model_id = "meta.llama3-8b-instruct-v1:0" + + # Create request body + body = json.dumps({ + "prompt": prompt, + }) + + # Send request to Bedrock + response = bedrock.invoke_model( + modelId=model_id, + body=body + ) + + # Process the response + response_body = json.loads(response['body'].read()) + generated_text = response_body['generation'] + generated_text = generated_text.lstrip('?').strip() + return generated_text + +# Create widgets +prompt_input = widgets.Text(description="Prompt:") +submit_button = widgets.Button(description="Submit") +output_area = widgets.Output() + +# Define button click event +def on_button_click(b): + with output_area: + output_area.clear_output() + print("Generating response...") + response = genai(prompt_input.value) + print("Response:") + print(response) + +# Attach the event to the button +submit_button.on_click(on_button_click) + +def display_widgets(): + """ + Function to display the widgets in the notebook. + Call this function to show the interactive prompt interface. + """ + display(prompt_input, submit_button, output_area) From accf28fa388915abf6e9e7c0e51816ad4e4f9cd1 Mon Sep 17 00:00:00 2001 From: jghanaim04 Date: Fri, 30 May 2025 09:18:12 -0400 Subject: [PATCH 2/2] Add Bedrock to Submodule 1 and README --- AWS/README.md | 17 ++++++++++ AWS/Submodule_1_background.ipynb | 58 ++++++++++++++++++++++++++++++-- 2 files changed, 73 insertions(+), 2 deletions(-) diff --git a/AWS/README.md b/AWS/README.md index 39f3b8a..cb20f57 100644 --- a/AWS/README.md +++ b/AWS/README.md @@ -79,3 +79,20 @@ Make sure that after you are done with the module, close the tab that appeared w - If you are unable to interact with your s3 bucket using the `aws s3` command, check your `nextflow-service-account` roles. Make sure that you have `Storage Admin` added. - If you are trying to execute a terminal command in a Jupyter code cell and it is not working, make sure that you have an `!` before the command. - e.g., `mkdir example-1` -> `!mkdir example-1` + +## AWS Bedrock (Optional) + +Generative AI is available for this tutorial if you would like to use it. To run it, please reference Submodule 1, or run the following code within a submodule notebook. + +```!pip install -q ipywidgets +import sys +import os +util_path = os.path.join(os.getcwd(), 'util') +if util_path not in sys.path: + sys.path.append(util_path) + +# Import the display_widgets function from your Python file +from genai import display_widgets + +# Call the function to display the widgets +display_widgets() \ No newline at end of file diff --git a/AWS/Submodule_1_background.ipynb b/AWS/Submodule_1_background.ipynb index df88a7f..004cba3 100644 --- a/AWS/Submodule_1_background.ipynb +++ b/AWS/Submodule_1_background.ipynb @@ -44,7 +44,12 @@ "\n", "6. **Introduce the AWS Batch API:** Learners are introduced to the AWS Batch API and its advantages for managing and executing workflows on cloud computing resources.\n", "\n", - "7. **Familiarize learners with Jupyter Notebooks:** The notebook provides instructions on how to navigate and use Jupyter Notebooks, including cell types and execution order." + "7. **Familiarize learners with Jupyter Notebooks:** The notebook provides instructions on how to navigate and use Jupyter Notebooks, including cell types and execution order.\n", + "\n", + "
\n", + " \n", + " Tip: If you're having trouble with any part of this tutorial, feel free to leverage AWS Bedrock (Amazon's advanced generative AI tool) at the bottom of this module.\n", + "
" ] }, { @@ -508,11 +513,60 @@ "\n", "Remember to proceed to the next notebook [`Submodule_2_annotation_only.ipynb`](./Submodule_2_annotation_only.ipynb) or shut down your instance if you are finished." ] + }, + { + "cell_type": "markdown", + "id": "fc886b9b-518c-446d-a703-363b98e72db7", + "metadata": {}, + "source": [ + "## Bedrock (Optional)\n", + "--------\n", + "\n", + "If you're having trouble with this submodule (or others within this tutorial), feel free to leverage Bedrock by running the cell below. Bedrock is a fully managed service that simplifies building and scaling generative AI applications. It provides access to various foundation models (FMs) from Amazon and other AI companies.\n", + "\n", + "Before being able to use the chatbot you must request **Llama 3 8B Instruct** model access through AWS Bedrock. In order to do this follow the instructions to request model access provided in [AWS Bedrock Intro Notebook](https://github.com/STRIDES/NIHCloudLabAWS/blob/main/notebooks/GenAI/AWS_Bedrock_Intro.ipynb). After requesting the Llama 3 8B Instruct access it should only take a minute to get approved. While waiting for model approval, attach the **AmazonBedrockFullAccess** permission to your notebook service role. Once approved run the following code cell to use the model within the notebook. " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2d9781c2-85b1-40de-a1f0-9cacaf5a7ff7", + "metadata": {}, + "outputs": [], + "source": [ + "# Ensure you have the necessary libraries installed\n", + "!pip install -q ipywidgets\n", + "import sys\n", + "import os\n", + "util_path = os.path.join(os.getcwd(), 'util')\n", + "if util_path not in sys.path:\n", + " sys.path.append(util_path)\n", + "\n", + "# Import the display_widgets function from your Python file\n", + "from genai import display_widgets\n", + "\n", + "# Call the function to display the widgets\n", + "display_widgets()" + ] } ], "metadata": { + "kernelspec": { + "display_name": "conda_python3", + "language": "python", + "name": "conda_python3" + }, "language_info": { - "name": "python" + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.17" } }, "nbformat": 4,