Skip to content
This repository was archived by the owner on Nov 22, 2024. It is now read-only.

Commit 42d8a62

Browse files
committed
added more AI examples
1 parent 241af4d commit 42d8a62

4 files changed

Lines changed: 137 additions & 5 deletions

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1270,7 +1270,9 @@ It contains a full overview of Cloudflare's GraphQL features and keywords.
12701270
See https://blog.cloudflare.com/workers-ai-update-stable-diffusion-code-llama-workers-ai-in-100-cities/ for the introduction,
12711271
along with https://developers.cloudflare.com/workers-ai/models/ for the nitty gritty details.
12721272

1273-
There are two examples for AI calls included with the code.
1273+
There are three examples for AI calls included with the code.
1274+
1275+
Image creation.
12741276

12751277
```bash
12761278
$ python examples/example_ai_images.py A happy llama running through an orange cloud > /tmp/image.png
@@ -1280,12 +1282,26 @@ $ file /tmp/image.png
12801282
$
12811283
```
12821284

1285+
Translation.
1286+
12831287
```bash
12841288
$ python examples/example_ai_translate.py I\'ll have an order of the moule frites
12851289
Je vais avoir une commande des frites de moule
12861290
$
12871291
```
12881292

1293+
Speech Recognition with the openai/whisper model.
1294+
The following downloads a speech as an mp3 file and passes it to the AI API.
1295+
It does a very good job transcribing; however, there's a good chance these mp3 files were use for training.
1296+
That said, the example code is here to show how the API works vs testing the AI/ML quality.
1297+
1298+
```bash
1299+
$ python examples/example_ai_speechrecognition.py
1300+
mp3 received: length=700367
1301+
My fellow Americans, Michelle and I have been so touched by all the well wishes that we've received over the past few weeks. But tonight, tonight it's my turn to say thanks.
1302+
$
1303+
```
1304+
12891305
This is presently work-in-progress because of the non-Python calling method. The syntax could change in the future.
12901306
The examples will be updated.
12911307

examples/example_ai_images.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import os
44
import sys
5+
6+
sys.path.insert(0, os.path.abspath('.'))
57
import CloudFlare
68

79
def find_call(cf, verbs):
@@ -11,6 +13,8 @@ def find_call(cf, verbs):
1113
# This is not normally needed for other calls
1214
m = cf
1315
for verb in verbs.split('/'):
16+
if verb == '' or verb[0] == ':':
17+
continue
1418
m = getattr(m, verb)
1519
return m
1620

@@ -44,14 +48,14 @@ def doit(account_name, prompt_text):
4448
# This should be easy to call; however, the @ will not work in Python (or many languages)
4549
# r = cf.accounts.ai.run.@cf.stabilityai.stable-diffusion-xl-base-1.0(account_id, data=image_create_data)
4650
# We find the endpoint via a quick string search
47-
r = find_call(cf, 'accounts/ai/run/@cf/stabilityai/stable_diffusion_xl_base_1.0').post(account_id, data=image_create_data)
51+
r = find_call(cf, '/accounts/:id/ai/run/@cf/stabilityai/stable_diffusion_xl_base_1.0').post(account_id, data=image_create_data)
4852
except CloudFlare.exceptions.CloudFlareAPIError as e:
4953
exit('/ai.run %d %s - api call failed' % (e, e))
5054

5155
sys.stdout.buffer.write(r)
5256

