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

window not defined + document "type": "module" requirement. #7103

Closed
1 task done
fffiloni opened this issue Jan 22, 2024 · 5 comments · Fixed by #8118
Closed
1 task done

window not defined + document "type": "module" requirement. #7103

fffiloni opened this issue Jan 22, 2024 · 5 comments · Fixed by #8118
Assignees
Labels
bug Something isn't working gradio_client Related to the one of the gradio client libraries

Comments

@fffiloni
Copy link

fffiloni commented Jan 22, 2024

Describe the bug

Hello !

I'm a working on a simple Node.js express app, which can call the gradio client to play with space apis.
I encountered several errors about "EventSource" not defined, and "window" not defined, errors i have been able to solve :)

Here's the workaround for @gradio/client 0.10.1 and Node.js v18.16.0
Note that we are workin with ES6 Syntax.

#1 First make sure in your package.json file you add

"type": "module"

#2 About EventSource

npm install eventsource

then in your server.js add

import { createRequire } from 'node:module'
const require = createRequire(import.meta.url);
global.EventSource = require('eventsource');

then in file /node_modules/@gradio/client/dist/index.js
line 744 replace:
replace:

eventSource = EventSource_factory(url);

by adding .toString() to url arg:

eventSource = EventSource_factory(url.toString());

same operation to line 1060

event_stream = EventSource_factory(url);

by adding .toString() to url arg:

event_stream = EventSource_factory(url.toString());


#3 About window not defined

It's not finished, now you will get window not defined event, so in file /node_modules/@gradio/client/dist/index.js:1078
line 1078

replace:

let fn2 = event_callbacks[event_id];
window.setTimeout(fn2, 0, _data);

by :

let fn2 = event_callbacks[event_id];
if (typeof window !== "undefined") {

  window.setTimeout(fn2, 0, _data);
} else {
  setTimeout(fn2, 0, _data);
}


So now in your main server.js, your code should be like this:

import { client } from "@gradio/client";
import { createRequire } from 'node:module'
const require = createRequire(import.meta.url);
global.EventSource = require('eventsource');

async function test_api(){
  const grapi_test = await client("gradio/hello_world");
  const apitest_result = await grapi_test.predict("/predict", [
    "John",
  ]);
  console.log(apitest_result.data);
}

test_api()

It should now work, as it works on my hand, console should log:
[ 'Hello John!' ]

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

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

async function test_api(){
  const grapi_test = await client("gradio/hello_world");
  const apitest_result = await grapi_test.predict("/predict", [
    "John",
  ]);
  console.log(apitest_result.data);
}

test_api()

Screenshot

No response

Logs

/node_modules/@gradio/client/dist/index.js:1209
  (...args) => new EventSource(...args)
               ^

ReferenceError: EventSource is not defined
/node_modules/@gradio/client/dist/index.js:1078
            window.setTimeout(fn2, 0, _data);
            ^

ReferenceError: window is not defined

System Info

@gradio/client 0.10.1
Node.js v18.16.0

Severity

I can work around it

@fffiloni fffiloni added the bug Something isn't working label Jan 22, 2024
@fffiloni
Copy link
Author

Note: this solution will work while calling public space API.

Still does not work with private spaces

@abidlabs abidlabs added the gradio_client Related to the one of the gradio client libraries label Feb 11, 2024
@abidlabs abidlabs added this to the Clients 1.0 📡 milestone Feb 11, 2024
@zishanahmed08
Copy link

@fffiloni Is there any solution to get it running with private spaces. Instead of Node.js , i tried with https://deno.land/ ,but i get different errors

@pngwn
Copy link
Member

pngwn commented Apr 11, 2024

We have an issue about the second point.

I'll keep this open for points 1 and 2 and will change the title.

@pngwn pngwn changed the title @Gradio/client 0.10.1 Multiple changes to avoid errors in Node.js apps window not defined + document "type": "module" requirement. Apr 11, 2024
@mananchawla2005
Copy link

Hey adding to that, there is a error while using with frameworks like nuxt as well. what to do for that cases?

@pngwn
Copy link
Member

pngwn commented Apr 11, 2024

We will look into these environment issues. I think they are all related.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working gradio_client Related to the one of the gradio client libraries
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants