mirror of
https://github.com/gradio-app/gradio.git
synced 2025-03-31 12:20:26 +08:00
Merge branch 'Format-The-Codebase' of https://github.com/gradio-app/gradio into Format-The-Codebase
This commit is contained in:
commit
8251a52447
@ -157,26 +157,17 @@ def start_server(
|
||||
|
||||
|
||||
def setup_tunnel(local_server_port: int, endpoint: str) -> str:
|
||||
response = url_request(
|
||||
response = requests.get(
|
||||
endpoint + "/v1/tunnel-request" if endpoint is not None else GRADIO_API_SERVER
|
||||
)
|
||||
if response and response.code == 200:
|
||||
if response and response.status_code == 200:
|
||||
try:
|
||||
payload = json.loads(response.read().decode("utf-8"))[0]
|
||||
payload = response.json()[0]
|
||||
return create_tunnel(payload, LOCALHOST_NAME, local_server_port)
|
||||
except Exception as e:
|
||||
raise RuntimeError(str(e))
|
||||
|
||||
|
||||
def url_request(url: str) -> Optional[http.client.HTTPResponse]:
|
||||
try:
|
||||
req = urllib.request.Request(
|
||||
url=url, headers={"content-type": "application/json"}
|
||||
)
|
||||
res = urllib.request.urlopen(req, timeout=10)
|
||||
return res
|
||||
except Exception as e:
|
||||
raise RuntimeError(str(e))
|
||||
else:
|
||||
raise RuntimeError("Could not get share link from Gradio API Server.")
|
||||
|
||||
|
||||
def url_ok(url: str) -> bool:
|
||||
|
@ -94,17 +94,12 @@ class TestInterpretation(unittest.TestCase):
|
||||
|
||||
|
||||
class TestURLs(unittest.TestCase):
|
||||
def test_url_ok(self):
|
||||
urllib.request.urlopen = mock.MagicMock(return_value="test")
|
||||
res = networking.url_request("http://www.gradio.app")
|
||||
self.assertEqual(res, "test")
|
||||
|
||||
def test_setup_tunnel(self):
|
||||
networking.create_tunnel = mock.MagicMock(return_value="test")
|
||||
res = networking.setup_tunnel(None, None)
|
||||
self.assertEqual(res, "test")
|
||||
|
||||
def test_url_ok_2(self):
|
||||
def test_url_ok(self):
|
||||
res = networking.url_ok("https://www.gradio.app")
|
||||
self.assertTrue(res)
|
||||
|
||||
|
@ -7,6 +7,7 @@ import unittest
|
||||
import unittest.mock as mock
|
||||
|
||||
import paramiko
|
||||
import requests
|
||||
|
||||
from gradio import Interface, networking, tunneling
|
||||
|
||||
@ -15,8 +16,8 @@ os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
||||
|
||||
class TestTunneling(unittest.TestCase):
|
||||
def test_create_tunnel(self):
|
||||
response = networking.url_request(networking.GRADIO_API_SERVER)
|
||||
payload = json.loads(response.read().decode("utf-8"))[0]
|
||||
response = requests.get(networking.GRADIO_API_SERVER)
|
||||
payload = response.json()[0]
|
||||
io = Interface(lambda x: x, "text", "text")
|
||||
_, path_to_local_server, _ = io.launch(prevent_thread_lock=True, share=False)
|
||||
_, localhost, port = path_to_local_server.split(":")
|
||||
|
Loading…
x
Reference in New Issue
Block a user