Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.20 代码 bug 专用 #96

Open
SnapdragonLee opened this issue Oct 17, 2023 · 4 comments
Open

v1.20 代码 bug 专用 #96

SnapdragonLee opened this issue Oct 17, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@SnapdragonLee
Copy link
Owner

所有的最新的问题请在这里直接提出,如果太长也可以单开issue

@SnapdragonLee SnapdragonLee pinned this issue Oct 17, 2023
@SnapdragonLee SnapdragonLee added the bug Something isn't working label Oct 18, 2023
@BarryAllen-Arrow
Copy link

replicate画图的方法失效了哦~提示404,应该是url变了。我看网站的cookies好像也加了什么crsf认证啥的,不知道还能不能搞诶

@BarryAllen-Arrow
Copy link

replicate画图的方法失效了哦~提示404,应该是url变了。我看网站的cookies好像也加了什么crsf认证啥的,不知道还能不能搞诶

我问了chatgpt,他说有这种认证的话好像不太好搞,如果我能直接打开浏览器,那其实可以用selenium来模拟浏览器操作

@BarryAllen-Arrow
Copy link

replicate画图的方法失效了哦~提示404,应该是url变了。我看网站的cookies好像也加了什么crsf认证啥的,不知道还能不能搞诶

通过与chatgpt的深入交流探讨。我已经研究出了selenium的方法,亲测有效!
这个方法方便很多,不需要随机生成anonymus_id,也不需要提交任何json。原理就是模拟浏览器实时操作。缺点是他会在后台打开一个浏览器进行操作,会增加电脑的运算负荷,大概增加个七十多M的运存占用吧。
已经写成一个class了,大佬有兴趣可以看看:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time


class LatentGenerator:
    def __init__(self):
        # 设置Chrome选项,以便在无头模式下运行(没有图形界面)
        chrome_options = Options()
        # 禁用加载网页图片的功能,加快网页加载
        prefs = {"profile.managed_default_content_settings.images": 2}
        chrome_options.add_experimental_option("prefs", prefs)
        chrome_options.add_argument('--incognito')  # 启动无痕(隐身)模式
        chrome_options.add_argument("--headless")  # 也可以不使用无头模式来直观地看到浏览器操作

        # 设置Selenium Webdriver
        self.driver = webdriver.Chrome(options=chrome_options)

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.driver.quit()

    def clear_and_send_keys(self, element_id, data):
        input_element = self.driver.find_element(By.ID, element_id)
        input_element.clear()  # 先清除输入框
        input_element.send_keys(data)

    def gen_image(self, prompt, width=768, height=768, num_images=1, num_inference_steps=8, guidance_scale=8):
        try:
            # 打开网页
            self.driver.get("https://replicate.com/luosiallen/latent-consistency-model")
            # 可能需要等待页面加载完成
            WebDriverWait(self.driver, 1).until(EC.presence_of_element_located((By.ID, "prompt")))
            print("页面加载完成")

            # 使用helper函数清除并发送keys
            self.clear_and_send_keys("prompt", prompt)
            print("输入prompt成功!")
            self.clear_and_send_keys("width", width)
            self.clear_and_send_keys("height", height)
            self.clear_and_send_keys("num_images", num_images)
            self.clear_and_send_keys("num_inference_steps", num_inference_steps)
            self.clear_and_send_keys("guidance_scale", guidance_scale)

            # 点击按钮
            submitButton = self.driver.find_element(By.XPATH, "//button[@type='submit']")
            submitButton.click()
            print("提交成功!")

            # 初始化变量以跟踪重试次数
            retries = 10
            # 初始化图片链接列表
            images_url = []
            # 尝试最多retries次检查图片元素是否存在
            for attempt in range(retries):
                try:
                    # 检查图片元素是否存在
                    WebDriverWait(self.driver, 5).until(
                        EC.presence_of_all_elements_located((By.CSS_SELECTOR, "img[data-testid='value-output-image']"))
                    )
                    image_elements = self.driver.find_elements(By.CSS_SELECTOR, "img[data-testid='value-output-image']")
                    if image_elements:
                        image_urls = [element.get_attribute("src") for element in image_elements]
                        print(f"找到图片元素:{len(image_elements)}张")
                        return image_urls
                    break  # 如果找到了元素,则跳出循环
                except Exception as e:
                    time.sleep(5)  # 等待5秒后重试
                    print(f"图片元素不存在,等待5秒后进行第 {attempt + 2} 次尝试...")
                    if attempt == retries - 1:
                        raise Exception("图片未能在1分钟内生成!多半是寄了!。")
                self.driver.delete_all_cookies()
        finally:
            # 清理:关闭浏览器
            self.driver.quit()

@SnapdragonLee
Copy link
Owner Author

上述办法会进行一些测试

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants