eugenecamus's picture
Improve instructions
27426b8 unverified
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()