|
| 1 | +import base64 |
| 2 | +from openai import OpenAI |
| 3 | +from javelin_sdk import JavelinClient, JavelinConfig |
| 4 | +import os |
| 5 | +import dotenv |
| 6 | + |
| 7 | +dotenv.load_dotenv() |
| 8 | + |
| 9 | +# Load API keys from environment variables |
| 10 | +JAVELIN_API_KEY = os.getenv("JAVELIN_API_KEY") |
| 11 | +LLM_API_KEY = os.getenv("LLM_API_KEY") |
| 12 | +BASE_URL = os.getenv("BASE_URL") |
| 13 | + |
| 14 | +# Configure Javelin |
| 15 | +config = JavelinConfig( |
| 16 | + base_url=BASE_URL, |
| 17 | + javelin_api_key=JAVELIN_API_KEY, |
| 18 | + llm_api_key=LLM_API_KEY, |
| 19 | +) |
| 20 | +javelin_client = JavelinClient(config) |
| 21 | + |
| 22 | +client = OpenAI(api_key=LLM_API_KEY) |
| 23 | +route_name = "openai_univ" # define your universal route name here |
| 24 | +javelin_client.register_openai(client, route_name=route_name) |
| 25 | + |
| 26 | +# --- Example 1: Edit an image --- |
| 27 | +# result = client.images.edit( |
| 28 | +# model="gpt-image-1", |
| 29 | +# image=open("examples/dog.png", "rb"), |
| 30 | +# prompt="an angry dog" |
| 31 | +# ) |
| 32 | +# image_base64 = result.data[0].b64_json |
| 33 | +# image_bytes = base64.b64decode(image_base64) |
| 34 | +# with open("angry_dog_2.png", "wb") as f: |
| 35 | +# f.write(image_bytes) |
| 36 | + |
| 37 | +# --- Example 2: Create image variations --- |
| 38 | +# response = client.images.create_variation( |
| 39 | +# image=open("examples/dog.png", "rb"), |
| 40 | +# n=2, |
| 41 | +# size="1024x1024" |
| 42 | +# ) |
| 43 | +# for idx, img_data in enumerate(response.data): |
| 44 | +# image_bytes = base64.b64decode(img_data.b64_json) |
| 45 | +# with open(f"dog_variation_{idx+1}.png", "wb") as f: |
| 46 | +# f.write(image_bytes) |
| 47 | + |
| 48 | +# --- Example 3: Generate an image --- |
| 49 | +img = client.images.generate( |
| 50 | + model="gpt-image-1", |
| 51 | + prompt="A friendly dog playing in a park.", |
| 52 | + n=1, |
| 53 | + size="1024x1024" |
| 54 | +) |
| 55 | + |
| 56 | +image_bytes = base64.b64decode(img.data[0].b64_json) |
| 57 | +with open("generated_image.png", "wb") as f: |
| 58 | + f.write(image_bytes) |
0 commit comments