zdou0830 commited on
Commit
af70a47
1 Parent(s): 9fdaaeb
Files changed (1) hide show
  1. main.py +35 -0
main.py CHANGED
@@ -4,6 +4,7 @@ import requests
4
  import socket
5
  from torchvision import transforms
6
  import os
 
7
 
8
  torch.hub._validate_not_a_forked_repo=lambda a,b,c: True
9
 
@@ -18,6 +19,40 @@ LOCALHOST_NAME = os.getenv("GRADIO_SERVER_NAME", "127.0.0.1")
18
  GRADIO_API_SERVER = "https://api.gradio.app/v1/tunnel-request"
19
 
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
22
  def get_first_available_port(initial: int, final: int) -> int:
23
  """
 
4
  import socket
5
  from torchvision import transforms
6
  import os
7
+ import asyncio
8
 
9
  torch.hub._validate_not_a_forked_repo=lambda a,b,c: True
10
 
 
19
  GRADIO_API_SERVER = "https://api.gradio.app/v1/tunnel-request"
20
 
21
 
22
+ if sys.platform == "win32" and hasattr(asyncio, "WindowsSelectorEventLoopPolicy"):
23
+ # "Any thread" and "selector" should be orthogonal, but there's not a clean
24
+ # interface for composing policies so pick the right base.
25
+ _BasePolicy = asyncio.WindowsSelectorEventLoopPolicy # type: ignore
26
+ else:
27
+ _BasePolicy = asyncio.DefaultEventLoopPolicy
28
+
29
+
30
+ class AnyThreadEventLoopPolicy(_BasePolicy): # type: ignore
31
+ """Event loop policy that allows loop creation on any thread.
32
+ The default `asyncio` event loop policy only automatically creates
33
+ event loops in the main threads. Other threads must create event
34
+ loops explicitly or `asyncio.get_event_loop` (and therefore
35
+ `.IOLoop.current`) will fail. Installing this policy allows event
36
+ loops to be created automatically on any thread, matching the
37
+ behavior of Tornado versions prior to 5.0 (or 5.0 on Python 2).
38
+ Usage::
39
+ asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
40
+ .. versionadded:: 5.0
41
+ """
42
+
43
+ def get_event_loop(self) -> asyncio.AbstractEventLoop:
44
+ try:
45
+ return super().get_event_loop()
46
+ except (RuntimeError, AssertionError):
47
+ # This was an AssertionError in python 3.4.2 (which ships with debian jessie)
48
+ # and changed to a RuntimeError in 3.4.3.
49
+ # "There is no current event loop in thread %r"
50
+ loop = self.new_event_loop()
51
+ self.set_event_loop(loop)
52
+ return loop
53
+
54
+
55
+ asyncio.set_event_loop_policy(AnyThreadEventLoopPolicy())
56
 
57
  def get_first_available_port(initial: int, final: int) -> int:
58
  """