terra1 / app.py
flatindo's picture
Update app.py
b2dc86f
raw
history blame
1.82 kB
import gradio as gr
from selenium import webdriver
from selenium.common.exceptions import WebDriverException
from PIL import Image
from io import BytesIO
import openai
api_key = "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"
openai.api_key = api_key
def generate_response():
try:
prompts = [
f"Please make 3 best microstock titles with {web_scrape}",
]
response = openai.Completion.create(
engine="davinci", # You can choose different engines
prompt=prompts,
max_tokens=50, # Adjust max_tokens as needed
)
generated_text = response.choices[0].text.strip()
return generated_text
except Exception as e:
return f"Error: {str(e)}"
def web_scrape(url):
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
try:
wd = webdriver.Chrome(options=options)
wd.set_window_size(1080, 720) # Adjust the window size here
wd.get(url)
wd.implicitly_wait(10)
page_title = wd.title.replace("Stock Photo | Adobe Stock", "").strip()
#content_value = meta_element.get_attribute("name")
return page_title
except WebDriverException as e:
return "error handle website"
finally:
if wd:
wd.quit()
return Image.open(BytesIO(screenshot))
iface = gr.Interface(
fn=web_scrape,
inputs=gr.inputs.Textbox(label="Website URL", default="https://stock.adobe.com/stock-photo/id/621214874"),
outputs=gr.outputs.Textbox(label="Web Content"),
title="Web Scraping with Selenium (Body Tag)",
description="Scrape the content of a website's <body> tag using Selenium.",
)
iface.launch()