Merge pull request #162 from hank-ai/fixfileuploadandaddkeeporigfilename

fixed single file handling, added keepfilename
This commit is contained in:
Abubakar Abid 2021-04-30 00:33:26 -04:00 committed by GitHub
commit dac5ce24c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 21 additions and 12 deletions

View File

@ -21,6 +21,7 @@ from ffmpy import FFmpeg
import math
import tempfile
from pandas.api.types import is_bool_dtype, is_numeric_dtype, is_string_dtype
from pathlib import Path
class InputComponent(Component):
@ -893,16 +894,18 @@ class File(InputComponent):
Input type: Union[file-object, bytes, List[Union[file-object, bytes]]]
"""
def __init__(self, file_count="single", type="file", label=None):
def __init__(self, file_count="single", type="file", label=None, keepfilename=True):
'''
Parameters:
file_count (str): if single, allows user to upload one file. If "multiple", user uploads multiple files. If "directory", user uploads all files in selected directory. Return type will be list for each file in case of "multiple" or "directory".
type (str): Type of value to be returned by component. "file" returns a temporary file object whose path can be retrieved by file_obj.name, "binary" returns an bytes object.
keepfilename (bool): whether to keep the original filename in the f.name field upon upload. If true, will place 'originalfilename' + a '_' before the unique temporary safe filename string and extension
label (str): component name in interface.
'''
self.file_count = file_count
self.type = type
self.test_input = None
self.keepfilename = keepfilename
super().__init__(label)
def get_template_context(self):
@ -925,7 +928,9 @@ class File(InputComponent):
if is_local_example:
return open(name)
else:
return processing_utils.decode_base64_to_file(data)
if self.keepfilename: filenameprefix=Path(name).stem+'_'
else: filenameprefix=""
return processing_utils.decode_base64_to_file(data, filenameprefix=filenameprefix)
elif self.type == "bytes":
if is_local_example:
with open(name, "rb") as file_data:
@ -934,7 +939,8 @@ class File(InputComponent):
else:
raise ValueError("Unknown type: " + str(self.type) + ". Please choose from: 'file', 'bytes'.")
if self.file_count == "single":
return process_single_file(x[0])
if isinstance(x, list): return process_single_file(x[0])
else: return process_single_file(x)
else:
return [process_single_file(f) for f in x]

View File

@ -373,7 +373,7 @@ class Interface:
print("PASSED")
continue
def launch(self, inline=None, inbrowser=None, share=False, debug=False, auth=None, auth_message=None, private_endpoint=None):
def launch(self, inline=None, inbrowser=None, share=False, debug=False, auth=None, auth_message=None, private_endpoint=None, prevent_thread_lock=False):
"""
Parameters:
inline (bool): whether to display in the interface inline on python notebooks.
@ -416,7 +416,7 @@ class Interface:
# If running in a colab or not able to access localhost, automatically create a shareable link
is_colab = utils.colab_check()
if is_colab or not(networking.url_ok(path_to_local_server)):
if is_colab or (share and not(networking.url_ok(path_to_local_server))):
share = True
if is_colab:
if debug:
@ -480,10 +480,12 @@ class Interface:
sys.stdout.flush()
time.sleep(0.1)
is_in_interactive_mode = bool(getattr(sys, 'ps1', sys.flags.interactive))
if not is_in_interactive_mode:
print('is_in_interactive_mode=={}'.format(is_in_interactive_mode))
if not prevent_thread_lock and not is_in_interactive_mode:
print("going to lock thread and run in foreground ...")
self.run_until_interrupted(thread, path_to_local_server)
return app, path_to_local_server, share_url
return app, path_to_local_server, share_url, thread
def integrate(self, comet_ml=None):

View File

@ -79,14 +79,15 @@ def decode_base64_to_binary(encoding):
data = encoding
return base64.b64decode(data), extension
def decode_base64_to_file(encoding, encryption_key=None):
def decode_base64_to_file(encoding, encryption_key=None, filenameprefix=""):
data, extension = decode_base64_to_binary(encoding)
if extension is None:
file_obj = tempfile.NamedTemporaryFile(delete=False)
file_obj = tempfile.NamedTemporaryFile(delete=False, prefix=filenameprefix)
else:
file_obj = tempfile.NamedTemporaryFile(delete=False, suffix="."+extension)
file_obj = tempfile.NamedTemporaryFile(delete=False, prefix=filenameprefix, suffix="."+extension)
if encryption_key is not None:
data = encryptor.encrypt(encryption_key, data)
#print("saving to ", file_obj.name)
file_obj.write(data)
file_obj.flush()
return file_obj

View File

@ -28,7 +28,7 @@
<meta name="twitter:description" content="{{ config['description'] or '' }}">
<meta name="twitter:image" content="{{ config['thumbnail'] or '' }}">
<title>Gradio</title>
<title>Hank.ai DocuVision UI</title>
<link href="https://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
<!-- VENDOR -->
<link type="text/css" href="{{ vendor_prefix }}/static/css/vendor/jquery-ui.css" rel="stylesheet">

View File

@ -1,6 +1,6 @@
<html>
<head>
<title>Gradio Login</title>
<title>Hank.ai Docuvision - Login</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
<link rel="stylesheet" href="{{ url_for('static', filename='css/gradio.css') }}">
<meta name="viewport" content="width=device-width">