gradio icon indicating copy to clipboard operation
gradio copied to clipboard

TypeError: Cannot set property WebSocket of #<Object> which has only a getter / Gradio App not working in NextJS

Open TommeZ opened this issue 2 years ago • 29 comments

Describe the bug

I can't find anything that resolves my specific issue.

I'm trying to make a fetch request to my HuggingFace space via the Gradio app on my NextJS project, but I'm getting the following error:

TypeError: Cannot set property WebSocket of #<Object> which has only a getter at eval (webpack-internal:///(rsc)/./node_modules/@gradio/client/dist/index.js:282:34)

  • error node_modules@gradio\client\dist\index.js (271:24) @ eval
  • error unhandledRejection: Error [TypeError]: Cannot set property WebSocket of #<Object> which has only a getter at eval (webpack-internal:///(rsc)/./node_modules/@gradio/client/dist/index.js:282:34) { digest: undefined }

Does anyone know if there's a way to resolve this, or know what's going on here that I can fix?

Have you searched existing issues? 🔎

  • [X] I have searched and found no existing issues

Reproduction

For private reasons, I have removed the names of the actual response picture and Hugging Face space.

`import { client } from "@gradio/client";

const HF_ACCESS_TOKEN = process.env.HF_ACCESS_TOKEN

export default async function getDetections() {

const response = await fetch("jpg");
const exampleImage = await response.blob();

const app = await client("HF_Name/MLDetector", { hf_token: HF_ACCESS_TOKEN });

const result = await app.predict("/predict", [
    exampleImage, 	// blob in 'img' Image component
]);

}`

Screenshot

Capture

Logs

No response

System Info

Gradio Environment Information:
Operating System:  Windows


gradio version:  3.38.0   

gradio Dependencies:      
  aiofiles: 23.1.0        
  aiohttp: 3.8.5
  altair: 5.0.1
  fastapi: 0.100.0        
  ffmpy: 0.3.1
  gradio-client: 0.2.10
  httpx: 0.24.1
  huggingface-hub: 0.16.4
  jinja2: 3.0.2
  markdown-it-py: 2.2.0
  markupsafe: 2.0.1
  matplotlib: 3.7.1
  mdit-py-plugins: 0.3.3
  numpy: 1.24.3
  orjson: 3.9.2
  packaging: 23.1
  pandas: 1.3.4
  pillow: 9.5.0
  pydantic: 1.10.11
  pydub: 0.25.1
  python-multipart: 0.0.6
  pyyaml: 6.0
  requests: 2.26.0
  semantic-version: 2.10.0
  typing-extensions: 4.7.1
  uvicorn: 0.23.1
  websockets: 11.0.3


gradio_client version:  0.2.10

gradio_client Dependencies:
  fsspec: 2023.6.0
  httpx: 0.24.1
  huggingface-hub: 0.16.4
  packaging: 23.1
  requests: 2.26.0
  typing-extensions: 4.7.1
  websockets: 11.0.3

Severity

Blocking usage of gradio

TommeZ avatar Sep 18 '23 05:09 TommeZ

Since this seems to to be an environment or framework specific issue, we will need a more complete reproduction. Could you put together a github repo with a default next app showing this problem? Preferably one that connects to a public HF space so we can run it.

Thanks.

pngwn avatar Sep 18 '23 19:09 pngwn

Will close for now -- can reopen once we have a repro.

abidlabs avatar Sep 22 '23 02:09 abidlabs

not fixed?

piscopancer avatar Oct 02 '23 17:10 piscopancer

I'm currently working on a way to replicate the issue, by creating a new repo that doesn't use my company's code. Will share once complete. But it appears so far that fetching data from Hugging Face via Gradio generates this error when run inside a Next.js project.

TommeZ avatar Oct 03 '23 08:10 TommeZ

Do you have a dump of your system info? Is next running locally or deployed to vercel? If deployed, do you know if it is using the worker runtime or the lambda runtime?

pngwn avatar Oct 03 '23 11:10 pngwn

