-
Notifications
You must be signed in to change notification settings - Fork 32
/
image_upload.py
39 lines (33 loc) · 1.38 KB
/
image_upload.py
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
from io import BytesIO
import PIL
import requests
import vertexai
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk import trace as trace_sdk
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
from vertexai.generative_models import GenerativeModel, Image, Part
from openinference.instrumentation import TraceConfig
from openinference.instrumentation.vertexai import VertexAIInstrumentor
endpoint = "http://127.0.0.1:4317"
tracer_provider = trace_sdk.TracerProvider()
tracer_provider.add_span_processor(SimpleSpanProcessor(OTLPSpanExporter(endpoint)))
tracer_provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
VertexAIInstrumentor().instrument(
tracer_provider=tracer_provider,
config=TraceConfig(base64_image_max_length=200_000),
)
vertexai.init(location="us-central1")
model = GenerativeModel("gemini-1.5-flash")
url = "https://nextml.github.io/caption-contest-data/cartoons/893.jpg"
image = PIL.Image.open(BytesIO(requests.get(url).content))
image.thumbnail((512, 512))
arr = BytesIO()
image.save(arr, format=image.format)
if __name__ == "__main__":
response = model.generate_content(
[
"What's funny about this caption?\n\nThe seller isn't willing to come down.",
Part.from_image(Image.from_bytes(arr.getvalue())),
]
)
print(response)