|
19 | 19 | "- `analyze_protein_sequence` — computes basic biophysical properties\n", |
20 | 20 | "- `summarize_protein_role` — asks the AI to summarize biological context\n", |
21 | 21 | "\n", |
22 | | - "Let's start with installing dependencies and loading API keys below. As we're using the Anthropic native tool use in this example, make sure you have an Anthropic API key." |
| 22 | + "\n", |
| 23 | + ":::{note}\n", |
| 24 | + "To run the code snippets, you can click the **Live Code** button OR the 🚀 icon at the top of the page to launch the page as an interactive Google Colab notebook.\n", |
| 25 | + "\n", |
| 26 | + "Clicking **Live Code** will launch a Binder environment (~1-2 min). \n", |
| 27 | + "Your code will run directly in the page once it's ready.\n", |
| 28 | + ":::\n", |
| 29 | + "\n", |
| 30 | + "If you're using Google Colab to run the notebook, make sure to install the requirements using the cell below. If you're using the \"Live Code\" option, you don't have to do anything." |
23 | 31 | ] |
24 | 32 | }, |
25 | 33 | { |
|
29 | 37 | "metadata": {}, |
30 | 38 | "outputs": [], |
31 | 39 | "source": [ |
32 | | - "!uv pip install anthropic biopython" |
| 40 | + "!pip install anthropic biopython" |
| 41 | + ] |
| 42 | + }, |
| 43 | + { |
| 44 | + "cell_type": "markdown", |
| 45 | + "id": "9199abee", |
| 46 | + "metadata": {}, |
| 47 | + "source": [ |
| 48 | + "Next, you have to setup an Anthropic API key. Your options are as follows:\n", |
| 49 | + "\n", |
| 50 | + "- If you're using the **Live Coding** option, you can input the API key when prompted. No need to add any environment variables.\n", |
| 51 | + "\n", |
| 52 | + "- If you're using **Google Colab** you can either set up the API key as a *colab secret* or in the `.env` file." |
33 | 53 | ] |
34 | 54 | }, |
35 | 55 | { |
|
40 | 60 | "outputs": [], |
41 | 61 | "source": [ |
42 | 62 | "import os\n", |
43 | | - "from dotenv import load_dotenv\n", |
44 | 63 | "\n", |
45 | | - "def get_api_key(provider: str = \"anthropic\"):\n", |
46 | | - " \"\"\"Load Anthropic API key from Colab secrets or environment variable.\"\"\"\n", |
| 64 | + "def get_api_key():\n", |
| 65 | + " \"\"\"Load API key from Colab secrets, environment variable, or user input.\"\"\"\n", |
| 66 | + " \n", |
| 67 | + " # 1. Try Colab secrets\n", |
47 | 68 | " try:\n", |
48 | 69 | " from google.colab import userdata\n", |
49 | | - " return userdata.get(\"ANTHROPIC_API_KEY\")\n", |
| 70 | + " return userdata.get('ANTHROPIC_API_KEY')\n", |
50 | 71 | " except ImportError:\n", |
51 | | - " # Not in Colab — fall back to environment variable\n", |
52 | | - " load_dotenv() \n", |
53 | | - " api_key = os.environ.get(\"ANTHROPIC_API_KEY\")\n", |
54 | | - " if not api_key:\n", |
55 | | - " raise ValueError(\n", |
56 | | - " \"API key not found. Please set the ANTHROPIC_API_KEY environment variable.\\n\"\n", |
57 | | - " \"You can do this by running the following in your terminal. Example:\\n\"\n", |
58 | | - " \" export ANTHROPIC_API_KEY='your-key-here'\\n\"\n", |
59 | | - " \"Or add it to a .env file in your project root.\"\n", |
60 | | - " )\n", |
61 | | - " return api_key\n" |
| 72 | + " pass\n", |
| 73 | + "\n", |
| 74 | + " # 2. Try environment variable / .env file\n", |
| 75 | + " try:\n", |
| 76 | + " from dotenv import load_dotenv\n", |
| 77 | + " load_dotenv()\n", |
| 78 | + " api_key = os.environ.get('ANTHROPIC_API_KEY')\n", |
| 79 | + " if api_key:\n", |
| 80 | + " return api_key\n", |
| 81 | + " except ImportError:\n", |
| 82 | + " pass\n", |
| 83 | + "\n", |
| 84 | + " # 3. Try Binder/Thebe — ask user to input it\n", |
| 85 | + " try:\n", |
| 86 | + " import ipywidgets as widgets\n", |
| 87 | + " from IPython.display import display\n", |
| 88 | + "\n", |
| 89 | + " key_input = widgets.Password(\n", |
| 90 | + " placeholder='Paste your Anthropic API key here',\n", |
| 91 | + " description='API Key:',\n", |
| 92 | + " layout=widgets.Layout(width='400px')\n", |
| 93 | + " )\n", |
| 94 | + " submit = widgets.Button(description='Submit', button_style='primary')\n", |
| 95 | + " output = widgets.Output()\n", |
| 96 | + "\n", |
| 97 | + " result = {'key': None}\n", |
| 98 | + "\n", |
| 99 | + " def on_submit(b):\n", |
| 100 | + " result['key'] = key_input.value\n", |
| 101 | + " os.environ['ANTHROPIC_API_KEY'] = key_input.value\n", |
| 102 | + " with output:\n", |
| 103 | + " print(\"✅ API key set successfully!\")\n", |
| 104 | + "\n", |
| 105 | + " submit.on_click(on_submit)\n", |
| 106 | + " display(widgets.VBox([key_input, submit, output]))\n", |
| 107 | + "\n", |
| 108 | + " # wait for user to submit\n", |
| 109 | + " return result # caller checks result['key']\n", |
| 110 | + "\n", |
| 111 | + " except ImportError:\n", |
| 112 | + " raise ValueError(\n", |
| 113 | + " \"API key not found. Please set ANTHROPIC_API_KEY:\\n\"\n", |
| 114 | + " \" export ANTHROPIC_API_KEY='your-key-here'\"\n", |
| 115 | + " )" |
62 | 116 | ] |
63 | 117 | }, |
64 | 118 | { |
|
84 | 138 | "from Bio.SeqUtils.ProtParam import ProteinAnalysis \n", |
85 | 139 | "\n", |
86 | 140 | "# Connect to the Claude AI model.\n", |
87 | | - "# Make sure you have set your API key as an environment variable\n", |
| 141 | + "# Make sure you have set your API key as an environment variable.\n", |
| 142 | + "# Or set up the API key when prompted.\n", |
88 | 143 | "client = anthropic.Anthropic(api_key=get_api_key())\n", |
89 | 144 | "\n", |
90 | 145 | "print(\"Libraries loaded and client ready.\")" |
|
0 commit comments