BlafKing / sd-civitai-browser-plus

Extension to access CivitAI via WebUI: download, delete, scan for updates, list installed models, assign tags, and boost downloads with multi-threading.
GNU Affero General Public License v3.0
289 stars 44 forks source link

[Bug]: User Agent #258

Closed duskfallcrew closed 3 months ago

duskfallcrew commented 4 months ago

Describe the bug.

Went to go search for a PonyXL model to add, and it gave me the error UserAgent not defined.

Steps to reproduce the problem.

  1. CivitAi Browser Tab
  2. Insert Search Term
  3. Press Search
  4. Error Pops up in the log

Expected behavior

Expected to get an erroneously LARGE AMOUNT of pony loras and models because I searched for "PONY" but meant to type ponyXL model LOL. INstead i got useragent error :P

System info

Console logs

Traceback (most recent call last):
  File "/workspace/venv/lib/python3.10/site-packages/gradio/routes.py", line 488, in run_predict
    output = await app.get_blocks().process_api(
  File "/workspace/venv/lib/python3.10/site-packages/gradio/blocks.py", line 1431, in process_api
    result = await self.call_function(
  File "/workspace/venv/lib/python3.10/site-packages/gradio/blocks.py", line 1103, in call_function
    prediction = await anyio.to_thread.run_sync(
  File "/workspace/venv/lib/python3.10/site-packages/anyio/to_thread.py", line 33, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "/workspace/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 877, in run_sync_in_worker_thread
    return await future
  File "/workspace/venv/lib/python3.10/site-packages/anyio/_backends/_asyncio.py", line 807, in run
    result = context.run(func, *args)
  File "/workspace/venv/lib/python3.10/site-packages/gradio/utils.py", line 707, in wrapper
    response = f(*args, **kwargs)
  File "/workspace/stable-diffusion-webui/extensions/sd-civitai-browser-plus/scripts/civitai_api.py", line 360, in initial_model_page
    gl.json_data = request_civit_api(api_url)
  File "/workspace/stable-diffusion-webui/extensions/sd-civitai-browser-plus/scripts/civitai_api.py", line 1180, in request_civit_api
    headers = get_headers()
  File "/workspace/stable-diffusion-webui/extensions/sd-civitai-browser-plus/scripts/civitai_api.py", line 1163, in get_headers
    user_agent = UserAgent().chrome
NameError: name 'UserAgent' is not defined

Additional information

No response

duskfallcrew commented 3 months ago

It told me via gpt, because i don't program lol - i look crap up! That the following MIGHT FIX THIS: from fake_useragent import UserAgent or: pip install fake_useragent

Noting that i'm on a linux based OS running it, but it's not asssuming my browser as i'm not on local -

Also noting: Kayfa from civit (we're both in the creator program) - said it was working fine for them on desktop... WHICH IS WEIRD. (And they said it was recently optimized for runpod, which is similar to vast in that both are jupyter/linux environments)

duskfallcrew commented 3 months ago

ALRIGHT! So this is how i managed to fix it on my end and if this breaks your code don't worry about patching it, i'm able to figure this out once it's installed

I had to do this: def get_headers(referer=None, no_api=None): api_key = getattr(opts, "custom_api_key", "") try: user_agent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36" except ImportError: user_agent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36" headers = { "Connection": "keep-alive", "User-Agent": user_agent, "Content-Type": "application/json" } if referer: headers['Referer'] = f"https://civitai.com/models/{referer}" if api_key and not no_api: headers['Authorization'] = f'Bearer {api_key}'

return headers

As well as the pip install thing.

Also extra error i'm coming across that's easy fix for me that's no huge biggie: When the API key is entered somehow the text box goes "API KEY" and then the api key. I just deleted the text and it worked.

Btw: You rule, this plugin is amazing.

duskfallcrew commented 3 months ago

civitai_api.py.zip

If it helps in context I patched the python file on the server i'm renting, and for easier use i'll just replace this each time for me XD I'm weird :3 sorry if this b reaks code p_p no like seriously this extension is saving my bacon

duskfallcrew commented 3 months ago

it turns out after a new GPU: I just need the PIP install thing. That's it, it's just a tad fussy - :) one more PIP install when i set up isn't gonna hurt <3 It's not a bug, it's just a feature LMAO.

BlafKing commented 3 months ago

Glad to hear you managed to fix it! And enjoy the new GPU :)