Repository navigation
Expand file tree
/
Copy pathpython_openai.py
More file actions
59 lines (52 loc) · 1.49 KB
/
Copy pathpython_openai.py
File metadata and controls
59 lines (52 loc) · 1.49 KB
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
"""Images and chat through the OpenAI SDK, video over plain HTTP.
pip install openai requests
export SPICYAPI_KEY=sk-spicy-...
"""
import os
import time
import requests
from openai import OpenAI
KEY = os.environ["SPICYAPI_KEY"]
API = "https://api.spicyapi.com/v1"
client = OpenAI(api_key=KEY, base_url=API)
img = client.images.generate(
model="spicy-image-1",
prompt="a woman on a beach at golden hour, photorealistic",
size="1024*1024",
n=1,
)
image_url = img.data[0].url
print("image:", image_url)
# Video is not an OpenAI endpoint, so call it directly.
headers = {"Authorization": f"Bearer {KEY}"}
task = requests.post(
f"{API}/videos/generations",
headers=headers,
json={
"model": "spicy-motion-2",
"prompt": "slow build, steady rhythm",
"image_url": image_url,
"resolution": "720P",
"duration": 5,
},
timeout=60,
)
task.raise_for_status()
task_id = task.json()["id"]
while True:
res = requests.get(f"{API}/videos/tasks/{task_id}", headers=headers, timeout=60).json()
print("status:", res["status"])
if res["status"] == "succeeded":
print("video:", res["output"]["video_url"])
break
if res["status"] == "failed":
raise SystemExit(res["error"])
time.sleep(12)
stream = client.chat.completions.create(
model="spicy-chat-1",
messages=[{"role": "user", "content": "hey"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()