| import gradio as gr |
| from selenium import webdriver |
| from selenium.webdriver.common.by import By |
| from selenium.webdriver.common.keys import Keys |
| from selenium.webdriver.support.ui import WebDriverWait |
| from selenium.webdriver.support import expected_conditions as EC |
| from selenium.webdriver.chrome.options import Options |
| from selenium.webdriver.chrome.service import Service |
| import time |
| import subprocess |
| import os |
| import logging |
|
|
| |
| logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s') |
| logger = logging.getLogger(__name__) |
|
|
| |
| def log_versions(): |
| try: |
| |
| logger.info(f"System PATH: {os.environ.get('PATH')}") |
| |
| chrome_binary = None |
| for binary in ["google-chrome", "google-chrome-stable", "chromium-browser"]: |
| try: |
| chrome_path = subprocess.check_output(["which", binary]).decode().strip() |
| logger.info(f"Found {binary} at: {chrome_path}") |
| chrome_binary = binary |
| break |
| except subprocess.CalledProcessError: |
| continue |
| if not chrome_binary: |
| logger.error("No Chrome binary (google-chrome, google-chrome-stable, chromium-browser) found") |
| return |
|
|
| |
| chrome_version = subprocess.check_output([chrome_binary, "--version"]).decode().strip() |
| logger.info(f"Chrome version: {chrome_version}") |
|
|
| |
| try: |
| chromedriver_path = subprocess.check_output(["which", "chromedriver"]).decode().strip() |
| logger.info(f"Found chromedriver at: {chromedriver_path}") |
| chromedriver_version = subprocess.check_output(["chromedriver", "--version"]).decode().strip() |
| logger.info(f"ChromeDriver version: {chromedriver_version}") |
| except subprocess.CalledProcessError as e: |
| logger.error(f"Error finding chromedriver: {str(e)}") |
|
|
| except Exception as e: |
| logger.error(f"Error checking versions: {str(e)}") |
|
|
| |
| def generate_image(prompt): |
| |
| log_versions() |
|
|
| |
| chrome_options = Options() |
| chrome_options.add_argument("--headless=new") |
| chrome_options.add_argument("--no-sandbox") |
| chrome_options.add_argument("--disable-dev-shm-usage") |
| chrome_options.add_argument("--disable-gpu") |
| chrome_options.add_argument("window-size=1920x1080") |
|
|
| |
| service = Service(executable_path="/usr/local/bin/chromedriver", log_path="/app/chromedriver.log") |
| try: |
| logger.info("Initializing ChromeDriver...") |
| driver = webdriver.Chrome(service=service, options=chrome_options) |
| logger.info("ChromeDriver initialized successfully") |
| except Exception as e: |
| logger.error(f"Error initializing ChromeDriver: {str(e)}") |
| with open("/app/chromedriver.log", "r") as log_file: |
| logger.error(f"ChromeDriver log: {log_file.read()}") |
| return f"Error initializing ChromeDriver: {str(e)}" |
|
|
| try: |
| |
| logger.info("Opening https://gptimage.ai/...") |
| driver.get("https://gptimage.ai/") |
|
|
| |
| logger.info("Waiting for input field...") |
| input_field = WebDriverWait(driver, 10).until( |
| EC.presence_of_element_located( |
| (By.CSS_SELECTOR, "textarea.wp-content-visualizer-chat-input") |
| ) |
| ) |
| |
| logger.info("Entering prompt...") |
| input_field.send_keys(prompt) |
|
|
| |
| logger.info("Waiting for generate button...") |
| generate_button = WebDriverWait(driver, 10).until( |
| EC.element_to_be_clickable( |
| (By.CSS_SELECTOR, "button.wp-content-visualizer-chat-send") |
| ) |
| ) |
| logger.info("Clicking generate button...") |
| generate_button.click() |
|
|
| |
| logger.info("Waiting for generated image...") |
| generated_image = WebDriverWait(driver, 20).until( |
| EC.presence_of_element_located( |
| (By.CSS_SELECTOR, "img.wp-content-visualizer-generated-image") |
| ) |
| ) |
|
|
| |
| logger.info("Extracting image URL...") |
| image_url = generated_image.get_attribute("src") |
| return image_url |
|
|
| except Exception as e: |
| logger.error(f"Error during image generation: {str(e)}") |
| return f"Error: {str(e)}" |
| finally: |
| |
| logger.info("Closing browser...") |
| driver.quit() |
|
|
| |
| def gradio_interface(prompt): |
| try: |
| image_url = generate_image(prompt) |
| if image_url.startswith("Error"): |
| return image_url, None |
| return f"Generated Image URL: {image_url}", image_url |
| except Exception as e: |
| logger.error(f"Error in gradio_interface: {str(e)}") |
| return f"Error: {str(e)}", None |
|
|
| |
| logger.info("Creating Gradio interface...") |
| iface = gr.Interface( |
| fn=gradio_interface, |
| inputs=gr.Textbox(label="Enter your prompt", placeholder="e.g., A serene mountain landscape at sunset"), |
| outputs=[gr.Textbox(label="Result"), gr.Image(label="Generated Image")], |
| title="Image Generator with Selenium", |
| description="Enter a prompt to generate an image using gptimage.ai.", |
| allow_flagging="never" |
| ) |
|
|
| |
| logger.info("Testing ChromeDriver at startup...") |
| test_result = generate_image("Test prompt at startup") |
| logger.info(f"Startup test result: {test_result}") |
|
|
| |
| if __name__ == "__main__": |
| logger.info("Launching Gradio app...") |
| iface.launch(server_name="0.0.0.0", server_port=7860, debug=True) |