eugenecamus
commited on
init app
Browse files- app.py +55 -0
- data/peugeot_206_rear.jpg +0 -0
- data/peugeot_306_rear.jpg +0 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import base64
|
2 |
+
import os
|
3 |
+
import gradio as gr
|
4 |
+
import openai
|
5 |
+
|
6 |
+
# OpenAI API Key
|
7 |
+
api_key = os.getenv("OPENAI_API_KEY")
|
8 |
+
client = openai.Client(api_key=api_key)
|
9 |
+
|
10 |
+
# Function to encode the image
|
11 |
+
def encode_image(image_path):
|
12 |
+
with open(image_path, "rb") as image_file:
|
13 |
+
return base64.b64encode(image_file.read()).decode("utf-8")
|
14 |
+
|
15 |
+
|
16 |
+
def ask_openai_about_image(image_path, user_question):
|
17 |
+
base64_image = encode_image(image_path)
|
18 |
+
messages = [
|
19 |
+
{
|
20 |
+
"role": "system",
|
21 |
+
"content": "You are an automotive expert. Your job is to identify details in a given photo of a car.",
|
22 |
+
},
|
23 |
+
{
|
24 |
+
"role": "user",
|
25 |
+
"content": [
|
26 |
+
{"type": "text", "text": user_question},
|
27 |
+
{
|
28 |
+
"type": "image_url",
|
29 |
+
"image_url": {"url": f"data:image/jpeg;base64,{base64_image}"},
|
30 |
+
},
|
31 |
+
],
|
32 |
+
},
|
33 |
+
]
|
34 |
+
|
35 |
+
response = client.chat.completions.create(model="gpt-4o", messages=messages)
|
36 |
+
return response.choices[0].message.content
|
37 |
+
|
38 |
+
|
39 |
+
# Gradio interface
|
40 |
+
iface = gr.Interface(
|
41 |
+
fn=ask_openai_about_image,
|
42 |
+
inputs=[
|
43 |
+
gr.Image(label="Upload Image of Car", type="filepath"),
|
44 |
+
gr.Textbox(label="Ask if a specific badge is present"),
|
45 |
+
],
|
46 |
+
examples=[
|
47 |
+
["data/peugeot_206_rear.jpg", "Does this car have the badge 206?"],
|
48 |
+
["data/peugeot_306_rear.jpg", "Does this car have the badge 106?"],
|
49 |
+
],
|
50 |
+
outputs="text",
|
51 |
+
title="Badge Detector",
|
52 |
+
description="Upload an image of a car and type your question about the car. You can also select an example image from our dataset.",
|
53 |
+
)
|
54 |
+
|
55 |
+
iface.launch()
|
data/peugeot_206_rear.jpg
ADDED
data/peugeot_306_rear.jpg
ADDED
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
gradio
|
2 |
+
openai
|