Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,26 +5,24 @@ import gradio as gr
|
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
-
# Load OpenAI key
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
| 11 |
-
# Encode the image to base64
|
| 12 |
def image_to_base64(image):
|
| 13 |
buffered = BytesIO()
|
| 14 |
image.save(buffered, format="PNG")
|
| 15 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 16 |
|
| 17 |
-
# Call GPT-4 Vision
|
| 18 |
def identify_vehicle(image):
|
| 19 |
try:
|
| 20 |
base64_image = image_to_base64(image)
|
| 21 |
response = openai.chat.completions.create(
|
| 22 |
-
model="gpt-4-vision
|
| 23 |
messages=[
|
| 24 |
{
|
| 25 |
"role": "user",
|
| 26 |
"content": [
|
| 27 |
-
{"type": "text", "text": "
|
| 28 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
|
| 29 |
]
|
| 30 |
}
|
|
@@ -35,13 +33,12 @@ def identify_vehicle(image):
|
|
| 35 |
except Exception as e:
|
| 36 |
return f"Error: {e}"
|
| 37 |
|
| 38 |
-
# Gradio app
|
| 39 |
interface = gr.Interface(
|
| 40 |
fn=identify_vehicle,
|
| 41 |
-
inputs=gr.Image(type="pil", label="Upload vehicle or part"),
|
| 42 |
outputs="text",
|
| 43 |
title="AutoIQ",
|
| 44 |
-
description="
|
| 45 |
)
|
| 46 |
|
| 47 |
if __name__ == "__main__":
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
from io import BytesIO
|
| 7 |
|
| 8 |
+
# Load your OpenAI API key from Hugging Face secrets
|
| 9 |
openai.api_key = os.getenv("OPENAI_API_KEY")
|
| 10 |
|
|
|
|
| 11 |
def image_to_base64(image):
|
| 12 |
buffered = BytesIO()
|
| 13 |
image.save(buffered, format="PNG")
|
| 14 |
return base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 15 |
|
|
|
|
| 16 |
def identify_vehicle(image):
|
| 17 |
try:
|
| 18 |
base64_image = image_to_base64(image)
|
| 19 |
response = openai.chat.completions.create(
|
| 20 |
+
model="gpt-4-vision",
|
| 21 |
messages=[
|
| 22 |
{
|
| 23 |
"role": "user",
|
| 24 |
"content": [
|
| 25 |
+
{"type": "text", "text": "Identify the car or part in this image."},
|
| 26 |
{"type": "image_url", "image_url": {"url": f"data:image/png;base64,{base64_image}"}}
|
| 27 |
]
|
| 28 |
}
|
|
|
|
| 33 |
except Exception as e:
|
| 34 |
return f"Error: {e}"
|
| 35 |
|
|
|
|
| 36 |
interface = gr.Interface(
|
| 37 |
fn=identify_vehicle,
|
| 38 |
+
inputs=gr.Image(type="pil", label="Upload a vehicle or part photo"),
|
| 39 |
outputs="text",
|
| 40 |
title="AutoIQ",
|
| 41 |
+
description="AI tool to identify cars and parts from photos."
|
| 42 |
)
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|