1{
2 lib,
3 stdenv,
4 buildPythonPackage,
5 fetchPypi,
6 pythonOlder,
7 pythonRelaxDepsHook,
8 writeShellScriptBin,
9 gradio,
10
11 # pyproject
12 hatchling,
13 hatch-requirements-txt,
14 hatch-fancy-pypi-readme,
15
16 # runtime
17 setuptools,
18 aiofiles,
19 altair,
20 diffusers,
21 fastapi,
22 ffmpy,
23 gradio-client,
24 httpx,
25 huggingface-hub,
26 importlib-resources,
27 jinja2,
28 markupsafe,
29 matplotlib,
30 numpy,
31 orjson,
32 packaging,
33 pandas,
34 pillow,
35 pydantic,
36 python-multipart,
37 pydub,
38 pyyaml,
39 semantic-version,
40 typing-extensions,
41 uvicorn,
42 typer,
43 tomlkit,
44
45 # oauth
46 authlib,
47 itsdangerous,
48
49 # check
50 pytestCheckHook,
51 boto3,
52 gradio-pdf,
53 ffmpeg,
54 ipython,
55 pytest-asyncio,
56 respx,
57 scikit-image,
58 torch,
59 tqdm,
60 transformers,
61 vega-datasets,
62}:
63
64buildPythonPackage rec {
65 pname = "gradio";
66 version = "4.29.0";
67 format = "pyproject";
68
69 disabled = pythonOlder "3.7";
70
71 # We use the Pypi release, since it provides prebuilt webui assets,
72 # and upstream has stopped tagging releases since 3.41.0
73 src = fetchPypi {
74 inherit pname version;
75 hash = "sha256-17KT0b9kBO+xLgIgxfpwjETDoRM4aTJPlJv7HjkJXjo=";
76 };
77
78 # fix packaging.ParserSyntaxError, which can't handle comments
79 postPatch = ''
80 sed -ie "s/ #.*$//g" requirements*.txt
81
82 # they bundle deps?
83 rm -rf venv/
84 '';
85
86 pythonRelaxDeps = [ "tomlkit" ];
87
88 pythonRemoveDeps = [
89 # our package is presented as a binary, not a python lib - and
90 # this isn't a real runtime dependency
91 "ruff"
92 ];
93
94 nativeBuildInputs = [
95 pythonRelaxDepsHook
96 hatchling
97 hatch-requirements-txt
98 hatch-fancy-pypi-readme
99 ];
100
101 dependencies = [
102 setuptools # needed for 'pkg_resources'
103 aiofiles
104 altair
105 diffusers
106 fastapi
107 ffmpy
108 gradio-client
109 httpx
110 huggingface-hub
111 importlib-resources
112 jinja2
113 markupsafe
114 matplotlib
115 numpy
116 orjson
117 packaging
118 pandas
119 pillow
120 pydantic
121 python-multipart
122 pydub
123 pyyaml
124 semantic-version
125 typing-extensions
126 uvicorn
127 typer
128 tomlkit
129 ] ++ typer.passthru.optional-dependencies.all;
130
131 passthru.optional-dependencies.oauth = [
132 authlib
133 itsdangerous
134 ];
135
136 nativeCheckInputs = [
137 pytestCheckHook
138 boto3
139 gradio-pdf
140 ffmpeg
141 ipython
142 pytest-asyncio
143 respx
144 scikit-image
145 # shap is needed as well, but breaks too often
146 torch
147 tqdm
148 transformers
149 vega-datasets
150
151 # mock calls to `shutil.which(...)`
152 (writeShellScriptBin "npm" "false")
153 ] ++ passthru.optional-dependencies.oauth ++ pydantic.passthru.optional-dependencies.email;
154
155 # Add a pytest hook skipping tests that access network, marking them as "Expected fail" (xfail).
156 # We additionally xfail FileNotFoundError, since the gradio devs often fail to upload test assets to pypi.
157 preCheck =
158 ''
159 export HOME=$TMPDIR
160 cat ${./conftest-skip-network-errors.py} >> test/conftest.py
161 ''
162 + lib.optionalString stdenv.isDarwin ''
163 # OSError: [Errno 24] Too many open files
164 ulimit -n 4096
165 '';
166
167 disabledTests = [
168 # Actually broken
169 "test_mount_gradio_app"
170
171 # requires network, it caught our xfail exception
172 "test_error_analytics_successful"
173
174 # Flaky, tries to pin dependency behaviour. Sensitive to dep versions
175 # These error only affect downstream use of the check dependencies.
176 "test_no_color"
177 "test_in_interface_as_output"
178 "test_should_warn_url_not_having_version"
179
180 # Flaky, unknown reason
181 "test_in_interface"
182
183 # shap is too often broken in nixpkgs
184 "test_shapley_text"
185
186 # fails without network
187 "test_download_if_url_correct_parse"
188
189 # tests if pip and other tools are installed
190 "test_get_executable_path"
191 ];
192 disabledTestPaths = [
193 # 100% touches network
194 "test/test_networking.py"
195 # makes pytest freeze 50% of the time
196 "test/test_interfaces.py"
197 ];
198 pytestFlagsArray = [
199 "-x" # abort on first failure
200 "-m 'not flaky'"
201 #"-W" "ignore" # uncomment for debugging help
202 ];
203
204 __darwinAllowLocalNetworking = true;
205
206 # check the binary works outside the build env
207 doInstallCheck = true;
208 postInstallCheck = ''
209 env --ignore-environment $out/bin/gradio environment >/dev/null
210 '';
211
212 pythonImportsCheck = [ "gradio" ];
213
214 # Cyclic dependencies are fun!
215 # This is gradio without gradio-client and gradio-pdf
216 passthru.sans-reverse-dependencies =
217 (gradio.override (old: {
218 gradio-client = null;
219 gradio-pdf = null;
220 })).overridePythonAttrs
221 (old: {
222 pname = old.pname + "-sans-reverse-dependencies";
223 pythonRemoveDeps = (old.pythonRemoveDeps or [ ]) ++ [ "gradio-client" ];
224 doInstallCheck = false;
225 doCheck = false;
226 pythonImportsCheck = null;
227 dontCheckRuntimeDeps = true;
228 });
229
230 meta = with lib; {
231 homepage = "https://www.gradio.app/";
232 description = "Python library for easily interacting with trained machine learning models";
233 license = licenses.asl20;
234 maintainers = with maintainers; [ pbsds ];
235 };
236}