Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,44 @@
|
|
| 1 |
import os
|
| 2 |
-
os.environ.pop("HTTP_PROXY", None)
|
| 3 |
-
os.environ.pop("HTTPS_PROXY", None)
|
| 4 |
-
os.environ.pop("ALL_PROXY", None)
|
| 5 |
-
|
| 6 |
import gradio as gr
|
| 7 |
from openai import OpenAI
|
| 8 |
-
|
| 9 |
-
import cv2
|
| 10 |
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
def
|
| 14 |
-
# Convert image
|
| 15 |
-
|
| 16 |
-
|
|
|
|
| 17 |
|
| 18 |
-
|
| 19 |
response = client.responses.create(
|
| 20 |
-
model="gpt-4o-mini",
|
| 21 |
-
input=
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
)
|
| 25 |
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
except Exception as e:
|
| 29 |
-
text_output = f"OpenAI Vision Error: {e}"
|
| 30 |
-
|
| 31 |
-
return text_output
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
| 35 |
-
fn=
|
| 36 |
-
inputs=gr.Image(type="numpy"
|
| 37 |
-
outputs=
|
| 38 |
-
title="OpenAI Phone
|
| 39 |
-
description="This model identifies scratches, cracks, dents and defects using OpenAI Vision."
|
| 40 |
)
|
| 41 |
|
| 42 |
-
|
|
|
|
| 1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from openai import OpenAI
|
| 4 |
+
from PIL import Image
|
|
|
|
| 5 |
|
| 6 |
+
# FIX: force proper OpenAI init
|
| 7 |
+
client = OpenAI(
|
| 8 |
+
api_key=os.environ["OPENAI_API_KEY"],
|
| 9 |
+
base_url="https://api.openai.com/v1"
|
| 10 |
+
)
|
| 11 |
|
| 12 |
+
def analyze(img):
|
| 13 |
+
# Convert Gradio’s numpy image → PIL → bytes
|
| 14 |
+
pil_img = Image.fromarray(img)
|
| 15 |
+
buf = os.path.join("/tmp", "input.jpg")
|
| 16 |
+
pil_img.save(buf)
|
| 17 |
|
| 18 |
+
with open(buf, "rb") as f:
|
| 19 |
response = client.responses.create(
|
| 20 |
+
model="gpt-4o-mini",
|
| 21 |
+
input=[
|
| 22 |
+
{
|
| 23 |
+
"role": "user",
|
| 24 |
+
"content": [
|
| 25 |
+
{"type": "input_text", "text": "Detect scratches, cracks, dents. Return JSON."},
|
| 26 |
+
{
|
| 27 |
+
"type": "input_image",
|
| 28 |
+
"image_url": f"data:image/jpeg;base64,{f.read().encode('base64')}"
|
| 29 |
+
}
|
| 30 |
+
]
|
| 31 |
+
}
|
| 32 |
+
]
|
| 33 |
)
|
| 34 |
|
| 35 |
+
return str(response)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
iface = gr.Interface(
|
| 38 |
+
fn=analyze,
|
| 39 |
+
inputs=gr.Image(type="numpy"),
|
| 40 |
+
outputs="text",
|
| 41 |
+
title="OpenAI Phone Scratch Checker"
|
|
|
|
| 42 |
)
|
| 43 |
|
| 44 |
+
iface.launch()
|