Spaces:
Paused
Paused
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from selenium import webdriver
|
| 3 |
+
from selenium.webdriver.common.by import By
|
| 4 |
+
from selenium.webdriver.common.keys import Keys
|
| 5 |
+
from selenium.webdriver.support.ui import WebDriverWait
|
| 6 |
+
from selenium.webdriver.support import expected_conditions as EC
|
| 7 |
+
from selenium.webdriver.chrome.options import Options
|
| 8 |
+
import time
|
| 9 |
+
|
| 10 |
+
# Function to generate image URL
|
| 11 |
+
def generate_image(prompt):
|
| 12 |
+
# Configure Chrome options for headless mode
|
| 13 |
+
chrome_options = Options()
|
| 14 |
+
chrome_options.add_argument("--headless")
|
| 15 |
+
chrome_options.add_argument("--no-sandbox")
|
| 16 |
+
chrome_options.add_argument("--disable-dev-shm-usage")
|
| 17 |
+
|
| 18 |
+
# Initialize the WebDriver
|
| 19 |
+
driver = webdriver.Chrome(options=chrome_options)
|
| 20 |
+
|
| 21 |
+
try:
|
| 22 |
+
# Open the website
|
| 23 |
+
driver.get("https://gptimage.ai/")
|
| 24 |
+
|
| 25 |
+
# Wait for the textarea input field
|
| 26 |
+
input_field = WebDriverWait(driver, 10).until(
|
| 27 |
+
EC.presence_of_element_located(
|
| 28 |
+
(By.CSS_SELECTOR, "textarea.wp-content-visualizer-chat-input")
|
| 29 |
+
)
|
| 30 |
+
)
|
| 31 |
+
# Enter the prompt
|
| 32 |
+
input_field.send_keys(prompt)
|
| 33 |
+
|
| 34 |
+
# Wait for the "Generate Image" button and click it
|
| 35 |
+
generate_button = WebDriverWait(driver, 10).until(
|
| 36 |
+
EC.element_to_be_clickable(
|
| 37 |
+
(By.CSS_SELECTOR, "button.wp-content-visualizer-chat-send")
|
| 38 |
+
)
|
| 39 |
+
)
|
| 40 |
+
generate_button.click()
|
| 41 |
+
|
| 42 |
+
# Wait for the generated image
|
| 43 |
+
generated_image = WebDriverWait(driver, 20).until(
|
| 44 |
+
EC.presence_of_element_located(
|
| 45 |
+
(By.CSS_SELECTOR, "img.wp-content-visualizer-generated-image")
|
| 46 |
+
)
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
# Extract the image URL
|
| 50 |
+
image_url = generated_image.get_attribute("src")
|
| 51 |
+
return image_url
|
| 52 |
+
|
| 53 |
+
finally:
|
| 54 |
+
# Close the browser
|
| 55 |
+
driver.quit()
|
| 56 |
+
|
| 57 |
+
# Gradio interface
|
| 58 |
+
def gradio_interface(prompt):
|
| 59 |
+
try:
|
| 60 |
+
image_url = generate_image(prompt)
|
| 61 |
+
return f"Generated Image URL: {image_url}", image_url
|
| 62 |
+
except Exception as e:
|
| 63 |
+
return f"Error: {str(e)}", None
|
| 64 |
+
|
| 65 |
+
# Create Gradio app
|
| 66 |
+
iface = gr.Interface(
|
| 67 |
+
fn=gradio_interface,
|
| 68 |
+
inputs=gr.Textbox(label="Enter your prompt", placeholder="e.g., A serene mountain landscape at sunset"),
|
| 69 |
+
outputs=[gr.Textbox(label="Result"), gr.Image(label="Generated Image")],
|
| 70 |
+
title="Image Generator with Selenium",
|
| 71 |
+
description="Enter a prompt to generate an image using gptimage.ai."
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
# Launch the app
|
| 75 |
+
if __name__ == "__main__":
|
| 76 |
+
iface.launch()
|