https://github.com/xtuc/ai-talk-demo
https://github.com/xtuc/ai-talk-demo
Last synced: 6 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/xtuc/ai-talk-demo
- Owner: xtuc
- Created: 2024-03-19T13:22:05.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-19T13:37:25.000Z (over 2 years ago)
- Last Synced: 2025-10-28T11:34:36.951Z (9 months ago)
- Size: 1000 Bytes
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Cloudflare Workers AI demo
## Ask Llama to generate a prompt
```python
import requests
import os
endpoint = "https://api.cloudflare.com/client/v4/accounts/85d0c02b57ed75faf9b18f92d5c01602/ai/run"
url = "{endpoint}/@cf/meta/llama-2-7b-chat-int8".format(endpoint=endpoint)
headers = {
"Content-Type": "application/json",
"Authorization": "Bearer {token}".format(token=os.getenv("API_TOKEN"))
}
payload = {
"messages": [
{
"role": "system",
"content":
"You are a simple and creative assistant that writes a prompt for a stable diffusion model, in a realistic and HD style."
"Reply with the prompt only, avoid 'Here is the prompt...', double quotes and 'Prompt: '."
},
{
"role": "user",
"content": "cute black and gray cat that looks a bit mad at you and is skinny"
},
]
}
response = requests.request("POST", url, json=payload, headers=headers)
response.raise_for_status()
prompt = response.json()["result"]["response"]
print("Prompt: " + prompt)
```
## Generate the image
```py
url = "{endpoint}/@cf/bytedance/stable-diffusion-xl-lightning".format(endpoint=endpoint)
payload = {
"prompt": prompt
}
response = requests.request("POST", url, json=payload, headers=headers)
response.raise_for_status()
with open("out.png", 'wb') as f:
f.write(response.content)
```
## Create an AI gateway
https://dash.cloudflare.com/85d0c02b57ed75faf9b18f92d5c01602/ai/ai-gateway
## Change the endpoint to AI gateway
```
endpoint = "https://gateway.ai.cloudflare.com/v1/85d0c02b57ed75faf9b18f92d5c01602/ai-talk-3/workers-ai"
```