5357
def main():
54-
if sys.argv[1] == '-a':
58+
if len(sys.argv) > 1 and sys.argv[1] == '-a':
5559
del sys.argv[1]
5660
account_name = sys.argv[1]
5761
del sys.argv[1]
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#!/usr/bin/env python
2+
3+
import os
4+
import sys
5+
import random
6+
import requests
7+
8+
sys.path.insert(0, os.path.abspath('.'))
9+
import CloudFlare
10+
11+
def find_call(cf, verbs):
12+
# So we walk over the @ via a getattr() call.
13+
# We also have to deal with a . in a verb - that does not work in Python. So sad.
14+
# Also, the - is actually an _ in this Python library.
15+
# This is not normally needed for other calls
16+
m = cf
17+
for verb in verbs.split('/'):
18+
if verb == '' or verb[0] == ':':
19+
continue
20+
m = getattr(m, verb)
21+
return m
22+
23+
def doit(account_name, mp3_data):
24+
25+
# Or place these in your cloudflare.cfg file
26+
os.environ['CLOUDFLARE_API_EXTRAS'] = '/accounts/:id/ai/run/@cf/openai/whisper'
27+
28+
# We set the timeout because these AI calls take longer than normal API calls
29+
cf = CloudFlare.CloudFlare(global_request_timeout=120)
30+
31+
try:
32+
if account_name == None or account_name == '':
33+
params = {'per_page': 1}
34+
else:
35+
params = {'name': account_name, 'per_page': 1}
36+
accounts = cf.accounts.get(params=params)
37+
except CloudFlare.exceptions.CloudFlareAPIError as e:
38+
exit('/accounts %d %s - api call failed' % (e, e))
39+
40+
try:
41+
account_id = accounts[0]['id']
42+
except IndexError:
43+
exit('%s: account name not found' % (account_name))
44+
45+
try:
46+
# This should be easy to call; however, the @ will not work in Python (or many languages)
47+
# r = cf.accounts.ai.run.@cf.openai/whisper(account_id, data=mp3_data)
48+
# We find the endpoint via a quick string search
49+
r = find_call(cf, '/accounts/:id/ai/run/@cf/openai/whisper').post(account_id, data=mp3_data)
50+
except CloudFlare.exceptions.CloudFlareAPIError as e:
51+
exit('/ai.run %d %s - api call failed' % (e, e))
52+
53+
print('%s' % (r['text']))
54+
# words = [word['word'] for word in r['words']]
55+
# print('%s' % ('|'.join(words)))
56+
57+
# based on ... thank you to the author
58+
# https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests
59+
def download_file(url, referer, n_requested):
60+
headers={}
61+
headers['Referer'] = referer
62+
headers['User-Agent'] = random.choice([
63+
'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/16.6 Safari/605.1.15',
64+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.101 Safari/537.36 Edg/91.0.864.48',
65+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
66+
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36',
67+
])
68+
69+
local_filename = '/tmp/' + url.split('/')[-1]
70+
n_received = 0
71+
# NOTE the stream=True parameter below
72+
with requests.get(url, headers=headers, stream=True) as r:
73+
r.raise_for_status()
74+
with open(local_filename, 'wb') as f:
75+
for chunk in r.iter_content(chunk_size=16*1024):
76+
# If you have chunk encoded response uncomment if
77+
# and set chunk_size parameter to None.
78+
#if chunk:
79+
f.write(chunk)
80+
n_received += len(chunk)
81+
if n_received > n_requested:
82+
break
83+
return local_filename
84+
85+
def main():
86+
if len(sys.argv) > 1 and sys.argv[1] == '-a':
87+
del sys.argv[1]
88+
account_name = sys.argv[1]
89+
del sys.argv[1]
90+
else:
91+
account_name = None
92+
93+
if len(sys.argv) > 1:
94+
url = sys.argv[1]
95+
referer = url
96+
else:
97+
url = 'https://www.americanrhetoric.com/mp3clipsXE/barackobama/barackobamapresidentialfarewellARXE.mp3'
98+
referer = 'https://www.americanrhetoric.com/barackobamaspeeches.htm'
99+
100+
# we only grab the first 680KB of the file - that's enough to show working code
101+
mp3_file = download_file(url, referer, 680 * 1024)
102+
mp3_data = open(mp3_file, "rb").read()
103+
print('mp3 received: length=%d' % (len(mp3_data)))
104+
105+
doit(account_name, mp3_data)
106+
107+
if __name__ == '__main__':
108+
main()

examples/example_ai_translate.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import os
44
import sys
5+
6+
sys.path.insert(0, os.path.abspath('.'))
57
import CloudFlare
68

79
def find_call(cf, verbs):
@@ -11,6 +13,8 @@ def find_call(cf, verbs):
1113
# This is not normally needed for other calls
1214
m = cf
1315
for verb in verbs.split('/'):
16+
if verb == '' or verb[0] == ':':
17+
continue
1418
m = getattr(m, verb)
1519
return m
1620

@@ -46,14 +50,14 @@ def doit(account_name, english_text):
4650
# This should be easy to call; however, the @ will not work in Python (or many languages)
4751
# r = cf.accounts.ai.run.@cf.meta.m2m100-1.2b(account_id, data=translate_data)
4852
# We find the endpoint via a quick string search
49-
r = find_call(cf, 'accounts/ai/run/@cf/meta/m2m100_1.2b').post(account_id, data=translate_data)
53+
r = find_call(cf, '/accounts/:id/ai/run/@cf/meta/m2m100_1.2b').post(account_id, data=translate_data)
5054
except CloudFlare.exceptions.CloudFlareAPIError as e:
5155
exit('/ai.run %d %s - api call failed' % (e, e))
5256

5357
print(r['translated_text'])
5458

5559
def main():
56-
if sys.argv[1] == '-a':
60+
if len(sys.argv) > 1 and sys.argv[1] == '-a':
5761
del sys.argv[1]
5862
account_name = sys.argv[1]
5963
del sys.argv[1]

0 commit comments

Comments
 (0)