Fix: Download button does not respect the filepath returned by the function (#2073)

* download fix

* tweaks

* formatting

* added orig filename

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
Dawood Khan 2022-08-29 16:39:15 -04:00 committed by GitHub
parent 85229a7058
commit 4816e3fb12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 64 deletions

View File

@ -2006,7 +2006,7 @@ class File(Changeable, Clearable, IOComponent, FileSerializable):
Parameters:
value: Default file to display, given as str file path. If callable, the function will be called whenever the app loads to set the initial value of the component.
file_count: 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: 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.
type: Type of value to be returned by component. "file" returns a temporary file object whose path can be retrieved by file_obj.name and original filename can be retrieved with file_obj.orig_name, "binary" returns an bytes object.
label: component name in interface.
show_label: if True, will display label.
interactive: if True, will allow users to upload a file; if False, can only be used to display files. If not provided, this is inferred based on whether the component is used as an input or output.
@ -2071,11 +2071,14 @@ class File(Changeable, Clearable, IOComponent, FileSerializable):
)
if self.type == "file":
if is_file:
return processing_utils.create_tmp_copy_of_file(file_name)
file = processing_utils.create_tmp_copy_of_file(file_name)
file.orig_name = file_name
else:
return processing_utils.decode_base64_to_file(
file = processing_utils.decode_base64_to_file(
data, file_path=file_name
)
file.orig_name = file_name
return file
elif self.type == "bytes":
if is_file:
with open(file_name, "rb") as file_data:
@ -2114,6 +2117,7 @@ class File(Changeable, Clearable, IOComponent, FileSerializable):
if isinstance(y, list):
return [
{
"orig_name": os.path.basename(file),
"name": processing_utils.create_tmp_copy_of_file(
file, self.temp_dir
).name,
@ -2125,6 +2129,7 @@ class File(Changeable, Clearable, IOComponent, FileSerializable):
]
else:
return {
"orig_name": os.path.basename(y),
"name": processing_utils.create_tmp_copy_of_file(
y, dir=self.temp_dir
).name,

View File

@ -854,6 +854,7 @@ class TestFile(unittest.TestCase):
file_input.serialize("test/test_files/sample_file.pdf")["name"],
"test/test_files/sample_file.pdf",
)
assert output.orig_name == "test/test_files/sample_file.pdf"
self.assertIsInstance(file_input.generate_sample(), dict)
file_input = gr.File(label="Upload Your File")

View File

@ -73,7 +73,7 @@
<div class="file-size w-3/12 p-2 hover:underline">
<a
href={download_files(file)}
download
download={display_file_name(file)}
class="text-indigo-600 hover:underline dark:text-indigo-300"
>Download</a
>
@ -91,7 +91,7 @@
<div class="file-size p-2 hover:underline">
<a
href={download_files(value)}
download
download={display_file_name(value)}
class="text-indigo-600 hover:underline dark:text-indigo-300"
>Download</a
>

View File

@ -13,7 +13,7 @@ export const prettyBytes = (bytes: number): string => {
export const display_file_name = (value: FileData): string => {
var str: string;
str = value.name;
str = value.orig_name || value.name;
if (str.length > 30) {
return `${str.substr(0, 30)}...`;
} else return str;

View File

@ -1,5 +1,6 @@
export interface FileData {
name: string;
orig_name?: string;
size?: number;
data: string;
is_file?: boolean;

98
ui/pnpm-lock.yaml generated
View File

@ -1,4 +1,4 @@
lockfileVersion: 5.4
lockfileVersion: 5.3
importers:
@ -43,7 +43,7 @@ importers:
'@tailwindcss/forms': 0.5.0_tailwindcss@3.1.6
'@testing-library/dom': 8.11.3
'@testing-library/svelte': 3.1.0_svelte@3.49.0
'@testing-library/user-event': 13.5.0_gzufz4q333be4gqfrvipwvqt6a
'@testing-library/user-event': 13.5.0_@testing-library+dom@8.11.3
autoprefixer: 10.4.4_postcss@8.4.6
babylonjs: 5.18.0
babylonjs-loaders: 5.18.0
@ -56,13 +56,13 @@ importers:
postcss: 8.4.6
postcss-nested: 5.0.6_postcss@8.4.6
prettier: 2.6.2
prettier-plugin-svelte: 2.7.0_3cyj5wbackxvw67rnaarcmbw7y
prettier-plugin-svelte: 2.7.0_prettier@2.6.2+svelte@3.49.0
sirv: 2.0.2
sirv-cli: 2.0.2
svelte: 3.49.0
svelte-check: 2.8.0_mgmdnb6x5rpawk37gozc2sbtta
svelte-check: 2.8.0_postcss@8.4.6+svelte@3.49.0
svelte-i18n: 3.3.13_svelte@3.49.0
svelte-preprocess: 4.10.6_mlkquajfpxs65rn6bdfntu7nmy
svelte-preprocess: 4.10.6_62d50a01257de5eec5be08cad9d3ed66
tailwindcss: 3.1.6
tinyspy: 0.3.0
typescript: 4.7.4
@ -131,7 +131,7 @@ importers:
'@gradio/video': link:../video
mime-types: 2.1.34
playwright: 1.22.2
svelte-i18n: 3.3.13
svelte-i18n: 3.3.13_svelte@3.49.0
packages/atoms:
specifiers:
@ -385,13 +385,13 @@ importers:
'@gradio/upload': link:../upload
'@gradio/video': link:../video
devDependencies:
'@sveltejs/adapter-auto': 1.0.0-next.65
'@sveltejs/kit': 1.0.0-next.318
'@sveltejs/adapter-auto': 1.0.0-next.67
'@sveltejs/kit': 1.0.0-next.318_svelte@3.49.0
autoprefixer: 10.4.2_postcss@8.4.6
postcss: 8.4.6
postcss-load-config: 3.1.1
svelte-check: 2.4.1_2y4otvh2n6klv6metqycpfiuzy
svelte-preprocess: 4.10.2_bw7ic75prjd4umr4fb55sbospu
svelte-check: 2.4.1_736abba5ed1eb6f8ecf70b1d49ead14b
svelte-preprocess: 4.10.2_d50790bb30dd88cc44babe7efa52bace
tailwindcss: 3.0.23_autoprefixer@10.4.2
tslib: 2.3.1
typescript: 4.5.5
@ -565,27 +565,27 @@ packages:
estree-walker: 2.0.2
picomatch: 2.3.1
/@sveltejs/adapter-auto/1.0.0-next.65:
resolution: {integrity: sha512-wYEcOeuCrswcmeOdmbaq+WxTp7vWN1fG1yPvmdzqe2LoUchOw6FQb6X/fR8miX6L8MXQXJteA0ntqE3FKqaBsw==}
/@sveltejs/adapter-auto/1.0.0-next.67:
resolution: {integrity: sha512-nLwdMIRK0igwoOTCwLW3bDr6VofT+8bFF/Nsvd+5t77X/AeS4cE667HOH1XcL4Is3JRKyWmBaE1iJnVhbbKfEg==}
dependencies:
'@sveltejs/adapter-cloudflare': 1.0.0-next.31
'@sveltejs/adapter-netlify': 1.0.0-next.71
'@sveltejs/adapter-vercel': 1.0.0-next.67
'@sveltejs/adapter-cloudflare': 1.0.0-next.32
'@sveltejs/adapter-netlify': 1.0.0-next.73
'@sveltejs/adapter-vercel': 1.0.0-next.69
transitivePeerDependencies:
- encoding
- supports-color
dev: true
/@sveltejs/adapter-cloudflare/1.0.0-next.31:
resolution: {integrity: sha512-HhEFZP72GJ8AZGgFECKIiayDcLaAWi65pI0AnBfiNhCifYSlH/mPNWNVD4AWRDnXnH6XU+FLwhGDnIDwytTyYg==}
/@sveltejs/adapter-cloudflare/1.0.0-next.32:
resolution: {integrity: sha512-tzkUsdQlBk9xUjcGUOBYos4HKaeaXvz9v4TQ1QS2yIHEtL5xvMEDPZ94/DB2gPL4LZCnYbdY2lsy5HCsoN0hkQ==}
dependencies:
'@cloudflare/workers-types': 3.14.1
esbuild: 0.14.53
worktop: 0.8.0-next.14
dev: true
/@sveltejs/adapter-netlify/1.0.0-next.71:
resolution: {integrity: sha512-la1CGtWO1xul1L3zEoFAoc4EX2uxZjrZcOMS3tkKB8drxhbQsNbnTE6fmSSMFiZXhxaikczrBgQwqIaDkLTmZg==}
/@sveltejs/adapter-netlify/1.0.0-next.73:
resolution: {integrity: sha512-dPsSPHbihycOvqfwNX6X2jfmdqYaZUKkDgky5vxR2sraTlMEwSo97s9N/fwRWwy9ORZi6DT5Ty0i0G+xJWiKPg==}
dependencies:
'@iarna/toml': 2.2.5
esbuild: 0.14.53
@ -593,25 +593,26 @@ packages:
tiny-glob: 0.2.9
dev: true
/@sveltejs/adapter-vercel/1.0.0-next.67:
resolution: {integrity: sha512-xg85d/vlivbTaZu70zmaPNkrY1YZhDrcxljuwVWO0LCzA4DACIA7CnXI9klUiXM5SPpsB8BhY6dS8sW5cDYWzw==}
/@sveltejs/adapter-vercel/1.0.0-next.69:
resolution: {integrity: sha512-E5mSkD3x3zYuJU5hPRmoqblrKZtEj2lRwhObnXf4OSFsMej2VUzoogbhaXHcVt57j0Ja5HgO07u7v9VUGOhi+w==}
dependencies:
'@vercel/nft': 0.21.0
'@vercel/nft': 0.22.0
esbuild: 0.14.53
transitivePeerDependencies:
- encoding
- supports-color
dev: true
/@sveltejs/kit/1.0.0-next.318:
/@sveltejs/kit/1.0.0-next.318_svelte@3.49.0:
resolution: {integrity: sha512-/M/XNvEqK71KCGro1xLuiUuklsMPe+G5DiVMs39tpfFIFhH4oCzAt+YBaIZDKORogGz3QDaYc5BV+eFv9E5cyw==}
engines: {node: '>=14.13'}
hasBin: true
peerDependencies:
svelte: ^3.44.0
dependencies:
'@sveltejs/vite-plugin-svelte': 1.0.0-next.41_vite@2.9.5
'@sveltejs/vite-plugin-svelte': 1.0.0-next.41_svelte@3.49.0+vite@2.9.5
sade: 1.8.1
svelte: 3.49.0
vite: 2.9.5
transitivePeerDependencies:
- diff-match-patch
@ -621,7 +622,7 @@ packages:
- supports-color
dev: true
/@sveltejs/vite-plugin-svelte/1.0.0-next.41_vite@2.9.5:
/@sveltejs/vite-plugin-svelte/1.0.0-next.41_svelte@3.49.0+vite@2.9.5:
resolution: {integrity: sha512-2kZ49mpi/YW1PIPvKaJNSSwIFgmw9QUf1+yaNa4U8yJD6AsfSHXAU3goscWbi1jfWnSg2PhvwAf+bvLCdp2F9g==}
engines: {node: ^14.13.1 || >= 16}
peerDependencies:
@ -636,7 +637,8 @@ packages:
debug: 4.3.4
kleur: 4.1.4
magic-string: 0.26.1
svelte-hmr: 0.14.11
svelte: 3.49.0
svelte-hmr: 0.14.11_svelte@3.49.0
vite: 2.9.5
transitivePeerDependencies:
- supports-color
@ -712,7 +714,7 @@ packages:
svelte: 3.49.0
dev: false
/@testing-library/user-event/13.5.0_gzufz4q333be4gqfrvipwvqt6a:
/@testing-library/user-event/13.5.0_@testing-library+dom@8.11.3:
resolution: {integrity: sha512-5Kwtbo3Y/NowpkbRuSepbyMFkZmHgD+vPzYB/RJ4oxt5Gj/avFFBYjhw27cqSVPVw/3a67NK1PbiIr9k4Gwmdg==}
engines: {node: '>=10', npm: '>=6'}
peerDependencies:
@ -828,8 +830,8 @@ packages:
'@types/yargs-parser': 21.0.0
dev: false
/@vercel/nft/0.21.0:
resolution: {integrity: sha512-hFCAETfI5cG8l5iAiLhMC2bReC5K7SIybzrxGorv+eGspIbIFsVw7Vg85GovXm/LxA08pIDrAlrhR6GN36XB/Q==}
/@vercel/nft/0.22.0:
resolution: {integrity: sha512-hB80/093PPiCefN2gVbqv6J93MH+63Zr7uDCwkiS/U4W07DXkLoftbnkBmZoS0Q84LiTSl9DRVSHU4XYCX+sJA==}
hasBin: true
dependencies:
'@mapbox/node-pre-gyp': 1.0.9
@ -2881,7 +2883,7 @@ packages:
picocolors: 1.0.0
source-map-js: 1.0.2
/prettier-plugin-svelte/2.7.0_3cyj5wbackxvw67rnaarcmbw7y:
/prettier-plugin-svelte/2.7.0_prettier@2.6.2+svelte@3.49.0:
resolution: {integrity: sha512-fQhhZICprZot2IqEyoiUYLTRdumULGRvw0o4dzl5jt0jfzVWdGqeYW27QTWAeXhoupEZJULmNoH3ueJwUWFLIA==}
peerDependencies:
prettier: ^1.16.4 || ^2.0.0
@ -3285,7 +3287,7 @@ packages:
resolution: {integrity: sha512-ot0WnXS9fgdkgIcePe6RHNk1WA8+muPa6cSjeR3V8K27q9BB1rTE3R1p7Hv0z1ZyAc8s6Vvv8DIyWf681MAt0w==}
engines: {node: '>= 0.4'}
/svelte-check/2.4.1_2y4otvh2n6klv6metqycpfiuzy:
/svelte-check/2.4.1_736abba5ed1eb6f8ecf70b1d49ead14b:
resolution: {integrity: sha512-xhf3ShP5rnRwBokrgTBJ/0cO9QIc1DAVu1NWNRTfCDsDBNjGmkS3HgitgUadRuoMKj1+irZR/yHJ+Uqobnkbrw==}
hasBin: true
peerDependencies:
@ -3298,7 +3300,8 @@ packages:
picocolors: 1.0.0
sade: 1.8.1
source-map: 0.7.3
svelte-preprocess: 4.10.2_bw7ic75prjd4umr4fb55sbospu
svelte: 3.49.0
svelte-preprocess: 4.10.2_d50790bb30dd88cc44babe7efa52bace
typescript: 4.5.5
transitivePeerDependencies:
- '@babel/core'
@ -3313,7 +3316,7 @@ packages:
- sugarss
dev: true
/svelte-check/2.8.0_mgmdnb6x5rpawk37gozc2sbtta:
/svelte-check/2.8.0_postcss@8.4.6+svelte@3.49.0:
resolution: {integrity: sha512-HRL66BxffMAZusqe5I5k26mRWQ+BobGd9Rxm3onh7ZVu0nTk8YTKJ9vu3LVPjUGLU9IX7zS+jmwPVhJYdXJ8vg==}
hasBin: true
peerDependencies:
@ -3326,7 +3329,7 @@ packages:
picocolors: 1.0.0
sade: 1.8.1
svelte: 3.49.0
svelte-preprocess: 4.10.6_mlkquajfpxs65rn6bdfntu7nmy
svelte-preprocess: 4.10.6_62d50a01257de5eec5be08cad9d3ed66
typescript: 4.7.4
transitivePeerDependencies:
- '@babel/core'
@ -3341,13 +3344,6 @@ packages:
- sugarss
dev: false
/svelte-hmr/0.14.11:
resolution: {integrity: sha512-R9CVfX6DXxW1Kn45Jtmx+yUe+sPhrbYSUp7TkzbW0jI5fVPn6lsNG9NEs5dFg5qRhFNAoVdRw5qQDLALNKhwbQ==}
engines: {node: ^12.20 || ^14.13.1 || >= 16}
peerDependencies:
svelte: '>=3.19.0'
dev: true
/svelte-hmr/0.14.11_svelte@3.49.0:
resolution: {integrity: sha512-R9CVfX6DXxW1Kn45Jtmx+yUe+sPhrbYSUp7TkzbW0jI5fVPn6lsNG9NEs5dFg5qRhFNAoVdRw5qQDLALNKhwbQ==}
engines: {node: ^12.20 || ^14.13.1 || >= 16}
@ -3355,21 +3351,6 @@ packages:
svelte: '>=3.19.0'
dependencies:
svelte: 3.49.0
dev: false
/svelte-i18n/3.3.13:
resolution: {integrity: sha512-RQM+ys4+Y9ztH//tX22H1UL2cniLNmIR+N4xmYygV6QpQ6EyQvloZiENRew8XrVzfvJ8HaE8NU6/yurLkl7z3g==}
engines: {node: '>= 11.15.0'}
hasBin: true
peerDependencies:
svelte: ^3.25.1
dependencies:
deepmerge: 4.2.2
estree-walker: 2.0.2
intl-messageformat: 9.11.4
sade: 1.8.1
tiny-glob: 0.2.9
dev: false
/svelte-i18n/3.3.13_svelte@3.49.0:
resolution: {integrity: sha512-RQM+ys4+Y9ztH//tX22H1UL2cniLNmIR+N4xmYygV6QpQ6EyQvloZiENRew8XrVzfvJ8HaE8NU6/yurLkl7z3g==}
@ -3386,7 +3367,7 @@ packages:
tiny-glob: 0.2.9
dev: false
/svelte-preprocess/4.10.2_bw7ic75prjd4umr4fb55sbospu:
/svelte-preprocess/4.10.2_d50790bb30dd88cc44babe7efa52bace:
resolution: {integrity: sha512-aPpkCreSo8EL/y8kJSa1trhiX0oyAtTjlNNM7BNjRAsMJ8Yy2LtqHt0zyd4pQPXt+D4PzbO3qTjjio3kwOxDlA==}
engines: {node: '>= 9.11.2'}
requiresBuild: true
@ -3435,10 +3416,11 @@ packages:
postcss-load-config: 3.1.1
sorcery: 0.10.0
strip-indent: 3.0.0
svelte: 3.49.0
typescript: 4.5.5
dev: true
/svelte-preprocess/4.10.6_mlkquajfpxs65rn6bdfntu7nmy:
/svelte-preprocess/4.10.6_62d50a01257de5eec5be08cad9d3ed66:
resolution: {integrity: sha512-I2SV1w/AveMvgIQlUF/ZOO3PYVnhxfcpNyGt8pxpUVhPfyfL/CZBkkw/KPfuFix5FJ9TnnNYMhACK3DtSaYVVQ==}
engines: {node: '>= 9.11.2'}
requiresBuild: true