Skip to content

Commit 2e18580

Browse files
Merge pull request #21 from NIGMS/jghanaim04-patch-1
Bedrock
2 parents 7921a52 + 17d0ef5 commit 2e18580

File tree

3 files changed

+125
-2
lines changed

3 files changed

+125
-2
lines changed

AWS/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,19 @@ These notebooks were designed to be run using a virtual machine on a cloud compu
6464

6565
**[Bonus](Tutorial_5_BonusNotebook.ipynb):** Test your knowledge by filling in the blanks for key Cloud and bioinformatic tasks learned in the other submodules.
6666

67+
## AWS Bedrock (Optional)
6768

69+
Generative AI is available for this tutorial if you would like to use it. To run it, please reference Tutorial 1, or run the following code within a submodule notebook.
70+
71+
```!pip install -q ipywidgets
72+
import sys
73+
import os
74+
util_path = os.path.join(os.getcwd(), 'util')
75+
if util_path not in sys.path:
76+
sys.path.append(util_path)
77+
78+
# Import the display_widgets function from your Python file
79+
from genai import display_widgets
80+
81+
# Call the function to display the widgets
82+
display_widgets()

AWS/Tutorial_1.ipynb

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,12 @@
3434
"* **Assess read quality with FastQC:** Understand how to use FastQC to evaluate the quality of reads and identify potential biases or issues.\n",
3535
"* **Generate a consolidated QC report with MultiQC:** Learn to use MultiQC to combine FastQC results and generate an overview of quality metrics across multiple samples.\n",
3636
"* **Index a transcriptome with Salmon:** Understand the purpose of indexing and learn how to use Salmon to create an index of the reference transcriptome for efficient read mapping.\n",
37-
"* **Map reads to a transcriptome and quantify expression with Salmon:** Learn how to use Salmon to map reads to transcripts and quantify gene expression levels."
37+
"* **Map reads to a transcriptome and quantify expression with Salmon:** Learn how to use Salmon to map reads to transcripts and quantify gene expression levels.\n",
38+
"\n",
39+
"<div class=\"alert alert-block alert-success\">\n",
40+
" <i class=\"fa fa-hand-paper-o\" aria-hidden=\"true\"></i>\n",
41+
" <b>Tip: </b> 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",
42+
"</div> "
3843
]
3944
},
4045
{
@@ -415,11 +420,58 @@
415420
"\n",
416421
"Remember to move to the next notebook or shut down your instance if you are finished."
417422
]
423+
},
424+
{
425+
"cell_type": "markdown",
426+
"metadata": {},
427+
"source": [
428+
"## Bedrock (Optional)\n",
429+
"--------\n",
430+
"\n",
431+
"If you're having trouble with this submodule (or others within this tutorial), feel free to leverage Bedrock by running the cell below. AWS 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",
432+
"\n",
433+
"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. "
434+
]
435+
},
436+
{
437+
"cell_type": "code",
438+
"execution_count": null,
439+
"metadata": {},
440+
"outputs": [],
441+
"source": [
442+
"# Ensure you have the necessary libraries installed\n",
443+
"!pip install -q ipywidgets\n",
444+
"import sys\n",
445+
"import os\n",
446+
"util_path = os.path.join(os.getcwd(), 'util')\n",
447+
"if util_path not in sys.path:\n",
448+
" sys.path.append(util_path)\n",
449+
"\n",
450+
"# Import the display_widgets function from your Python file\n",
451+
"from genai import display_widgets\n",
452+
"\n",
453+
"# Call the function to display the widgets\n",
454+
"display_widgets()"
455+
]
418456
}
419457
],
420458
"metadata": {
459+
"kernelspec": {
460+
"display_name": "conda_python3",
461+
"language": "python",
462+
"name": "conda_python3"
463+
},
421464
"language_info": {
422-
"name": "python"
465+
"codemirror_mode": {
466+
"name": "ipython",
467+
"version": 3
468+
},
469+
"file_extension": ".py",
470+
"mimetype": "text/x-python",
471+
"name": "python",
472+
"nbconvert_exporter": "python",
473+
"pygments_lexer": "ipython3",
474+
"version": "3.10.17"
423475
}
424476
},
425477
"nbformat": 4,

AWS/util/genai.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import boto3
2+
import json
3+
import ipywidgets as widgets
4+
from IPython.display import display
5+
6+
# Setup Model
7+
my_session = boto3.session.Session()
8+
my_region = my_session.region_name
9+
bedrock = boto3.client(
10+
service_name='bedrock-runtime',
11+
region_name=my_region,
12+
)
13+
def genai(prompt):
14+
# Define model ID
15+
model_id = "meta.llama3-8b-instruct-v1:0"
16+
17+
# Create request body
18+
body = json.dumps({
19+
"prompt": prompt,
20+
})
21+
22+
# Send request to Bedrock
23+
response = bedrock.invoke_model(
24+
modelId=model_id,
25+
body=body
26+
)
27+
28+
# Process the response
29+
response_body = json.loads(response['body'].read())
30+
generated_text = response_body['generation']
31+
generated_text = generated_text.lstrip('?').strip()
32+
return generated_text
33+
34+
# Create widgets
35+
prompt_input = widgets.Text(description="Prompt:")
36+
submit_button = widgets.Button(description="Submit")
37+
output_area = widgets.Output()
38+
39+
# Define button click event
40+
def on_button_click(b):
41+
with output_area:
42+
output_area.clear_output()
43+
print("Generating response...")
44+
response = genai(prompt_input.value)
45+
print("Response:")
46+
print(response)
47+
48+
# Attach the event to the button
49+
submit_button.on_click(on_button_click)
50+
51+
def display_widgets():
52+
"""
53+
Function to display the widgets in the notebook.
54+
Call this function to show the interactive prompt interface.
55+
"""
56+
display(prompt_input, submit_button, output_area)

0 commit comments

Comments
 (0)