Spaces:
Running
Running
fix build issues
Browse files
app.py
CHANGED
|
@@ -152,24 +152,61 @@ def describe_image_hf(image_path: str) -> str:
|
|
| 152 |
|
| 153 |
|
| 154 |
def describe_image_openai(image_path: str) -> str:
|
| 155 |
-
"""
|
| 156 |
if not OPENAI_AVAILABLE:
|
| 157 |
return "OpenAI not available for image captioning"
|
|
|
|
| 158 |
try:
|
|
|
|
| 159 |
with open(image_path, "rb") as f:
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
)
|
| 166 |
resp = openai.ChatCompletion.create(
|
| 167 |
-
model="gpt-4o-mini",
|
|
|
|
|
|
|
| 168 |
)
|
| 169 |
return resp.choices[0].message.content.strip()
|
|
|
|
| 170 |
except Exception as e:
|
| 171 |
return f"OpenAI image describe error: {e}"
|
| 172 |
|
|
|
|
| 173 |
# -----------------------------
|
| 174 |
# MCP Tools
|
| 175 |
# -----------------------------
|
|
|
|
| 152 |
|
| 153 |
|
| 154 |
def describe_image_openai(image_path: str) -> str:
|
| 155 |
+
"""Describe an image using OpenAI Vision (modern SDK compatible)."""
|
| 156 |
if not OPENAI_AVAILABLE:
|
| 157 |
return "OpenAI not available for image captioning"
|
| 158 |
+
|
| 159 |
try:
|
| 160 |
+
# Read image bytes
|
| 161 |
with open(image_path, "rb") as f:
|
| 162 |
+
image_bytes = f.read()
|
| 163 |
+
|
| 164 |
+
# Convert to base64 for safe transport in older SDKs
|
| 165 |
+
b64_image = base64.b64encode(image_bytes).decode("utf-8")
|
| 166 |
+
|
| 167 |
+
# Modern prompt content
|
| 168 |
+
prompt = (
|
| 169 |
+
"You are an accessibility assistant that describes images for visually impaired users. "
|
| 170 |
+
"Provide a clear, helpful, vivid, human-friendly description of the image.\n"
|
| 171 |
+
)
|
| 172 |
+
|
| 173 |
+
# Some OpenAI SDK versions require: client = openai.OpenAI()
|
| 174 |
+
try:
|
| 175 |
+
client = openai.OpenAI()
|
| 176 |
+
response = client.chat.completions.create(
|
| 177 |
+
model="gpt-4o-mini",
|
| 178 |
+
messages=[
|
| 179 |
+
{"role": "system", "content": "You describe images for visually impaired users."},
|
| 180 |
+
{"role": "user", "content": [
|
| 181 |
+
{"type": "text", "text": prompt},
|
| 182 |
+
{
|
| 183 |
+
"type": "image_url",
|
| 184 |
+
"image_url": f"data:image/jpeg;base64,{b64_image}"
|
| 185 |
+
}
|
| 186 |
+
]}
|
| 187 |
+
],
|
| 188 |
+
max_tokens=300,
|
| 189 |
+
)
|
| 190 |
+
return response.choices[0].message.content.strip()
|
| 191 |
+
|
| 192 |
+
except Exception:
|
| 193 |
+
# Fallback for legacy SDKs
|
| 194 |
+
legacy_prompt = (
|
| 195 |
+
"You are an assistant that describes images for visually impaired users.\n"
|
| 196 |
+
"Provide a concise, vivid, accessible description.\n"
|
| 197 |
+
"Image(base64): " + b64_image
|
| 198 |
)
|
| 199 |
resp = openai.ChatCompletion.create(
|
| 200 |
+
model="gpt-4o-mini",
|
| 201 |
+
messages=[{"role": "user", "content": legacy_prompt}],
|
| 202 |
+
max_tokens=300,
|
| 203 |
)
|
| 204 |
return resp.choices[0].message.content.strip()
|
| 205 |
+
|
| 206 |
except Exception as e:
|
| 207 |
return f"OpenAI image describe error: {e}"
|
| 208 |
|
| 209 |
+
|
| 210 |
# -----------------------------
|
| 211 |
# MCP Tools
|
| 212 |
# -----------------------------
|