fix threading, launching

This commit is contained in:
Ali Abid 2020-12-23 10:08:09 -08:00
parent bd28056fed
commit ad8512f98d
4 changed files with 33 additions and 25 deletions

View File

@ -1,3 +1,2 @@
recursive-include gradio/templates * recursive-include gradio/templates *
recursive-include gradio/static * recursive-include gradio/static *
include gradio/launches.json

View File

@ -7,7 +7,6 @@ gradio/embeddings.py
gradio/inputs.py gradio/inputs.py
gradio/interface.py gradio/interface.py
gradio/interpretation.py gradio/interpretation.py
gradio/launches.json
gradio/networking.py gradio/networking.py
gradio/notebook.py gradio/notebook.py
gradio/outputs.py gradio/outputs.py

View File

@ -3,6 +3,7 @@ This is the core file in the `gradio` package, and defines the Interface class,
interface using the input and output types. interface using the input and output types.
""" """
import gradio
from gradio.inputs import InputComponent from gradio.inputs import InputComponent
from gradio.outputs import OutputComponent from gradio.outputs import OutputComponent
from gradio import networking, strings, utils from gradio import networking, strings, utils
@ -26,7 +27,7 @@ analytics.write_key = "uxIFddIEuuUcFLf9VgH2teTEtPlWdkNy"
analytics_url = 'https://api.gradio.app/' analytics_url = 'https://api.gradio.app/'
ip_address = networking.get_local_ip_address() ip_address = networking.get_local_ip_address()
JSON_PATH = pkg_resources.resource_filename("gradio", "launches.json") JSON_PATH = os.path.join(os.path.dirname(gradio.__file__), "launches.json")
class Interface: class Interface:
""" """
@ -405,16 +406,6 @@ class Interface:
else: else:
print("Colab notebook detected. To show errors in colab notebook, set debug=True in launch()") print("Colab notebook detected. To show errors in colab notebook, set debug=True in launch()")
if not os.path.exists(JSON_PATH):
with open(JSON_PATH, "w+") as j:
launches = {"launches": 0}
j.write(json.dumps(launches))
else:
with open(JSON_PATH) as j:
launches = json.load(j)
if launches["launches"] in [25, 50]:
print(strings.en["BETA_INVITE"])
self.share = share self.share = share
if share: if share:
@ -451,6 +442,8 @@ class Interface:
if inbrowser is None: if inbrowser is None:
inbrowser = False inbrowser = False
launch_counter()
if inbrowser and not is_colab: if inbrowser and not is_colab:
webbrowser.open(path_to_local_server) # Open a browser tab webbrowser.open(path_to_local_server) # Open a browser tab
# with the interface. # with the interface.
@ -492,13 +485,31 @@ class Interface:
is_in_interactive_mode = bool(getattr(sys, 'ps1', sys.flags.interactive)) is_in_interactive_mode = bool(getattr(sys, 'ps1', sys.flags.interactive))
if not is_in_interactive_mode: if not is_in_interactive_mode:
self.run_until_interrupted(thread, path_to_local_server) self.run_until_interrupted(thread, path_to_local_server)
launches["launches"] += 1
with open(JSON_PATH, "w") as j:
j.write(json.dumps(launches))
return app, path_to_local_server, share_url return app, path_to_local_server, share_url
def launch_counter():
try:
if not os.path.exists(JSON_PATH):
print("creating")
launches = {"launches": 1}
with open(JSON_PATH, "w+") as j:
json.dump(launches, j)
else:
print("loading")
with open(JSON_PATH) as j:
launches = json.load(j)
launches["launches"] += 1
print(launches["launches"])
if launches["launches"] in [25, 50]:
print(strings.en["BETA_INVITE"])
with open(JSON_PATH, "w") as j:
j.write(json.dumps(launches))
except:
import sys
print(sys.exc_info()[0])
print("Not tracking launches.")
def reset_all(): def reset_all():
for io in Interface.get_instances(): for io in Interface.get_instances():

View File

@ -268,13 +268,12 @@ def start_server(interface, server_name, server_port=None):
log.setLevel(logging.ERROR) log.setLevel(logging.ERROR)
if interface.save_to is not None: if interface.save_to is not None:
interface.save_to["port"] = port interface.save_to["port"] = port
# thread = threading.Thread(target=app.run, thread = threading.Thread(target=app.run,
# kwargs={"port": port, "host": server_name}, kwargs={"port": port, "host": server_name},
# daemon=True) daemon=True)
# thread.start() thread.start()
app.run(port=port, host=server_name)
return port, app, None return port, app, thread
def close_server(process): def close_server(process):
process.terminate() process.terminate()