I also met this issue. running in my local env.

sw_vers
ProductName:		macOS
ProductVersion:		13.2.1

node -v
v18.14.2

npm show @gradio/client version
0.4.1

JimmyLv avatar Oct 04 '23 07:10 JimmyLv

Do you have a dump of your system info? Is next running locally or deployed to vercel? If deployed, do you know if it is using the worker runtime or the lambda runtime?

I'm running it locally on VSCode. Windows 10

    "@gradio/client": "^0.3.1",
    "@types/node": "20.6.2",
    "@types/react": "18.2.21",
    "@types/react-dom": "18.2.7",
    "@types/ws": "^8.5.5",
    "autoprefixer": "10.4.15",
    "dotenv": "^16.3.1",
    "eslint": "8.49.0",
    "eslint-config-next": "13.4.19",
    "next": "13.4.19",
    "postcss": "8.4.29",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "tailwindcss": "3.3.3",
    "typescript": "5.2.2",
    "web3": "^1.7.4",
    "ws": "^8.14.1"

TommeZ avatar Oct 04 '23 14:10 TommeZ

I've replicated the issue at the following repo:

https://github.com/TommeZ/gradio-fetch

getTest().js makes a request to Hugging Face which returns the mentioned error, whereas it should be returning a string in the terminal.

TommeZ avatar Oct 05 '23 08:10 TommeZ

Thanks you!

pngwn avatar Oct 05 '23 15:10 pngwn

Found a similar issue here: https://github.com/vercel/next.js/issues/41866 with a really simple fix

Jay2theWhy avatar Oct 08 '23 20:10 Jay2theWhy

@Jay2theWhy do you mean the fix: replaced axios with cross-fetch?

JimmyLv avatar Oct 09 '23 04:10 JimmyLv

@JimmyLv I mean this PR that was merged for the fix: https://github.com/spacebudz/lucid/pull/108. From my testing, this resolved my conflict with my gradio app and next.js. I also ran the unit tests on this change and all passed.

Jay2theWhy avatar Oct 10 '23 18:10 Jay2theWhy

@Jay2theWhy cool, I just saw your PR https://github.com/gradio-app/gradio/pull/5840 good job! 👍

JimmyLv avatar Oct 12 '23 01:10 JimmyLv

Still having this exact issue with NextJS implemented with hugging face.

dylan0356 avatar Oct 13 '23 20:10 dylan0356

I've just updated to the latest gradio/client via npm, and I'm receiving the same error still?

Not sure if I follow what the fix was and the subsequent steps to take.

TommeZ avatar Oct 16 '23 19:10 TommeZ

We haven't released a new version of the client just yet, but will be soon (in the next day or so)

abidlabs avatar Oct 16 '23 19:10 abidlabs

@TommeZ You can follow this PR for when the next version is released: https://github.com/gradio-app/gradio/pull/5831 Should be resolved for v0.5.2

Jay2theWhy avatar Oct 16 '23 19:10 Jay2theWhy

Thanks for your support gentlemen.

TommeZ avatar Oct 16 '23 19:10 TommeZ

Just upgraded to v0.5.2 but still running into this problem, I'm pretty sure global.Websocket is undefined but can't assign a value.

========global.Websocket======== undefined undefined

TypeError: Cannot set property WebSocket of #<Object> which has only a getter

Seems like the error might be due to trying to set a property on a frozen object or a non-extensible global object.

So my solution was to define let MyWebSocket with a new variable and the problem was solved!

let MyWebSocket
function api_factory(fetch_implementation, WebSocket_factory) {
   ...
     async function client2(app_reference, options = { normalise_files: true }) {
       if ((typeof window === "undefined" || !("WebSocket" in window)) && !globalThis.Websocket) {
          const ws = await import("./wrapper-6f348d45.js");
          NodeBlob = (await import("node:buffer")).Blob;
          MyWebSocket = ws.WebSocket;
        }
      }
}

const { post_data, upload_files, client, handle_blob } = api_factory(
  fetch,
  (...args) => new MyWebSocket(...args)
);

