File size: 1,664 Bytes
d4c42c0 27426b8 d4c42c0 |
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 |
import base64
import os
import gradio as gr
import openai
# OpenAI API Key
api_key = os.getenv("OPENAI_API_KEY")
client = openai.Client(api_key=api_key)
# Function to encode the image
def encode_image(image_path):
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
def ask_openai_about_image(image_path, user_question):
base64_image = encode_image(image_path)
messages = [
{
"role": "system",
"content": "You are an automotive expert. Your job is to identify details in a given photo of a car.",
},
{
"role": "user",
"content": [
{"type": "text", "text": user_question},
{
"type": "image_url",
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
},
],
},
]
response = client.chat.completions.create(model="gpt-4o", messages=messages)
return response.choices[0].message.content
# Gradio interface
iface = gr.Interface(
fn=ask_openai_about_image,
inputs=[
gr.Image(label="Upload Image of Car", type="filepath"),
gr.Textbox(label="Ask if a specific badge is present"),
],
examples=[
["data/peugeot_206_rear.jpg", "Does this car have the badge 206?"],
["data/peugeot_306_rear.jpg", "Does this car have the badge 106?"],
],
outputs="text",
title="Badge Detector",
description="Upload an image of a car and type your question about the car's badge. You can also select an example image from our dataset.",
)
iface.launch()
|