Skip to content

Commit

Permalink
Making script more robust
Browse files Browse the repository at this point in the history
Allowing for intermittent internet.
  • Loading branch information
mgifford authored Jan 31, 2025
1 parent ebb4287 commit 74e60e7
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions alt-text-generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Disable parallelism for Hugging Face tokenizers to avoid fork-related warnings
os.environ["TOKENIZERS_PARALLELISM"] = "false"

import socket
import argparse
import csv
import json
Expand Down Expand Up @@ -84,6 +85,18 @@ def validate_anthropic_key(selected_model):
# Enable logging with timestamps
logging.basicConfig(level=logging.DEBUG, format="%(asctime)s - %(levelname)s - %(message)s")

def check_internet(host="8.8.8.8", port=53, timeout=3):
"""
Checks if the system has an active internet connection.
Attempts to connect to a well-known public DNS server (Google's 8.8.8.8).
Returns True if the connection is successful, otherwise False.
"""
try:
socket.setdefaulttimeout(timeout)
socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect((host, port))
return True
except socket.error:
return False

def generate_with_blip(image_path_or_url, alt_text="", title_text=""):
"""
Expand Down Expand Up @@ -514,10 +527,16 @@ def clean_and_post_process_alt_text(generated_text):
# Generate alt text using BLIP with alt_text and title_text integration
def generate_alt_text(image_url, alt_text="", title_text="", model="blip", client=None):
"""Generate alt text using BLIP, Anthropic, Ollama, or Azure OpenAI."""

while not check_internet(): # ✅ If no internet, pause execution
print("⏳ Internet connection lost. Pausing scan... Retrying in 60 seconds.")
time.sleep(60)

try:
# Ensure the client is provided for Azure OpenAI
if model == "azure_openai" and client is None:
raise ValueError("Azure OpenAI client is missing. Ensure it's properly initialized and passed.")
# Check if the image exists (Wrap in retry logic)
while not check_internet():
print("⏳ Internet connection lost. Retrying in 60 seconds...")
time.sleep(60)

# Skip SVG files (not supported)
if image_url.lower().endswith(".svg"):
Expand Down Expand Up @@ -545,6 +564,9 @@ def generate_alt_text(image_url, alt_text="", title_text="", model="blip", clien
prompt = f"Generate concise and descriptive alt text for the following image URL: {image_url}."
return generate_with_ollama(image_url, prompt)
elif model == "azure_openai":
# Ensure the client is provided for Azure OpenAI
if model == "azure_openai" and client is None:
raise ValueError("Azure OpenAI client is missing. Ensure it's properly initialized and passed.")
return generate_with_azure_openai(image_url, max_tokens=300)
else:
return f"Unsupported model: {model}"
Expand Down

0 comments on commit 74e60e7

Please sign in to comment.