Spaces:
Running
Running
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import base64
|
| 2 |
+
import os
|
| 3 |
+
from io import BytesIO
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import gradio as gr
|
| 6 |
+
from openai import OpenAI
|
| 7 |
+
|
| 8 |
+
client = OpenAI()
|
| 9 |
+
|
| 10 |
+
def image_to_base64(image):
|
| 11 |
+
buffered = BytesIO()
|
| 12 |
+
image.save(buffered, format="PNG")
|
| 13 |
+
img_bytes = buffered.getvalue()
|
| 14 |
+
base64_image = base64.b64encode(img_bytes).decode("utf-8")
|
| 15 |
+
return f"data:image/png;base64,{base64_image}"
|
| 16 |
+
|
| 17 |
+
def identify_vehicle(image):
|
| 18 |
+
try:
|
| 19 |
+
base64_img = image_to_base64(image)
|
| 20 |
+
|
| 21 |
+
response = client.chat.completions.create(
|
| 22 |
+
model="gpt-4-vision-preview",
|
| 23 |
+
messages=[
|
| 24 |
+
{
|
| 25 |
+
"role": "user",
|
| 26 |
+
"content": [
|
| 27 |
+
{"type": "text", "text": "What is in this image? Identify vehicle make, model, year, and part name if possible."},
|
| 28 |
+
{"type": "image_url", "image_url": {"url": base64_img}}
|
| 29 |
+
]
|
| 30 |
+
}
|
| 31 |
+
],
|
| 32 |
+
max_tokens=500
|
| 33 |
+
)
|
| 34 |
+
return response.choices[0].message.content
|
| 35 |
+
|
| 36 |
+
except Exception as e:
|
| 37 |
+
return f"Error: {e}"
|
| 38 |
+
|
| 39 |
+
iface = gr.Interface(
|
| 40 |
+
fn=identify_vehicle,
|
| 41 |
+
inputs=gr.Image(type="pil", label="Upload a car or part photo"),
|
| 42 |
+
outputs="text",
|
| 43 |
+
title="AutoIQ",
|
| 44 |
+
description="Upload a car or part photo. AutoIQ will identify it using GPT-4 Vision."
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
iface.launch()
|