Skip to content

Commit 5e8c6e5

Browse files
Merge pull request #15 from NIGMS/llm-integration
Added util folder with gemini.py, added Gemini to RNA-seq module, and…
2 parents 31c7461 + b0ece0f commit 5e8c6e5

File tree

3 files changed

+157
-4
lines changed

3 files changed

+157
-4
lines changed

GoogleCloud/01-RNA-Seq/RNA-seq.ipynb

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,12 @@
141141
"\n",
142142
"* **Data visualization and interpretation:** The notebook emphasizes creating and interpreting various visualizations, including boxplots, mean-standard deviation plots, PCA plots, histograms of p-values, MA plots, and heatmaps.\n",
143143
"\n",
144-
"* **Using bioinformatics tools:** The notebook shows how to use Nextflow (a workflow management system) for RNA-seq data processing and R for statistical analysis and visualization."
144+
"* **Using bioinformatics tools:** The notebook shows how to use Nextflow (a workflow management system) for RNA-seq data processing and R for statistical analysis and visualization.\n",
145+
"\n",
146+
"<div class=\"alert alert-block alert-success\">\n",
147+
" <i class=\"fa fa-hand-paper-o\" aria-hidden=\"true\"></i>\n",
148+
" <b>Tip: </b> If you're having trouble with any part of this tutorial, feel free to leverage Gemini (Google's advanced generative AI model) at the bottom of this module.\n",
149+
"</div> "
145150
]
146151
},
147152
{
@@ -755,6 +760,41 @@
755760
"<hr style=\"border:2px solid Orange\">"
756761
]
757762
},
763+
{
764+
"cell_type": "markdown",
765+
"id": "4c0cd032-0b9e-4dfe-9708-a3a5e520cf31",
766+
"metadata": {},
767+
"source": [
768+
"## Gemini (Optional)\n",
769+
"--------\n",
770+
"\n",
771+
"If you're having trouble with this submodule (or others within this tutorial), feel free to leverage Gemini by running the cell below. Gemini is Google's advanced generative AI model designed to enhance the capabilities of AI applications across various domains."
772+
]
773+
},
774+
{
775+
"cell_type": "code",
776+
"execution_count": null,
777+
"id": "4b345c9c-13b0-4486-81ee-f12399714550",
778+
"metadata": {},
779+
"outputs": [],
780+
"source": [
781+
"# Ensure you have the necessary libraries installed\n",
782+
"!pip install -q google-generativeai google-cloud-secret-manager\n",
783+
"!pip install -q git+https://github.com/NIGMS/NIGMS-Sandbox-Repository-Template.git#subdirectory=llm_integrations\n",
784+
"!pip install -q ipywidgets\n",
785+
"\n",
786+
"import sys\n",
787+
"import os\n",
788+
"util_path = os.path.join(os.getcwd(), 'util')\n",
789+
"if util_path not in sys.path:\n",
790+
" sys.path.append(util_path)\n",
791+
"\n",
792+
"from gemini import run_gemini_widget, create_gemini_chat_widget \n",
793+
"from IPython.display import display\n",
794+
"\n",
795+
"run_gemini_widget()"
796+
]
797+
},
758798
{
759799
"cell_type": "markdown",
760800
"id": "919d00c4-2247-4f94-aa4f-b7cf6c0334d3",
@@ -777,9 +817,9 @@
777817
],
778818
"metadata": {
779819
"kernelspec": {
780-
"display_name": "conda_python3",
820+
"display_name": "Python 3 (ipykernel) (Local)",
781821
"language": "python",
782-
"name": "conda_python3"
822+
"name": "conda-base-py"
783823
},
784824
"language_info": {
785825
"codemirror_mode": {
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import gemini_helper.build as builder
2+
import google.generativeai as genai
3+
import ipywidgets as widgets
4+
5+
_model = None # Private module-level variable for the model
6+
7+
def _initialize_model():
8+
"""Initializes the Gemini model if not already initialized."""
9+
global _model
10+
if _model is None:
11+
try:
12+
key = builder.get_api_key()
13+
genai.configure(api_key=key)
14+
_model = genai.GenerativeModel("gemini-1.5-flash")
15+
print("Gemini model initialized successfully.")
16+
except Exception as e:
17+
print(f"Error initializing Gemini model: {e}")
18+
_model = None # Ensure it's None if initialization fails
19+
return _model
20+
21+
# --- Widget Creation and Logic ---
22+
def create_gemini_chat_widget():
23+
"""
24+
Creates and returns the Ipywidgets for a Gemini chat interface.
25+
The widgets should be displayed in the calling environment (e.g., Jupyter Notebook).
26+
"""
27+
model = _initialize_model()
28+
if not model:
29+
# If model failed to initialize, return a message widget
30+
error_message = widgets.HTML("Failed to initialize Gemini model. Please check API key and configuration.")
31+
return (error_message,) # Return as a tuple for consistency with display
32+
33+
# Create a text box for input
34+
prompt_input = widgets.Text(
35+
value='',
36+
placeholder='Type your prompt here',
37+
description='Prompt:',
38+
disabled=False
39+
)
40+
41+
# Create an output area for the response
42+
response_output = widgets.Output()
43+
44+
# Create a button to submit the prompt
45+
submit_button = widgets.Button(description="Submit")
46+
47+
# Define the function to handle the button click
48+
def on_submit_button_clicked(b):
49+
# Ensure the model is available (it should be if we reached here)
50+
if not _model:
51+
with response_output:
52+
response_output.clear_output()
53+
print("Model is not available.")
54+
return
55+
56+
with response_output:
57+
response_output.clear_output() # Clear previous output
58+
prompt_text = prompt_input.value
59+
if not prompt_text.strip():
60+
print("Please enter a prompt.")
61+
return
62+
63+
# Indicate processing
64+
print("Generating response...")
65+
try:
66+
# Generate the response using the Gemini model
67+
response = _model.generate_content(prompt_text)
68+
response_output.clear_output() # Clear "Generating response..."
69+
print(response.text)
70+
except Exception as e:
71+
response_output.clear_output() # Clear "Generating response..."
72+
print(f"Error generating content: {e}")
73+
74+
# Attach the click event handler to the button
75+
submit_button.on_click(on_submit_button_clicked)
76+
77+
# Return the widgets to be displayed by the caller
78+
return prompt_input, submit_button, response_output
79+
80+
def run_gemini_widget():
81+
"""
82+
An example "empty" function to create and immediately display the widget.
83+
"""
84+
from IPython.display import display # Import display here, as it's for notebook environment
85+
86+
widgets_to_display = create_gemini_chat_widget()
87+
if widgets_to_display: # Check if widgets were created
88+
# If only one widget is returned (e.g., an error message), display it
89+
if isinstance(widgets_to_display, tuple) and len(widgets_to_display) > 1:
90+
display(*widgets_to_display)
91+
else:
92+
display(widgets_to_display)
93+
else:
94+
print("Could not create Gemini widget.")

GoogleCloud/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,23 @@ Now, you can explore the tutorial and run code blocks from each Jupyter notebook
3737

3838
As seen in the above figure, we have downloaded the data from the NCBI GEO website with accession number GSE173380. Sample data is already provided in the `gs://nigms-sandbox/nosi-und` Google Cloud bucket. There is no need to download the data again unless you want to run the optional Nextflow preprocessing step on the entire dataset (which could be computationally expensive). In the second step of submodule 1 and 2, Nextflow, in collaboration with Google Batch API and Vertex AI, is used to perform the preprocessing. Nextflow works as a workflow manager, which enables scalable and reproducible scientific workflows using containers. Google Life Sciences API is a suite of tools and services for managing, processing, and transforming life science data where it creates and manages clusters and virtual machines. It helps to split the job into multiple jobs and assigns each job to a set of designated virtual machines. Vertex AI, on the other hand, behaves like an interface to manage and execute the process.
3939

40-
After initial preprocessing using Nextflow, further preprocessing, normalization, clustering analysis, differential analysis, and visualization is done in Vertex AI's Jupyter notebook using the R kernel. The results are written in the current working directory inside the Vertex AI instance and transferred to cloud buckets for storage. In the fourth step, we will extract the data from step two and three to use for the multi-omics module's integration analysis. The integrative analysis is also performed using Vertex AI's Jupyter notebook using the R kernel. We will use multi-omics integrative techniques like correlation tests, overlaps and enrichment colocalization, functional and pathway relation, and motifs search. The results from these techniques will be explored in the notebook and then transferred to cloud storage for future reference.
40+
After initial preprocessing using Nextflow, further preprocessing, normalization, clustering analysis, differential analysis, and visualization is done in Vertex AI's Jupyter notebook using the R kernel. The results are written in the current working directory inside the Vertex AI instance and transferred to cloud buckets for storage. In the fourth step, we will extract the data from step two and three to use for the multi-omics module's integration analysis. The integrative analysis is also performed using Vertex AI's Jupyter notebook using the R kernel. We will use multi-omics integrative techniques like correlation tests, overlaps and enrichment colocalization, functional and pathway relation, and motifs search. The results from these techniques will be explored in the notebook and then transferred to cloud storage for future reference.
41+
42+
## Gemini (Optional)
43+
44+
Generative AI is available for this tutorial in the form of Gemini if you would like to use it. To run it, please reference Submodule 1-RNA-Seq, or run the following code within a submodule notebook. You will need to save the util folder with the gemini.py file in the same directory as the notebook where you are running Gemini.
45+
46+
```!pip install -q google-generativeai google-cloud-secret-manager
47+
!pip install -q git+https://github.com/NIGMS/NIGMS-Sandbox-Repository-Template.git#subdirectory=llm_integrations
48+
!pip install -q ipywidgets
49+
50+
import sys
51+
import os
52+
util_path = os.path.join(os.getcwd(), 'util')
53+
if util_path not in sys.path:
54+
sys.path.append(util_path)
55+
56+
from gemini import run_gemini_widget, create_gemini_chat_widget
57+
from IPython.display import display
58+
59+
run_gemini_widget()

0 commit comments

Comments
 (0)