Spaces:
Paused
Paused
File size: 4,502 Bytes
771b04b a5d0f31 771b04b 15f2a36 f494df8 15f2a36 f494df8 a0de170 15f2a36 a0de170 15f2a36 771b04b 15f2a36 a5d0f31 3f54a6f 771b04b 3f54a6f 15f2a36 771b04b a5d0f31 771b04b a5d0f31 771b04b a5d0f31 771b04b a5d0f31 771b04b a5d0f31 771b04b a5d0f31 3f54a6f 771b04b a5d0f31 771b04b a0de170 771b04b 15f2a36 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
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
import time
import subprocess
import os
# Function to log Chrome and ChromeDriver versions
def log_versions():
try:
# Print PATH for debugging
print(f"System PATH: {os.environ.get('PATH')}")
# Try finding Chrome binary
chrome_binary = None
for binary in ["google-chrome", "google-chrome-stable", "chromium-browser"]:
try:
chrome_path = subprocess.check_output(["which", binary]).decode().strip()
print(f"Found {binary} at: {chrome_path}")
chrome_binary = binary
break
except subprocess.CalledProcessError:
continue
if not chrome_binary:
print("Error: No Chrome binary (google-chrome, google-chrome-stable, chromium-browser) found")
return
# Get Chrome version
chrome_version = subprocess.check_output([chrome_binary, "--version"]).decode().strip()
print(f"Chrome version: {chrome_version}")
# Try finding ChromeDriver
try:
chromedriver_path = subprocess.check_output(["which", "chromedriver"]).decode().strip()
print(f"Found chromedriver at: {chromedriver_path}")
chromedriver_version = subprocess.check_output(["chromedriver", "--version"]).decode().strip()
print(f"ChromeDriver version: {chromedriver_version}")
except subprocess.CalledProcessError as e:
print(f"Error finding chromedriver: {str(e)}")
except Exception as e:
print(f"Error checking versions: {str(e)}")
# Function to generate image URL
def generate_image(prompt):
# Log versions for debugging
log_versions()
# Configure Chrome options for headless mode
chrome_options = Options()
chrome_options.add_argument("--headless")
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")
# Initialize the WebDriver
try:
driver = webdriver.Chrome(options=chrome_options)
except Exception as e:
return f"Error initializing ChromeDriver: {str(e)}"
try:
# Open the website
driver.get("https://gptimage.ai/")
# Wait for the textarea input field
input_field = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "textarea.wp-content-visualizer-chat-input")
)
)
# Enter the prompt
input_field.send_keys(prompt)
# Wait for the "Generate Image" button and click it
generate_button = WebDriverWait(driver, 10).until(
EC.element_to_be_clickable(
(By.CSS_SELECTOR, "button.wp-content-visualizer-chat-send")
)
)
generate_button.click()
# Wait for the generated image
generated_image = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(
(By.CSS_SELECTOR, "img.wp-content-visualizer-generated-image")
)
)
# Extract the image URL
image_url = generated_image.get_attribute("src")
return image_url
except Exception as e:
return f"Error: {str(e)}"
finally:
# Close the browser
driver.quit()
# Gradio interface
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:
return f"Error: {str(e)}", None
# Create Gradio app
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."
)
# Launch the app
if __name__ == "__main__":
iface.launch(server_name="0.0.0.0", server_port=7860) # No share=True |