Skip to content

Instantly share code, notes, and snippets.

@elchead
Last active May 19, 2025 10:46
Show Gist options
  • Save elchead/14785c17994e8c6aaac1c57647719db5 to your computer and use it in GitHub Desktop.
Save elchead/14785c17994e8c6aaac1c57647719db5 to your computer and use it in GitHub Desktop.
from openai import OpenAI
import base64
import os
import time
# docker run --pull=always -p 8080:8080 ghcr.io/edgelesssys/privatemode/privatemode-proxy:latest
# PRIVATEMODE_API_KEY=<> uv run --with openai gemma-vision.py
api_key = os.environ.get("PRIVATEMODE_API_KEY") # insert
api_base = "http://localhost:8080/v1"
image_path = (
"" # insert
)
client = OpenAI(
api_key=api_key,
base_url=api_base,
)
def encode_image_to_base64(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
if not os.path.exists(image_path):
print(f"Error: Image file not found at {image_path}")
exit(1)
base64_image = encode_image_to_base64(image_path)
start_time = time.time()
chat_response = client.chat.completions.create(
model="google/gemma-3-27b-it",
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "What's in this image?"},
{
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{base64_image}"},
},
],
}
],
)
end_time = time.time()
response_time = end_time - start_time
print("Chat completion output:", chat_response.choices[0].message.content)
print(f"Request-to-response time: {response_time:.2f} seconds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment