Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleoconnell-NIH authored Oct 30, 2024
1 parent e3f55b4 commit 4455f31
Showing 1 changed file with 264 additions and 0 deletions.
264 changes: 264 additions & 0 deletions reference_notebooks/gemini-demo-final.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,264 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "98e7ff3b-6e9c-4fad-a7d2-c68a75e22fce",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"!pip install google-generativeai google-cloud-secret-manager"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a8f32d37-05b4-4d6d-82ea-3c0d9892f20d",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from IPython import get_ipython\n",
"from IPython.display import Markdown, display"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "ad718263-dbbc-4675-b3fc-198d9f44e3b3",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"! gcloud services enable secretmanager.googleapis.com"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "08792739-81cb-4d8e-b58c-0ecc836c4d08",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import subprocess\n",
"import json\n",
"import pandas as pd\n",
"def get_projects_dataframe():\n",
" # Command to get projects in JSON format\n",
" command = [\"gcloud\", \"projects\", \"list\", \"--format=json\"]\n",
"\n",
" # Run the command and capture the output\n",
" result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)\n",
"\n",
" # Check for errors\n",
" if result.returncode != 0:\n",
" print(\"An error occurred while running gcloud projects list:\")\n",
" print(result.stderr)\n",
" exit(1)\n",
"\n",
" # Parse the JSON output\n",
" projects_data = json.loads(result.stdout)\n",
"\n",
" # Convert to pandas DataFrame\n",
" df = pd.DataFrame(projects_data)\n",
"\n",
" return df\n",
"\n",
"# Get the DataFrame\n",
"df = get_projects_dataframe()\n",
"\n",
"# Display the DataFrame\n",
"projectNumber = df['projectNumber']\n",
"print(projectNumber)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d19e07d3-4bab-4d9e-ba31-dfa1137ff7d2",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"! gcloud secrets create gemini_api \\\n",
" --replication-policy=\"automatic\""
]
},
{
"cell_type": "markdown",
"id": "8231930e-4f51-4eba-a650-8d5f1bf52407",
"metadata": {},
"source": [
"**Now fetch your API key for Gemini from [this website](https://aistudio.google.com/app/apikey), click `Get API Key`.** and add it to your secret manager secret."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "05ea6adf-5e87-44b1-9996-81dd2226c950",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from google.cloud import secretmanager\n",
"\n",
"def access_secret_version(resource_name):\n",
" client = secretmanager.SecretManagerServiceClient()\n",
" response = client.access_secret_version(request={\"name\": resource_name})\n",
" return response.payload.data.decode(\"UTF-8\")\n",
"\n",
"\n",
"# Your secret's resource name\n",
"resource_name = f\"projects/{projectNumber[0]}/secrets/gemini_api/versions/1\"\n",
"\n",
"# Access the secret\n",
"apiKey = access_secret_version(resource_name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "e5874884-8e9a-44db-b929-e0bcb09a29e3",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import google.generativeai as genai\n",
"\n",
"genai.configure(api_key=apiKey)\n",
"\n",
"model = genai.GenerativeModel(\"gemini-1.5-flash\")\n",
"# response = model.generate_content(\"Explain how AI works\")\n",
"# print(response.text)\n",
"welcome_prompt = \"You are a excellent developer in life science and healthcare research. The mission is to advise researchers with limited coding experience. Please format your response in markdown by default.\""
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "50ee016a-b3e3-4f71-8191-5f52db57a193",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"def explain(cell_number):\n",
" \"\"\"Return the content of the specified cell number.\"\"\"\n",
" ipython = get_ipython() # Get the current IPython instance\n",
" \n",
" # Ensure the cell number is valid\n",
" if cell_number < len(ipython.user_ns['In']):\n",
" content = \"Based on the input. Please concisely comment this code to explain each line. Ignore the run cell magic and just focus on the code or error \" + ipython.user_ns['In'][cell_number] + \"Do not add additional code\"\n",
" response = model.generate_content(welcome_prompt + content)\n",
"\n",
" return display(Markdown(response.text))\n",
" # Return the content of the cell\n",
" #return ipython.user_ns['In'][cell_number]\n",
" else:\n",
" # Error message for invalid cell number\n",
" return \"Cell number out of range.\"\n",
" \n",
"\n",
" \n",
"def modify(cell_number,modification):\n",
" \"\"\"Return the content of the specified cell number.\"\"\"\n",
" ipython = get_ipython() # Get the current IPython instance\n",
" \n",
" # Ensure the cell number is valid\n",
" if cell_number < len(ipython.user_ns['In']):\n",
" prompt = \"Please modify the code \" + ipython.user_ns['In'][cell_number] + \" to accomplish \" + modification + \"Ignore the run cell magic and just focus on the code. Assume all library has been loaded. Return only code.\"\n",
" response = model.generate_content(prompt)\n",
"\n",
" return create_new_cell(\"%%R\\n\\n\" + response.text)\n",
"\n",
" # Return the content of the cell\n",
" #return ipython.user_ns['In'][cell_number]\n",
" else:\n",
" # Error message for invalid cell number\n",
" return \"Cell number out of range.\"\n",
"\n",
"def propose(suggest):\n",
" response = model.generate_content(\"Please suggest code to accomplish \" + suggest + \". Return only code.\")\n",
" #print(response.choices[0].message.content)\n",
" #return display(Markdown(response.choices[0].message.content))\n",
" return create_new_cell(\"\\n\\n\" + response.text)\n",
" \n",
"def create_new_cell(contents):\n",
" shell = get_ipython()\n",
" shell.set_next_input(contents, replace=False)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "a6ec0696-fb7f-485a-ba2a-a6e7e38c8d82",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"propose('write me code for openai')"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "2f8b1038-03eb-4622-a100-c40a01d27730",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"propose(\"I need to find the 25 most signficantly upregulated genes from a dataframe called gene. Here are the column names: ENTREZID, SYMBOL, GENENAME, logFC AveExpr, t, P.Value, adj.P.Val\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3316d3c1-c178-4fd4-971c-18f5a8aeb957",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"propose(\"help me develop a nextflow workflow\")"
]
}
],
"metadata": {
"environment": {
"kernel": "conda-base-py",
"name": "workbench-notebooks.m124",
"type": "gcloud",
"uri": "us-docker.pkg.dev/deeplearning-platform-release/gcr.io/workbench-notebooks:m124"
},
"kernelspec": {
"display_name": "Python 3 (ipykernel) (Local)",
"language": "python",
"name": "conda-base-py"
},
"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.10.14"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit 4455f31

Please sign in to comment.