Skip to content

Commit 8dc5e39

Browse files
authored
Got API for Azure OpenAI working. Finally....
1 parent 9c52f8c commit 8dc5e39

File tree

1 file changed

+59
-46
lines changed

1 file changed

+59
-46
lines changed

alt-text-generator.py

Lines changed: 59 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
# import openai
1616
from openai import OpenAIError
1717
# Initialize the OpenAI client
18-
from openai import OpenAI
18+
# from openai import OpenAI
19+
from openai import AzureOpenAI
1920
import pytesseract
2021
from io import BytesIO
2122
from tqdm import tqdm # Import tqdm for progress bar
@@ -38,24 +39,32 @@
3839
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
3940

4041
#OpenAI API
41-
endpoint = os.getenv("ENDPOINT_URL", "https://civicactions-openai.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2024-08-01-preview")
42-
deployment = os.getenv("DEPLOYMENT_NAME", "gpt-4o")
43-
# subscription_key = os.getenv("AZURE_OPENAI_API_KEY", "REPLACE_WITH_YOUR_KEY_VALUE_HERE")
4442
# export AZURE_OPENAI_API_KEY="your_api_key_here"
45-
# Ensure API key is set
43+
AZURE_OPENAI_ENDPOINT = "https://civicactions-openai.openai.azure.com/"
44+
DEPLOYMENT_NAME = "gpt-4o"
45+
API_VERSION = "2024-05-01-preview" # ✅ Define API_VERSION before using it
4646
subscription_key = os.getenv("AZURE_OPENAI_API_KEY")
47+
48+
# Debugging: Log API credentials (excluding API key for security)
49+
# print(f"DEBUG: AZURE_OPENAI_ENDPOINT = {AZURE_OPENAI_ENDPOINT}")
50+
# print(f"DEBUG: DEPLOYMENT_NAME = {DEPLOYMENT_NAME}")
51+
# print(f"DEBUG: API_VERSION = {API_VERSION}")
52+
# print(f"DEBUG: AZURE_OPENAI_API_KEY = {subscription_key[:5]}********")
53+
4754
if not subscription_key:
48-
raise ValueError("AZURE_OPENAI_API_KEY environment variable is missing.")
55+
raise ValueError("❌ ERROR: AZURE_OPENAI_API_KEY environment variable is missing.")
56+
4957
try:
50-
client = OpenAI(api_key=subscription_key)
58+
client = AzureOpenAI(
59+
api_key=subscription_key,
60+
azure_endpoint=AZURE_OPENAI_ENDPOINT, # Correct base URL
61+
api_version=API_VERSION,
62+
)
5163
logging.info("✅ Azure OpenAI client initialized successfully.")
5264
except Exception as e:
53-
logging.error(f"❌ Failed to initialize Azure OpenAI client: {e}")
65+
logging.exception(f"❌ Failed to initialize Azure OpenAI client: {e}")
5466
client = None
5567

