Skip to content

Commit

Permalink
added more AI examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mahtin committed Dec 24, 2023
1 parent 241af4d commit 42d8a62
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 5 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1270,7 +1270,9 @@ It contains a full overview of Cloudflare's GraphQL features and keywords.
See https://blog.cloudflare.com/workers-ai-update-stable-diffusion-code-llama-workers-ai-in-100-cities/ for the introduction,
along with https://developers.cloudflare.com/workers-ai/models/ for the nitty gritty details.

There are two examples for AI calls included with the code.
There are three examples for AI calls included with the code.

Image creation.

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

Translation.

```bash
$ python examples/example_ai_translate.py I\'ll have an order of the moule frites
Je vais avoir une commande des frites de moule
$
```

Speech Recognition with the openai/whisper model.
The following downloads a speech as an mp3 file and passes it to the AI API.
It does a very good job transcribing; however, there's a good chance these mp3 files were use for training.
That said, the example code is here to show how the API works vs testing the AI/ML quality.

```bash
$ python examples/example_ai_speechrecognition.py
mp3 received: length=700367
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.
$
```

This is presently work-in-progress because of the non-Python calling method. The syntax could change in the future.
The examples will be updated.

Expand Down
8 changes: 6 additions & 2 deletions examples/example_ai_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import os
import sys

sys.path.insert(0, os.path.abspath('.'))
import CloudFlare

def find_call(cf, verbs):
Expand All @@ -11,6 +13,8 @@ def find_call(cf, verbs):
# This is not normally needed for other calls
m = cf
for verb in verbs.split('/'):
if verb == '' or verb[0] == ':':
continue
m = getattr(m, verb)
return m

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

sys.stdout.buffer.write(r)

def main():
if sys.argv[1] == '-a':
if len(sys.argv) > 1 and sys.argv[1] == '-a':
del sys.argv[1]
account_name = sys.argv[1]
del sys.argv[1]
Expand Down
108 changes: 108 additions & 0 deletions examples/example_ai_speechrecognition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#!/usr/bin/env python

import os
import sys
import random
import requests

sys.path.insert(0, os.path.abspath('.'))
import CloudFlare

def find_call(cf, verbs):
# So we walk over the @ via a getattr() call.
# We also have to deal with a . in a verb - that does not work in Python. So sad.
# Also, the - is actually an _ in this Python library.
# This is not normally needed for other calls
m = cf
for verb in verbs.split('/'):
if verb == '' or verb[0] == ':':
continue
m = getattr(m, verb)
return m

def doit(account_name, mp3_data):

# Or place these in your cloudflare.cfg file
os.environ['CLOUDFLARE_API_EXTRAS'] = '/accounts/:id/ai/run/@cf/openai/whisper'

# We set the timeout because these AI calls take longer than normal API calls
cf = CloudFlare.CloudFlare(global_request_timeout=120)

try:
if account_name == None or account_name == '':
params = {'per_page': 1}
else:
params = {'name': account_name, 'per_page': 1}
accounts = cf.accounts.get(params=params)
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/accounts %d %s - api call failed' % (e, e))

try:
account_id = accounts[0]['id']
except IndexError:
exit('%s: account name not found' % (account_name))

try:
# This should be easy to call; however, the @ will not work in Python (or many languages)
# r = cf.accounts.ai.run.@cf.openai/whisper(account_id, data=mp3_data)
# We find the endpoint via a quick string search
r = find_call(cf, '/accounts/:id/ai/run/@cf/openai/whisper').post(account_id, data=mp3_data)
except CloudFlare.exceptions.CloudFlareAPIError as e:
exit('/ai.run %d %s - api call failed' % (e, e))

print('%s' % (r['text']))
# words = [word['word'] for word in r['words']]
# print('%s' % ('|'.join(words)))

# based on ... thank you to the author
# https://stackoverflow.com/questions/16694907/download-large-file-in-python-with-requests
def download_file(url, referer, n_requested):
headers={}
headers['Referer'] = referer
headers['User-Agent'] = random.choice([
'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',
'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',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:89.0) Gecko/20100101 Firefox/89.0',
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.111 Safari/537.36',
])

local_filename = '/tmp/' + url.split('/')[-1]
n_received = 0
# NOTE the stream=True parameter below
with requests.get(url, headers=headers, stream=True) as r:
r.raise_for_status()
with open(local_filename, 'wb') as f:
for chunk in r.iter_content(chunk_size=16*1024):
# If you have chunk encoded response uncomment if
# and set chunk_size parameter to None.
#if chunk:
f.write(chunk)
n_received += len(chunk)
if n_received > n_requested:
break
return local_filename

def main():
if len(sys.argv) > 1 and sys.argv[1] == '-a':
del sys.argv[1]
account_name = sys.argv[1]
del sys.argv[1]
else:
account_name = None

if len(sys.argv) > 1:
url = sys.argv[1]
referer = url
else:
url = 'https://www.americanrhetoric.com/mp3clipsXE/barackobama/barackobamapresidentialfarewellARXE.mp3'
referer = 'https://www.americanrhetoric.com/barackobamaspeeches.htm'

# we only grab the first 680KB of the file - that's enough to show working code
mp3_file = download_file(url, referer, 680 * 1024)
mp3_data = open(mp3_file, "rb").read()
print('mp3 received: length=%d' % (len(mp3_data)))

doit(account_name, mp3_data)

if __name__ == '__main__':
main()
8 changes: 6 additions & 2 deletions examples/example_ai_translate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import os
import sys

sys.path.insert(0, os.path.abspath('.'))
import CloudFlare

def find_call(cf, verbs):
Expand All @@ -11,6 +13,8 @@ def find_call(cf, verbs):
# This is not normally needed for other calls
m = cf
for verb in verbs.split('/'):
if verb == '' or verb[0] == ':':
continue
m = getattr(m, verb)
return m

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

print(r['translated_text'])

def main():
if sys.argv[1] == '-a':
if len(sys.argv) > 1 and sys.argv[1] == '-a':
del sys.argv[1]
account_name = sys.argv[1]
del sys.argv[1]
Expand Down

0 comments on commit 42d8a62

Please sign in to comment.