At the same time I realized that another solution is to customize a client using api_factory but not using the passed in WebSocket_factory in the constructor method, hahaha I don't know why.

JimmyLv avatar Oct 17 '23 07:10 JimmyLv

If it is undefined but can't be assigned then the client won't work without some other changes. I'll take a closer look at this as soon as I can.

pngwn avatar Oct 17 '23 13:10 pngwn

Yeah even on the newest version I am still getting

TypeError: Cannot set property WebSocket of #<Object> which has only a getter at eval (webpack-internal:///(rsc)/./node_modules/@gradio/client/dist/index.js:289:34) ⨯ node_modules/@gradio/client/dist/index.js (278:24) @ eval ⨯ unhandledRejection: TypeError: Cannot set property WebSocket of #<Object> which has only a getter at eval (webpack-internal:///(rsc)/./node_modules/@gradio/client/dist/index.js:289:34) null ⨯ node_modules/@gradio/client/dist/index.js (278:24) @ eval ⨯ unhandledRejection: TypeError: Cannot set property WebSocket of #<Object> which has only a getter at eval (webpack-internal:///(rsc)/./node_modules/@gradio/client/dist/index.js:289:34) null

dylan0356 avatar Oct 17 '23 17:10 dylan0356

Any updates or workarounds on this?

odysseaspapadimas avatar Nov 03 '23 14:11 odysseaspapadimas

Any update guys?

TommeZ avatar Nov 12 '23 15:11 TommeZ

I also have the issue. Super annoying. Is there any version of node or nextjs where this isn't an issue?

cantonalex avatar Nov 23 '23 06:11 cantonalex

Why was this closed it is still not fixed

dylan0356 avatar Dec 08 '23 02:12 dylan0356

@pngwn

cantonalex avatar Dec 10 '23 13:12 cantonalex

I'm also getting the same issue. client version 0.10.1. Vanilla install of nextjs v14

import { client } from "@gradio/client";
try {
    const app = await client("http://localhost:7860/", {});
    const api_info = await app.view_api();
    console.log("Gradio Client info", api_info)
} catch (e) {
  console.log("Gradio Client error", e)
}

Error

TypeError: Cannot set property WebSocket of #<Object> which has only a getter
    at eval (webpack-internal:///(rsc)/./node_modules/@gradio/client/dist/index.js:403:34)
TypeError: Cannot set property WebSocket of #<Object> which has only a getter
    at eval (webpack-internal:///(rsc)/./node_modules/@gradio/client/dist/index.js:403:34)
 ⨯ node_modules/@gradio/client/dist/index.js (397:24) @ eval
 ⨯ unhandledRejection: TypeError: Cannot set property WebSocket of #<Object> which has only a getter
    at eval (webpack-internal:///(rsc)/./node_modules/@gradio/client/dist/index.js:403:34)
null
 ⨯ node_modules/@gradio/client/dist/index.js (397:24) @ eval
 ⨯ unhandledRejection: TypeError: Cannot set property WebSocket of #<Object> which has only a getter
    at eval (webpack-internal:///(rsc)/./node_modules/@gradio/client/dist/index.js:403:34)
null

@pngwn

daz-williams avatar Jan 29 '24 13:01 daz-williams

I also have the same issue. gradio client version 0.12.2 and nextjs v14.

kosamit avatar Mar 06 '24 06:03 kosamit

Sorry guys, finally had some time to take a second look at this and figured out why my previous PR wasn't working as intended: #5840 . !global.Websocket needed to be !global.WebSocket. Apologies for the silly mistake. I'll have some free time in the next ~48 hours to submit a new PR and test the change locally, but if a team member could make the change before then, that would also be great. Again, apologies for not catching this earlier and blocking people's projects.

For anyone curious, this commit was the culprit

Jay2theWhy avatar Mar 06 '24 16:03 Jay2theWhy

Closed by #8118!

pngwn avatar May 02 '24 21:05 pngwn