gmshroff commited on
Commit
b30dfe7
1 Parent(s): 3732e41

error handling in Gemini call

Browse files
Files changed (2) hide show
  1. library.ipynb +5 -2
  2. test.ipynb +0 -367
library.ipynb CHANGED
@@ -24,8 +24,11 @@
24
  " # return response\n",
25
  " genai.configure(api_key=key)\n",
26
  " model = genai.GenerativeModel('gemini-pro')\n",
27
- " response = model.generate_content(text)\n",
28
- " return response.text"
 
 
 
29
  ]
30
  },
31
  {
 
24
  " # return response\n",
25
  " genai.configure(api_key=key)\n",
26
  " model = genai.GenerativeModel('gemini-pro')\n",
27
+ " try:\n",
28
+ " response = model.generate_content(text)\n",
29
+ " except Exception as e:\n",
30
+ " return -1,str(e)\n",
31
+ " return 0,response.text"
32
  ]
33
  },
34
  {
test.ipynb DELETED
@@ -1,367 +0,0 @@
1
- {
2
- "cells": [
3
- {
4
- "cell_type": "code",
5
- "execution_count": null,
6
- "metadata": {
7
- "tags": []
8
- },
9
- "outputs": [],
10
- "source": [
11
- "import requests\n",
12
- "import json\n",
13
- "from urllib.request import urlretrieve\n",
14
- "import pandas as pd\n",
15
- "import time\n",
16
- "from allkeys import OPENAIKEY, GEMENIKEY"
17
- ]
18
- },
19
- {
20
- "cell_type": "code",
21
- "execution_count": null,
22
- "metadata": {},
23
- "outputs": [],
24
- "source": [
25
- "import anvil.server\n",
26
- "anvil.server.connect('PLMOIU5VCGGUOJH2XORIBWV3-ZXZVFLWX7QFIIAF4')"
27
- ]
28
- },
29
- {
30
- "cell_type": "code",
31
- "execution_count": null,
32
- "metadata": {},
33
- "outputs": [],
34
- "source": [
35
- "def fetch_result(task_id):\n",
36
- " while True:\n",
37
- " result=anvil.server.call('poll',task_id)\n",
38
- " if result!='In Progress' or result=='No such task': break\n",
39
- " else: \n",
40
- " time.sleep(1)\n",
41
- " print(result)\n",
42
- " print(result)\n",
43
- " return result"
44
- ]
45
- },
46
- {
47
- "cell_type": "code",
48
- "execution_count": null,
49
- "metadata": {},
50
- "outputs": [],
51
- "source": [
52
- "text='write a python function to compute the nth digit of pi'\n",
53
- "model='gpt-3.5-turbo'"
54
- ]
55
- },
56
- {
57
- "cell_type": "code",
58
- "execution_count": null,
59
- "metadata": {},
60
- "outputs": [],
61
- "source": [
62
- "task_id=anvil.server.call('launch','call_gemini',text,GEMENIKEY)"
63
- ]
64
- },
65
- {
66
- "cell_type": "code",
67
- "execution_count": null,
68
- "metadata": {},
69
- "outputs": [],
70
- "source": [
71
- "task_id=anvil.server.call('launch','call_gpt',text,OPENAIKEY,model)"
72
- ]
73
- },
74
- {
75
- "cell_type": "code",
76
- "execution_count": null,
77
- "metadata": {},
78
- "outputs": [],
79
- "source": [
80
- "fetch_result(task_id)"
81
- ]
82
- },
83
- {
84
- "cell_type": "code",
85
- "execution_count": null,
86
- "metadata": {},
87
- "outputs": [],
88
- "source": [
89
- "print(result)"
90
- ]
91
- },
92
- {
93
- "cell_type": "code",
94
- "execution_count": null,
95
- "metadata": {},
96
- "outputs": [],
97
- "source": [
98
- "print(result[1],end='\\n')"
99
- ]
100
- },
101
- {
102
- "cell_type": "code",
103
- "execution_count": null,
104
- "metadata": {},
105
- "outputs": [],
106
- "source": [
107
- "import pathlib\n",
108
- "import textwrap\n",
109
- "from IPython.display import display\n",
110
- "from IPython.display import Markdown\n",
111
- "\n",
112
- "def to_markdown(text):\n",
113
- " text = text.replace('•', ' *')\n",
114
- " return Markdown(textwrap.indent(text, '> ', predicate=lambda _: True))"
115
- ]
116
- },
117
- {
118
- "cell_type": "code",
119
- "execution_count": null,
120
- "metadata": {},
121
- "outputs": [],
122
- "source": [
123
- "prompt='write code that defines a transformer network from scratch in pytorch'"
124
- ]
125
- },
126
- {
127
- "cell_type": "code",
128
- "execution_count": null,
129
- "metadata": {},
130
- "outputs": [],
131
- "source": [
132
- "response=anvil.server.call('call_gemini',prompt)"
133
- ]
134
- },
135
- {
136
- "cell_type": "code",
137
- "execution_count": null,
138
- "metadata": {},
139
- "outputs": [],
140
- "source": [
141
- "anvil.server.call('encode_anvil',prompt)"
142
- ]
143
- },
144
- {
145
- "cell_type": "code",
146
- "execution_count": null,
147
- "metadata": {},
148
- "outputs": [],
149
- "source": [
150
- "to_markdown(response)"
151
- ]
152
- },
153
- {
154
- "cell_type": "code",
155
- "execution_count": null,
156
- "metadata": {},
157
- "outputs": [],
158
- "source": [
159
- "anvil.server.call('encode_anvil','I am a robot')[0]"
160
- ]
161
- },
162
- {
163
- "cell_type": "code",
164
- "execution_count": null,
165
- "metadata": {},
166
- "outputs": [],
167
- "source": [
168
- "def encode(text,server='local'):\n",
169
- " headers = {'Content-Type': 'application/json'}\n",
170
- " if server=='local': url='http://127.0.0.1:7860/encode'\n",
171
- " elif server=='hf': url='https://huggingface.co/spaces/gmshroff/gmserver/encode'\n",
172
- " body={'text':text}\n",
173
- " response=requests.post(url=url,data=json.dumps(body),headers = {'Content-Type': 'application/json'})\n",
174
- " return response\n",
175
- " return json.loads(response.content)['embedding']"
176
- ]
177
- },
178
- {
179
- "cell_type": "code",
180
- "execution_count": null,
181
- "metadata": {},
182
- "outputs": [],
183
- "source": [
184
- "response=encode('I am a robot',server='local')"
185
- ]
186
- },
187
- {
188
- "cell_type": "code",
189
- "execution_count": null,
190
- "metadata": {},
191
- "outputs": [],
192
- "source": [
193
- "response.content"
194
- ]
195
- },
196
- {
197
- "cell_type": "code",
198
- "execution_count": null,
199
- "metadata": {},
200
- "outputs": [],
201
- "source": [
202
- "headers = {'Content-Type': 'application/json'}\n",
203
- "# url='http://127.0.0.1:5000/run'\n",
204
- "url='https://huggingface.co/spaces/gmshroff/gmserver/'\n",
205
- "# url='http://127.0.0.1:7860/run'\n",
206
- "# body={\"script\":\"python update_valdata.py\"}\n",
207
- "# body={\"script\":\"pwd\"}"
208
- ]
209
- },
210
- {
211
- "cell_type": "code",
212
- "execution_count": null,
213
- "metadata": {},
214
- "outputs": [],
215
- "source": [
216
- "response=requests.get(url=url)"
217
- ]
218
- },
219
- {
220
- "cell_type": "code",
221
- "execution_count": null,
222
- "metadata": {},
223
- "outputs": [],
224
- "source": [
225
- "response.content"
226
- ]
227
- },
228
- {
229
- "cell_type": "code",
230
- "execution_count": null,
231
- "metadata": {},
232
- "outputs": [],
233
- "source": [
234
- "# url='http://127.0.0.1:7860/encode'\n",
235
- "body={'text':'I am very good'}\n"
236
- ]
237
- },
238
- {
239
- "cell_type": "code",
240
- "execution_count": null,
241
- "metadata": {},
242
- "outputs": [],
243
- "source": [
244
- "response=requests.post(url=url,data=json.dumps(body),headers = {'Content-Type': 'application/json'})\n"
245
- ]
246
- },
247
- {
248
- "cell_type": "code",
249
- "execution_count": null,
250
- "metadata": {},
251
- "outputs": [],
252
- "source": [
253
- "url"
254
- ]
255
- },
256
- {
257
- "cell_type": "code",
258
- "execution_count": null,
259
- "metadata": {},
260
- "outputs": [],
261
- "source": [
262
- "print(response)"
263
- ]
264
- },
265
- {
266
- "cell_type": "code",
267
- "execution_count": null,
268
- "metadata": {},
269
- "outputs": [],
270
- "source": [
271
- "print(response.__dict__)"
272
- ]
273
- },
274
- {
275
- "cell_type": "code",
276
- "execution_count": null,
277
- "metadata": {},
278
- "outputs": [],
279
- "source": [
280
- "print(json.loads(response.content)['embedding'])"
281
- ]
282
- },
283
- {
284
- "cell_type": "code",
285
- "execution_count": null,
286
- "metadata": {
287
- "tags": []
288
- },
289
- "outputs": [],
290
- "source": [
291
- "urlretrieve(url='http://127.0.0.1:7860/file/data.csv',filename='./returned_file.csv')"
292
- ]
293
- },
294
- {
295
- "cell_type": "code",
296
- "execution_count": null,
297
- "metadata": {},
298
- "outputs": [],
299
- "source": [
300
- "df=pd.read_parquet('/tmp/validation_subset_int8.parquet')"
301
- ]
302
- },
303
- {
304
- "cell_type": "code",
305
- "execution_count": null,
306
- "metadata": {},
307
- "outputs": [],
308
- "source": [
309
- "import torch\n",
310
- "import torch.nn as nn\n",
311
- "import torch.nn.functional as F\n",
312
- "\n",
313
- "class Transformer(nn.Module):\n",
314
- " def __init__(self, d_model, nhead, num_encoder_layers, num_decoder_layers, dim_feedforward, dropout=0.1):\n",
315
- " super(Transformer, self).__init__()\n",
316
- " self.transformer = nn.Transformer(d_model, nhead, num_encoder_layers, num_decoder_layers, dim_feedforward, dropout)\n",
317
- "\n",
318
- " def forward(self, src, tgt):\n",
319
- " output = self.transformer(src, tgt)\n",
320
- " return output\n",
321
- "\n",
322
- "# Example usage:\n",
323
- "# Define the model parameters\n",
324
- "d_model = 512\n",
325
- "nhead = 8\n",
326
- "num_encoder_layers = 6\n",
327
- "num_decoder_layers = 6\n",
328
- "dim_feedforward = 2048\n",
329
- "dropout = 0.1\n",
330
- "\n",
331
- "# Initialize the model\n",
332
- "model = Transformer(d_model, nhead, num_encoder_layers, num_decoder_layers, dim_feedforward, dropout)\n",
333
- "\n",
334
- "# Generate some sample data\n",
335
- "src = torch.rand(10, 32, 512)\n",
336
- "tgt = torch.rand(20, 32, 512)\n",
337
- "\n",
338
- "# Pass the data through the model\n",
339
- "output = model(src, tgt)\n",
340
- "\n",
341
- "# Print the output shape\n",
342
- "print(output.shape)"
343
- ]
344
- }
345
- ],
346
- "metadata": {
347
- "kernelspec": {
348
- "display_name": "Python 3 (ipykernel)",
349
- "language": "python",
350
- "name": "python3"
351
- },
352
- "language_info": {
353
- "codemirror_mode": {
354
- "name": "ipython",
355
- "version": 3
356
- },
357
- "file_extension": ".py",
358
- "mimetype": "text/x-python",
359
- "name": "python",
360
- "nbconvert_exporter": "python",
361
- "pygments_lexer": "ipython3",
362
- "version": "3.10.13"
363
- }
364
- },
365
- "nbformat": 4,
366
- "nbformat_minor": 4
367
- }