|
15 | 15 | # import openai
|
16 | 16 | from openai import OpenAIError
|
17 | 17 | # Initialize the OpenAI client
|
18 |
| -from openai import OpenAI |
| 18 | +# from openai import OpenAI |
| 19 | +from openai import AzureOpenAI |
19 | 20 | import pytesseract
|
20 | 21 | from io import BytesIO
|
21 | 22 | from tqdm import tqdm # Import tqdm for progress bar
|
|
38 | 39 | model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-base")
|
39 | 40 |
|
40 | 41 | #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") |
44 | 42 | # 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 |
46 | 46 | 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 | + |
47 | 54 | 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 | + |
49 | 57 | 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 | + ) |
51 | 63 | logging.info("✅ Azure OpenAI client initialized successfully.")
|
52 | 64 | 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}") |
54 | 66 | client = None
|
55 | 67 |
|
56 |
| - |
57 |
| -print(f"DEBUG: API Key = {subscription_key}") |
58 |
| - |
59 | 68 | def validate_anthropic_key(selected_model):
|
60 | 69 | """Ensure the Anthropic API key is set only if the 'anthropic' model is selected."""
|
61 | 70 | 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:
|
295 | 304 |
|
296 | 305 |
|
297 | 306 | # 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 | + |
299 | 310 | if client is None:
|
300 | 311 | logging.error("❌ Azure OpenAI client is missing. Skipping this image.")
|
301 | 312 | return "Error: Azure OpenAI client missing"
|
| 313 | + |
302 | 314 | 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}") |
305 | 317 |
|
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") |
310 | 322 |
|
311 |
| - # Determine MIME type |
| 323 | + # ✅ Determine MIME Type |
312 | 324 | mime_type = "image/jpeg"
|
313 | 325 | if image_url.lower().endswith(".png"):
|
314 | 326 | mime_type = "image/png"
|
315 | 327 | elif image_url.lower().endswith(".webp"):
|
316 | 328 | mime_type = "image/webp"
|
317 | 329 |
|
318 |
| - # Format the base64 data correctly for OpenAI |
| 330 | + # ✅ Format Image for OpenAI API |
319 | 331 | image_data = f"data:{mime_type};base64,{image_base64}"
|
320 | 332 |
|
321 |
| - # Construct messages payload |
| 333 | + # ✅ Prepare Prompt |
322 | 334 | messages = [
|
323 | 335 | {"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 |
326 | 338 | ]
|
327 | 339 |
|
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, |
333 | 342 | messages=messages,
|
334 |
| - max_tokens=max_tokens |
| 343 | + max_tokens=max_tokens, |
335 | 344 | )
|
336 | 345 |
|
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: |
339 | 348 | 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 |
340 | 352 | 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 | + |
353 | 366 | 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)}" |
356 | 369 |
|
357 | 370 |
|
358 | 371 | def check_image_exists(image_url):
|
@@ -532,7 +545,7 @@ def generate_alt_text(image_url, alt_text="", title_text="", model="blip", clien
|
532 | 545 | prompt = f"Generate concise and descriptive alt text for the following image URL: {image_url}."
|
533 | 546 | return generate_with_ollama(image_url, prompt)
|
534 | 547 | 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) |
536 | 549 | else:
|
537 | 550 | return f"Unsupported model: {model}"
|
538 | 551 |
|
|
0 commit comments