Spaces:
Running
Running
Create app.py
Browse filesgradio
openai
Pillow
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import os
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
# Set your OpenAI API key (if using GPT-4 Vision)
|
| 7 |
+
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 8 |
+
|
| 9 |
+
def identify_vehicle(image):
|
| 10 |
+
# Send the image to OpenAI Vision or another model
|
| 11 |
+
response = openai.ChatCompletion.create(
|
| 12 |
+
model="gpt-4-vision-preview",
|
| 13 |
+
messages=[
|
| 14 |
+
{
|
| 15 |
+
"role": "user",
|
| 16 |
+
"content": [
|
| 17 |
+
{"type": "text", "text": "Identify the car make/model/year or parts in this image."},
|
| 18 |
+
{"type": "image_url", "image_url": {"url": "data:image/jpeg;base64," + image_to_base64(image)}}
|
| 19 |
+
]
|
| 20 |
+
}
|
| 21 |
+
],
|
| 22 |
+
max_tokens=300
|
| 23 |
+
)
|
| 24 |
+
return response.choices[0].message["content"]
|
| 25 |
+
|
| 26 |
+
def image_to_base64(image):
|
| 27 |
+
import base64
|
| 28 |
+
from io import BytesIO
|
| 29 |
+
buffered = BytesIO()
|
| 30 |
+
image.save(buffered, format="JPEG")
|
| 31 |
+
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 32 |
+
|
| 33 |
+
interface = gr.Interface(
|
| 34 |
+
fn=identify_vehicle,
|
| 35 |
+
inputs=gr.Image(type="pil", label="Upload Car or Part Photo"),
|
| 36 |
+
outputs=gr.Textbox(label="Identification Result"),
|
| 37 |
+
title="VehicIdent AI",
|
| 38 |
+
description="Upload a car or part photo. The AI will identify make, model, year, or visible components."
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
interface.launch()
|