Spaces:
Sleeping
Sleeping
File size: 10,918 Bytes
5e4969d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"!pip install -q -U google-generativeai"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from dotenv import load_dotenv\n",
"import os\n",
"load_dotenv()"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/vasim/.local/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
" from .autonotebook import tqdm as notebook_tqdm\n"
]
}
],
"source": [
"import pathlib\n",
"import textwrap\n",
"\n",
"import google.generativeai as genai\n",
"\n",
"from IPython.display import display\n",
"from IPython.display import Markdown\n",
"\n",
"\n",
"def to_markdown(text):\n",
" text = text.replace('•', ' *')\n",
" return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"genai.configure(api_key=os.getenv(\"GOOGLE_API_KEY\"))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"models/gemini-pro\n",
"models/gemini-pro-vision\n"
]
}
],
"source": [
"for m in genai.list_models():\n",
" if 'generateContent' in m.supported_generation_methods:\n",
" print(m.name)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"model = genai.GenerativeModel('gemini-pro')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 44.3 ms, sys: 615 µs, total: 44.9 ms\n",
"Wall time: 9.75 s\n"
]
}
],
"source": [
"%%time\n",
"response = model.generate_content(\"What is the meaning of life?\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"> The meaning of life is a philosophical question that has been pondered by people for thousands of years. There is no one answer that is universally agreed upon, as the meaning of life is subjective and varies from person to person. However, some common themes that emerge when people discuss the meaning of life include:\n",
"> \n",
"> * **Happiness and fulfillment:** Many people believe that the meaning of life is to be happy and fulfilled. This can involve pursuing activities that bring you joy, such as spending time with loved ones, pursuing your hobbies, or making a difference in the world.\n",
"> * **Purpose and meaning:** Some people believe that the meaning of life is to find a purpose or calling that gives their life meaning. This could involve pursuing a career that you are passionate about, volunteering your time to help others, or raising a family.\n",
"> * **Connection and belonging:** Many people believe that the meaning of life is to connect with others and feel a sense of belonging. This can involve spending time with friends and family, participating in community activities, or contributing to society in a meaningful way.\n",
"> * **Growth and learning:** Some people believe that the meaning of life is to grow and learn as a person. This can involve challenging yourself to try new things, stepping outside of your comfort zone, and expanding your knowledge and understanding of the world.\n",
"> \n",
"> Ultimately, the meaning of life is something that each person must decide for themselves. There is no right or wrong answer, and the meaning of life can change over time as you grow and learn. The important thing is to find a meaning that resonates with you and that gives your life purpose and direction.\n",
"> \n",
"> Here are some additional thoughts on the meaning of life from a variety of sources:\n",
"> \n",
"> * \"The meaning of life is to give life meaning.\" - Viktor Frankl\n",
"> * \"The purpose of life is not to be happy. It is to be useful, to be honorable, to be compassionate, to have it make some difference that you have lived and lived well.\" - Ralph Waldo Emerson\n",
"> * \"When you discover your purpose, you will feel driven to pursue it. Your whole life will change as you make that pursuit your top priority.\" - John Wooden\n",
"> * \"The meaning of life is whatever you assign to it, and the value of life is whatever you choose to make it.\" - John C. Maxwell\n",
"> * \"Life is not a problem to be solved, but a gift to be enjoyed.\" - Dalai Lama"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"to_markdown(response.text)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"safety_ratings {\n",
" category: HARM_CATEGORY_SEXUALLY_EXPLICIT\n",
" probability: NEGLIGIBLE\n",
"}\n",
"safety_ratings {\n",
" category: HARM_CATEGORY_HATE_SPEECH\n",
" probability: NEGLIGIBLE\n",
"}\n",
"safety_ratings {\n",
" category: HARM_CATEGORY_HARASSMENT\n",
" probability: NEGLIGIBLE\n",
"}\n",
"safety_ratings {\n",
" category: HARM_CATEGORY_DANGEROUS_CONTENT\n",
" probability: NEGLIGIBLE\n",
"}"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.prompt_feedback"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[index: 0\n",
"content {\n",
" parts {\n",
" text: \"The meaning of life is a philosophical question that has been pondered by people for thousands of years. There is no one answer that is universally agreed upon, as the meaning of life is subjective and varies from person to person. However, some common themes that emerge when people discuss the meaning of life include:\\n\\n* **Happiness and fulfillment:** Many people believe that the meaning of life is to be happy and fulfilled. This can involve pursuing activities that bring you joy, such as spending time with loved ones, pursuing your hobbies, or making a difference in the world.\\n* **Purpose and meaning:** Some people believe that the meaning of life is to find a purpose or calling that gives their life meaning. This could involve pursuing a career that you are passionate about, volunteering your time to help others, or raising a family.\\n* **Connection and belonging:** Many people believe that the meaning of life is to connect with others and feel a sense of belonging. This can involve spending time with friends and family, participating in community activities, or contributing to society in a meaningful way.\\n* **Growth and learning:** Some people believe that the meaning of life is to grow and learn as a person. This can involve challenging yourself to try new things, stepping outside of your comfort zone, and expanding your knowledge and understanding of the world.\\n\\nUltimately, the meaning of life is something that each person must decide for themselves. There is no right or wrong answer, and the meaning of life can change over time as you grow and learn. The important thing is to find a meaning that resonates with you and that gives your life purpose and direction.\\n\\nHere are some additional thoughts on the meaning of life from a variety of sources:\\n\\n* \\\"The meaning of life is to give life meaning.\\\" - Viktor Frankl\\n* \\\"The purpose of life is not to be happy. It is to be useful, to be honorable, to be compassionate, to have it make some difference that you have lived and lived well.\\\" - Ralph Waldo Emerson\\n* \\\"When you discover your purpose, you will feel driven to pursue it. Your whole life will change as you make that pursuit your top priority.\\\" - John Wooden\\n* \\\"The meaning of life is whatever you assign to it, and the value of life is whatever you choose to make it.\\\" - John C. Maxwell\\n* \\\"Life is not a problem to be solved, but a gift to be enjoyed.\\\" - Dalai Lama\"\n",
" }\n",
" role: \"model\"\n",
"}\n",
"finish_reason: STOP\n",
"safety_ratings {\n",
" category: HARM_CATEGORY_SEXUALLY_EXPLICIT\n",
" probability: NEGLIGIBLE\n",
"}\n",
"safety_ratings {\n",
" category: HARM_CATEGORY_HATE_SPEECH\n",
" probability: NEGLIGIBLE\n",
"}\n",
"safety_ratings {\n",
" category: HARM_CATEGORY_HARASSMENT\n",
" probability: NEGLIGIBLE\n",
"}\n",
"safety_ratings {\n",
" category: HARM_CATEGORY_DANGEROUS_CONTENT\n",
" probability: NEGLIGIBLE\n",
"}\n",
"citation_metadata {\n",
" citation_sources {\n",
" start_index: 1850\n",
" end_index: 2013\n",
" uri: \"https://labor.alaska.gov/dvr/newsletter/dvr-newsletter-2020-03-director.htm\"\n",
" license_: \"\"\n",
" }\n",
" citation_sources {\n",
" start_index: 1916\n",
" end_index: 2045\n",
" uri: \"http://www.marksoffroad.net/Quotes.html\"\n",
" license_: \"\"\n",
" }\n",
"}\n",
"]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"response.candidates"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|