56-
57-
print(f"DEBUG: API Key = {subscription_key}")
58-
5968
def validate_anthropic_key(selected_model):
6069
"""Ensure the Anthropic API key is set only if the 'anthropic' model is selected."""
6170
if selected_model == "anthropic" and not ANTHROPIC_API_KEY:
@@ -295,64 +304,68 @@ def generate_with_ollama(image_path_or_url, prompt, model_name="llama3.2-vision:
295304

296305

297306
# def generate_with_azure_openai(image_url, model, max_tokens, client):
298-
def generate_with_azure_openai(image_url, model, max_tokens, client):
307+
def generate_with_azure_openai(image_url, max_tokens=300):
308+
"""Generate alt text using Azure OpenAI with a given image URL."""
309+
299310
if client is None:
300311
logging.error("❌ Azure OpenAI client is missing. Skipping this image.")
301312
return "Error: Azure OpenAI client missing"
313+
302314
try:
303-
logging.info("\n======================")
304-
logging.info(f"Generating alt text for image: {image_url}\n")
315+
logging.info(f"🔵 Processing Image: {image_url}")
316+
print(f"🔵 Processing Image: {image_url}")
305317

306-
# Fetch and encode the image in base64
307-
response = requests.get(image_url, stream=True)
308-
response.raise_for_status()
309-
image_base64 = base64.b64encode(response.content).decode("utf-8")
318+
# Fetch & Encode Image
319+
image_response = requests.get(image_url, stream=True) # Renamed response
320+
image_response.raise_for_status()
321+
image_base64 = base64.b64encode(image_response.content).decode("utf-8")
310322

311-
# Determine MIME type
323+
# Determine MIME Type
312324
mime_type = "image/jpeg"
313325
if image_url.lower().endswith(".png"):
314326
mime_type = "image/png"
315327
elif image_url.lower().endswith(".webp"):
316328
mime_type = "image/webp"
317329

318-
# Format the base64 data correctly for OpenAI
330+
# Format Image for OpenAI API
319331
image_data = f"data:{mime_type};base64,{image_base64}"
320332

321-
# Construct messages payload
333+
# ✅ Prepare Prompt
322334
messages = [
323335
{"role": "system", "content": "Generate concise and descriptive alt text for an image."},
324-
{"role": "user", "content": "Describe this image in detail."},
325-
{"role": "user", "content": [{"type": "image_url", "image_url": image_data}]}
336+
{"role": "user", "content": "Describe this image in detail. Keep the response less than 40 words."},
337+
{"role": "user", "content": [{"type": "image_url", "image_url": {"url": image_data}}]} # Correct format
326338
]
327339

328-
logging.info(f"Sending request to Azure OpenAI with {len(image_base64)}-character base64 image data.")
329-
330-
# Call Azure OpenAI API
331-
response = client.chat.completions.create(
332-
model=model,
340+
response = client.chat.completions.create( # Use the client object directly
341+
model=DEPLOYMENT_NAME,
333342
messages=messages,
334-
max_tokens=max_tokens
343+
max_tokens=max_tokens,
335344
)
336345

337-
# Extract and clean the response
338-
if response and hasattr(response, 'choices') and response.choices:
346+
# Extract Response
347+
if hasattr(response, "choices") and response.choices:
339348
alt_text = response.choices[0].message.content.strip()
349+
logging.info(f"✅ Generated Alt Text: {alt_text}")
350+
print(f"✅ Generated Alt Text: {alt_text}")
351+
return alt_text
340352
else:
341-
alt_text = "Error: No valid response from OpenAI"
342-
logging.error("Received an unexpected response format from OpenAI.")
343-
344-
logging.info(f"Generated Alt Text: {alt_text}\n")
345-
return alt_text
346-
347-
except OpenAIError as e:
348-
logging.error(f"OpenAI API error: {str(e)}")
349-
return "Error: OpenAI API issue"
350-
except requests.RequestException as e:
351-
logging.error(f"Request error: {str(e)}")
352-
return "Error: Unable to retrieve image"
353+
logging.error("❌ No valid response from OpenAI")
354+
return "Error: No valid response from OpenAI"
355+
356+
except requests.exceptions.HTTPError as http_err:
357+
logging.error(f"❌ HTTP Error: {http_err.response.status_code} - {http_err.response.text}")
358+
if http_err.response.status_code == 404:
359+
logging.error(f"❌ 404 Error: Resource not found at {AZURE_OPENAI_ENDPOINT}")
360+
return f"HTTP Error: {http_err.response.status_code}"
361+
362+
except requests.exceptions.RequestException as e:
363+
logging.error(f"❌ Image retrieval error: {str(e)}")
364+
return f"Error: Unable to retrieve image - {str(e)}"
365+
353366
except Exception as e:
354-
logging.error(f"Unexpected error: {str(e)}")
355-
return "Error: Something went wrong"
367+
logging.error(f"Unexpected error: {str(e)}")
368+
return f"Error: Something went wrong - {str(e)}"
356369

357370

358371
def check_image_exists(image_url):
@@ -532,7 +545,7 @@ def generate_alt_text(image_url, alt_text="", title_text="", model="blip", clien
532545
prompt = f"Generate concise and descriptive alt text for the following image URL: {image_url}."
533546
return generate_with_ollama(image_url, prompt)
534547
elif model == "azure_openai":
535-
return generate_with_azure_openai(image_url, model, 300, client)
548+
return generate_with_azure_openai(image_url, max_tokens=300)
536549
else:
537550
return f"Unsupported model: {model}"
538551

0 commit comments

Comments
 (0)