WahibR commited on
Commit
f7dbe7b
·
verified ·
1 Parent(s): bb11b9a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -30
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
- import numpy as np
9
- import cv2
10
 
11
- client = OpenAI() # API key is taken from HF Secret
 
 
 
 
12
 
13
- def analyze_image(image):
14
- # Convert image -> JPEG bytes
15
- _, buffer = cv2.imencode(".jpg", image)
16
- img_bytes = buffer.tobytes()
 
17
 
18
- try:
19
  response = client.responses.create(
20
- model="gpt-4o-mini",
21
- input="Detect all scratches, cracks, dents or marks on this phone. "
22
- "Be specific, describe severity and location.",
23
- image=[{"type": "input_image", "image": img_bytes}]
 
 
 
 
 
 
 
 
 
24
  )
25
 
26
- text_output = response.output[0].content[0].text
27
-
28
- except Exception as e:
29
- text_output = f"OpenAI Vision Error: {e}"
30
-
31
- return text_output
32
-
33
 
34
- demo = gr.Interface(
35
- fn=analyze_image,
36
- inputs=gr.Image(type="numpy", label="Upload Phone Photo"),
37
- outputs=gr.Textbox(label="OpenAI Vision Damage Report"),
38
- title="OpenAI Phone Damage Detector",
39
- description="This model identifies scratches, cracks, dents and defects using OpenAI Vision."
40
  )
41
 
42
- demo.launch()
 
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()