Yw22 commited on
Commit
9e4009f
1 Parent(s): adbf393
Files changed (3) hide show
  1. app.py +3 -2
  2. helpers.py +1134 -0
  3. requirements.txt +1 -1
app.py CHANGED
@@ -4,7 +4,7 @@ import sys
4
 
5
  print("Installing correct gradio version...")
6
  os.system("pip uninstall -y gradio")
7
- os.system("pip install gradio==4.37.2")
8
  print("Installing Finished!")
9
 
10
 
@@ -131,7 +131,8 @@ if not os.path.exists("models/personalized/TUSUN.safetensors"):
131
  os.system(f'mv models/personalized/TUSUN.safetensors?download=true models/personalized/TUSUN.safetensors')
132
  print("TUSUN Download!", )
133
 
134
-
 
135
 
136
  # - - - - - examples - - - - - #
137
 
 
4
 
5
  print("Installing correct gradio version...")
6
  os.system("pip uninstall -y gradio")
7
+ os.system("pip install gradio==4.7.0")
8
  print("Installing Finished!")
9
 
10
 
 
131
  os.system(f'mv models/personalized/TUSUN.safetensors?download=true models/personalized/TUSUN.safetensors')
132
  print("TUSUN Download!", )
133
 
134
+ os.system(f'mv /usr/local/lib/python3.10/site-packages/gradio/helpers.py /usr/local/lib/python3.10/site-packages/gradio/helpers_bkp.py')
135
+ os.system(f'mv helpers.py /usr/local/lib/python3.10/site-packages/gradio/helpers.py')
136
 
137
  # - - - - - examples - - - - - #
138
 
helpers.py ADDED
@@ -0,0 +1,1134 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Defines helper methods useful for loading and caching Interface examples.
3
+ """
4
+ from __future__ import annotations
5
+
6
+ import ast
7
+ import csv
8
+ import inspect
9
+ import os
10
+ import shutil
11
+ import subprocess
12
+ import tempfile
13
+ import warnings
14
+ from functools import partial
15
+ from pathlib import Path
16
+ from typing import TYPE_CHECKING, Any, Callable, Iterable, Literal, Optional
17
+
18
+ import matplotlib.pyplot as plt
19
+ import numpy as np
20
+ import PIL
21
+ import PIL.Image
22
+ from gradio_client import utils as client_utils
23
+ from gradio_client.documentation import document, set_documentation_group
24
+ from matplotlib import animation
25
+
26
+ from gradio import components, oauth, processing_utils, routes, utils, wasm_utils
27
+ from gradio.context import Context, LocalContext
28
+ from gradio.data_classes import GradioModel, GradioRootModel
29
+ from gradio.events import EventData
30
+ from gradio.exceptions import Error
31
+ from gradio.flagging import CSVLogger
32
+
33
+ if TYPE_CHECKING: # Only import for type checking (to avoid circular imports).
34
+ from gradio.components import Component
35
+
36
+ CACHED_FOLDER = "gradio_cached_examples"
37
+ LOG_FILE = "log.csv"
38
+
39
+ set_documentation_group("helpers")
40
+
41
+
42
+ def create_examples(
43
+ examples: list[Any] | list[list[Any]] | str,
44
+ inputs: Component | list[Component],
45
+ outputs: Component | list[Component] | None = None,
46
+ fn: Callable | None = None,
47
+ cache_examples: bool = False,
48
+ examples_per_page: int = 10,
49
+ _api_mode: bool = False,
50
+ label: str | None = None,
51
+ elem_id: str | None = None,
52
+ run_on_click: bool = False,
53
+ preprocess: bool = True,
54
+ postprocess: bool = True,
55
+ api_name: str | Literal[False] = "load_example",
56
+ batch: bool = False,
57
+ ):
58
+ """Top-level synchronous function that creates Examples. Provided for backwards compatibility, i.e. so that gr.Examples(...) can be used to create the Examples component."""
59
+ examples_obj = Examples(
60
+ examples=examples,
61
+ inputs=inputs,
62
+ outputs=outputs,
63
+ fn=fn,
64
+ cache_examples=cache_examples,
65
+ examples_per_page=examples_per_page,
66
+ _api_mode=_api_mode,
67
+ label=label,
68
+ elem_id=elem_id,
69
+ run_on_click=run_on_click,
70
+ preprocess=preprocess,
71
+ postprocess=postprocess,
72
+ api_name=api_name,
73
+ batch=batch,
74
+ _initiated_directly=False,
75
+ )
76
+ examples_obj.create()
77
+ return examples_obj
78
+
79
+
80
+ @document()
81
+ class Examples:
82
+ """
83
+ This class is a wrapper over the Dataset component and can be used to create Examples
84
+ for Blocks / Interfaces. Populates the Dataset component with examples and
85
+ assigns event listener so that clicking on an example populates the input/output
86
+ components. Optionally handles example caching for fast inference.
87
+
88
+ Demos: blocks_inputs, fake_gan
89
+ Guides: more-on-examples-and-flagging, using-hugging-face-integrations, image-classification-in-pytorch, image-classification-in-tensorflow, image-classification-with-vision-transformers, create-your-own-friends-with-a-gan
90
+ """
91
+
92
+ def __init__(
93
+ self,
94
+ examples: list[Any] | list[list[Any]] | str,
95
+ inputs: Component | list[Component],
96
+ outputs: Component | list[Component] | None = None,
97
+ fn: Callable | None = None,
98
+ cache_examples: bool = False,
99
+ examples_per_page: int = 10,
100
+ _api_mode: bool = False,
101
+ label: str | None = "Examples",
102
+ elem_id: str | None = None,
103
+ run_on_click: bool = False,
104
+ preprocess: bool = True,
105
+ postprocess: bool = True,
106
+ api_name: str | Literal[False] = "load_example",
107
+ batch: bool = False,
108
+ _initiated_directly: bool = True,
109
+ ):
110
+ """
111
+ Parameters:
112
+ examples: example inputs that can be clicked to populate specific components. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component. A string path to a directory of examples can also be provided but it should be within the directory with the python file running the gradio app. If there are multiple input components and a directory is provided, a log.csv file must be present in the directory to link corresponding inputs.
113
+ inputs: the component or list of components corresponding to the examples
114
+ outputs: optionally, provide the component or list of components corresponding to the output of the examples. Required if `cache_examples` is True.
115
+ fn: optionally, provide the function to run to generate the outputs corresponding to the examples. Required if `cache_examples` is True.
116
+ cache_examples: if True, caches examples for fast runtime. If True, then `fn` and `outputs` must be provided. If `fn` is a generator function, then the last yielded value will be used as the output.
117
+ examples_per_page: how many examples to show per page.
118
+ label: the label to use for the examples component (by default, "Examples")
119
+ elem_id: an optional string that is assigned as the id of this component in the HTML DOM.
120
+ run_on_click: if cache_examples is False, clicking on an example does not run the function when an example is clicked. Set this to True to run the function when an example is clicked. Has no effect if cache_examples is True.
121
+ preprocess: if True, preprocesses the example input before running the prediction function and caching the output. Only applies if `cache_examples` is True.
122
+ postprocess: if True, postprocesses the example output after running the prediction function and before caching. Only applies if `cache_examples` is True.
123
+ api_name: Defines how the event associated with clicking on the examples appears in the API docs. Can be a string or False. If set to a string, the endpoint will be exposed in the API docs with the given name. If False, the endpoint will not be exposed in the API docs and downstream apps (including those that `gr.load` this app) will not be able to use the example function.
124
+ batch: If True, then the function should process a batch of inputs, meaning that it should accept a list of input values for each parameter. Used only if cache_examples is True.
125
+ """
126
+ if _initiated_directly:
127
+ warnings.warn(
128
+ "Please use gr.Examples(...) instead of gr.examples.Examples(...) to create the Examples.",
129
+ )
130
+
131
+ if cache_examples and (fn is None or outputs is None):
132
+ raise ValueError("If caching examples, `fn` and `outputs` must be provided")
133
+
134
+ if not isinstance(inputs, list):
135
+ inputs = [inputs]
136
+ if outputs and not isinstance(outputs, list):
137
+ outputs = [outputs]
138
+
139
+ working_directory = Path().absolute()
140
+
141
+ if examples is None:
142
+ raise ValueError("The parameter `examples` cannot be None")
143
+ elif isinstance(examples, list) and (
144
+ len(examples) == 0 or isinstance(examples[0], list)
145
+ ):
146
+ pass
147
+ elif (
148
+ isinstance(examples, list) and len(inputs) == 1
149
+ ): # If there is only one input component, examples can be provided as a regular list instead of a list of lists
150
+ examples = [[e] for e in examples]
151
+ elif isinstance(examples, str):
152
+ if not Path(examples).exists():
153
+ raise FileNotFoundError(
154
+ f"Could not find examples directory: {examples}"
155
+ )
156
+ working_directory = examples
157
+ if not (Path(examples) / LOG_FILE).exists():
158
+ if len(inputs) == 1:
159
+ examples = [[e] for e in os.listdir(examples)]
160
+ else:
161
+ raise FileNotFoundError(
162
+ "Could not find log file (required for multiple inputs): "
163
+ + LOG_FILE
164
+ )
165
+ else:
166
+ with open(Path(examples) / LOG_FILE) as logs:
167
+ examples = list(csv.reader(logs))
168
+ examples = [
169
+ examples[i][: len(inputs)] for i in range(1, len(examples))
170
+ ] # remove header and unnecessary columns
171
+
172
+ else:
173
+ raise ValueError(
174
+ "The parameter `examples` must either be a string directory or a list"
175
+ "(if there is only 1 input component) or (more generally), a nested "
176
+ "list, where each sublist represents a set of inputs."
177
+ )
178
+
179
+ input_has_examples = [False] * len(inputs)
180
+ for example in examples:
181
+ for idx, example_for_input in enumerate(example):
182
+ if example_for_input is not None:
183
+ try:
184
+ input_has_examples[idx] = True
185
+ except IndexError:
186
+ pass # If there are more example components than inputs, ignore. This can sometimes be intentional (e.g. loading from a log file where outputs and timestamps are also logged)
187
+
188
+ inputs_with_examples = [
189
+ inp for (inp, keep) in zip(inputs, input_has_examples) if keep
190
+ ]
191
+ non_none_examples = [
192
+ [ex for (ex, keep) in zip(example, input_has_examples) if keep]
193
+ for example in examples
194
+ ]
195
+
196
+ self.examples = examples
197
+ self.non_none_examples = non_none_examples
198
+ self.inputs = inputs
199
+ self.inputs_with_examples = inputs_with_examples
200
+ self.outputs = outputs or []
201
+ self.fn = fn
202
+ self.cache_examples = cache_examples
203
+ self._api_mode = _api_mode
204
+ self.preprocess = preprocess
205
+ self.postprocess = postprocess
206
+ self.api_name = api_name
207
+ self.batch = batch
208
+
209
+ with utils.set_directory(working_directory):
210
+ self.processed_examples = []
211
+ for example in examples:
212
+ sub = []
213
+ for component, sample in zip(inputs, example):
214
+ prediction_value = component.postprocess(sample)
215
+ if isinstance(prediction_value, (GradioRootModel, GradioModel)):
216
+ prediction_value = prediction_value.model_dump()
217
+ prediction_value = processing_utils.move_files_to_cache(
218
+ prediction_value, component, postprocess=True
219
+ )
220
+ sub.append(prediction_value)
221
+ self.processed_examples.append(sub)
222
+
223
+ self.non_none_processed_examples = [
224
+ [ex for (ex, keep) in zip(example, input_has_examples) if keep]
225
+ for example in self.processed_examples
226
+ ]
227
+ if cache_examples:
228
+ for example in self.examples:
229
+ if len([ex for ex in example if ex is not None]) != len(self.inputs):
230
+ warnings.warn(
231
+ "Examples are being cached but not all input components have "
232
+ "example values. This may result in an exception being thrown by "
233
+ "your function. If you do get an error while caching examples, make "
234
+ "sure all of your inputs have example values for all of your examples "
235
+ "or you provide default values for those particular parameters in your function."
236
+ )
237
+ break
238
+
239
+ from gradio import components
240
+
241
+ with utils.set_directory(working_directory):
242
+ self.dataset = components.Dataset(
243
+ components=inputs_with_examples,
244
+ samples=non_none_examples,
245
+ type="index",
246
+ label=label,
247
+ samples_per_page=examples_per_page,
248
+ elem_id=elem_id,
249
+ )
250
+
251
+ self.cached_folder = Path(CACHED_FOLDER) / str(self.dataset._id)
252
+ self.cached_file = Path(self.cached_folder) / "log.csv"
253
+ self.cache_examples = cache_examples
254
+ self.run_on_click = run_on_click
255
+
256
+ async def load_example(self, example_id):
257
+ processed_example = self.non_none_processed_examples[example_id]
258
+ if len(self.inputs_with_examples) == 1:
259
+ return update(
260
+ value=processed_example[0], **self.dataset.component_props[0]
261
+ )
262
+ return [
263
+ update(value=processed_example[i], **self.dataset.component_props[i])
264
+ for i in range(len(self.inputs_with_examples))
265
+ ]
266
+
267
+
268
+ def create(self) -> None:
269
+ """Caches the examples if self.cache_examples is True and creates the Dataset
270
+ component to hold the examples"""
271
+
272
+ if Context.root_block:
273
+ self.load_input_event = self.dataset.click(
274
+ self.load_example,
275
+ inputs=[self.dataset],
276
+ outputs=self.inputs_with_examples, # type: ignore
277
+ show_progress="hidden",
278
+ postprocess=False,
279
+ queue=False,
280
+ api_name=self.api_name, # type: ignore
281
+ )
282
+ if self.run_on_click and not self.cache_examples:
283
+ if self.fn is None:
284
+ raise ValueError("Cannot run_on_click if no function is provided")
285
+ self.load_input_event.then(
286
+ self.fn,
287
+ inputs=self.inputs, # type: ignore
288
+ outputs=self.outputs, # type: ignore
289
+ )
290
+
291
+ if self.cache_examples:
292
+ if wasm_utils.IS_WASM:
293
+ # In the Wasm mode, the `threading` module is not supported,
294
+ # so `client_utils.synchronize_async` is also not available.
295
+ # And `self.cache()` should be waited for to complete before this method returns,
296
+ # (otherwise, an error "Cannot cache examples if not in a Blocks context" will be raised anyway)
297
+ # so `eventloop.create_task(self.cache())` is also not an option.
298
+ raise wasm_utils.WasmUnsupportedError(
299
+ "Caching examples is not supported in the Wasm mode."
300
+ )
301
+ client_utils.synchronize_async(self.cache)
302
+
303
+ async def cache(self) -> None:
304
+ """
305
+ Caches all of the examples so that their predictions can be shown immediately.
306
+ """
307
+ if Context.root_block is None:
308
+ raise ValueError("Cannot cache examples if not in a Blocks context")
309
+ if Path(self.cached_file).exists():
310
+ print(
311
+ f"Using cache from '{utils.abspath(self.cached_folder)}' directory. If method or examples have changed since last caching, delete this folder to clear cache.\n"
312
+ )
313
+ else:
314
+ print(f"Caching examples at: '{utils.abspath(self.cached_folder)}'")
315
+ cache_logger = CSVLogger()
316
+
317
+ generated_values = []
318
+ if inspect.isgeneratorfunction(self.fn):
319
+
320
+ def get_final_item(*args): # type: ignore
321
+ x = None
322
+ generated_values.clear()
323
+ for x in self.fn(*args): # noqa: B007 # type: ignore
324
+ generated_values.append(x)
325
+ return x
326
+
327
+ fn = get_final_item
328
+ elif inspect.isasyncgenfunction(self.fn):
329
+
330
+ async def get_final_item(*args):
331
+ x = None
332
+ generated_values.clear()
333
+ async for x in self.fn(*args): # noqa: B007 # type: ignore
334
+ generated_values.append(x)
335
+ return x
336
+
337
+ fn = get_final_item
338
+ else:
339
+ fn = self.fn
340
+
341
+ # create a fake dependency to process the examples and get the predictions
342
+ from gradio.events import EventListenerMethod
343
+
344
+ dependency, fn_index = Context.root_block.set_event_trigger(
345
+ [EventListenerMethod(Context.root_block, "load")],
346
+ fn=fn,
347
+ inputs=self.inputs_with_examples, # type: ignore
348
+ outputs=self.outputs, # type: ignore
349
+ preprocess=self.preprocess and not self._api_mode,
350
+ postprocess=self.postprocess and not self._api_mode,
351
+ batch=self.batch,
352
+ )
353
+
354
+ assert self.outputs is not None
355
+ cache_logger.setup(self.outputs, self.cached_folder)
356
+ for example_id, _ in enumerate(self.examples):
357
+ print(f"Caching example {example_id + 1}/{len(self.examples)}")
358
+ processed_input = self.processed_examples[example_id]
359
+ if self.batch:
360
+ processed_input = [[value] for value in processed_input]
361
+ with utils.MatplotlibBackendMananger():
362
+ prediction = await Context.root_block.process_api(
363
+ fn_index=fn_index,
364
+ inputs=processed_input,
365
+ request=None,
366
+ )
367
+ output = prediction["data"]
368
+ if len(generated_values):
369
+ output = merge_generated_values_into_output(
370
+ self.outputs, generated_values, output
371
+ )
372
+
373
+ if self.batch:
374
+ output = [value[0] for value in output]
375
+ cache_logger.flag(output)
376
+ # Remove the "fake_event" to prevent bugs in loading interfaces from spaces
377
+ Context.root_block.dependencies.remove(dependency)
378
+ Context.root_block.fns.pop(fn_index)
379
+
380
+ # Remove the original load_input_event and replace it with one that
381
+ # also populates the input. We do it this way to to allow the cache()
382
+ # method to be called independently of the create() method
383
+ index = Context.root_block.dependencies.index(self.load_input_event)
384
+ Context.root_block.dependencies.pop(index)
385
+ Context.root_block.fns.pop(index)
386
+
387
+ def load_example(example_id):
388
+ processed_example = self.non_none_processed_examples[
389
+ example_id
390
+ ] + self.load_from_cache(example_id)
391
+ return utils.resolve_singleton(processed_example)
392
+
393
+ self.load_input_event = self.dataset.click(
394
+ load_example,
395
+ inputs=[self.dataset],
396
+ outputs=self.inputs_with_examples + self.outputs, # type: ignore
397
+ show_progress="hidden",
398
+ postprocess=False,
399
+ queue=False,
400
+ api_name=self.api_name, # type: ignore
401
+ )
402
+
403
+ def load_from_cache(self, example_id: int) -> list[Any]:
404
+ """Loads a particular cached example for the interface.
405
+ Parameters:
406
+ example_id: The id of the example to process (zero-indexed).
407
+ """
408
+ with open(self.cached_file, encoding="utf-8") as cache:
409
+ examples = list(csv.reader(cache))
410
+ example = examples[example_id + 1] # +1 to adjust for header
411
+ output = []
412
+ assert self.outputs is not None
413
+ for component, value in zip(self.outputs, example):
414
+ value_to_use = value
415
+ try:
416
+ value_as_dict = ast.literal_eval(value)
417
+ # File components that output multiple files get saved as a python list
418
+ # need to pass the parsed list to serialize
419
+ # TODO: Better file serialization in 4.0
420
+ if isinstance(value_as_dict, list) and isinstance(
421
+ component, components.File
422
+ ):
423
+ value_to_use = value_as_dict
424
+ assert utils.is_update(value_as_dict)
425
+ output.append(value_as_dict)
426
+ except (ValueError, TypeError, SyntaxError, AssertionError):
427
+ output.append(
428
+ component.read_from_flag(
429
+ value_to_use,
430
+ self.cached_folder,
431
+ )
432
+ )
433
+ return output
434
+
435
+
436
+ def merge_generated_values_into_output(
437
+ components: list[Component], generated_values: list, output: list
438
+ ):
439
+ from gradio.components.base import StreamingOutput
440
+
441
+ for output_index, output_component in enumerate(components):
442
+ if isinstance(output_component, StreamingOutput) and output_component.streaming:
443
+ binary_chunks = []
444
+ for i, chunk in enumerate(generated_values):
445
+ if len(components) > 1:
446
+ chunk = chunk[output_index]
447
+ processed_chunk = output_component.postprocess(chunk)
448
+ if isinstance(processed_chunk, (GradioModel, GradioRootModel)):
449
+ processed_chunk = processed_chunk.model_dump()
450
+ binary_chunks.append(
451
+ output_component.stream_output(processed_chunk, "", i == 0)[0]
452
+ )
453
+ binary_data = b"".join(binary_chunks)
454
+ tempdir = os.environ.get("GRADIO_TEMP_DIR") or str(
455
+ Path(tempfile.gettempdir()) / "gradio"
456
+ )
457
+ os.makedirs(tempdir, exist_ok=True)
458
+ temp_file = tempfile.NamedTemporaryFile(dir=tempdir, delete=False)
459
+ with open(temp_file.name, "wb") as f:
460
+ f.write(binary_data)
461
+
462
+ output[output_index] = {
463
+ "path": temp_file.name,
464
+ }
465
+
466
+ return output
467
+
468
+
469
+ class TrackedIterable:
470
+ def __init__(
471
+ self,
472
+ iterable: Iterable | None,
473
+ index: int | None,
474
+ length: int | None,
475
+ desc: str | None,
476
+ unit: str | None,
477
+ _tqdm=None,
478
+ progress: float | None = None,
479
+ ) -> None:
480
+ self.iterable = iterable
481
+ self.index = index
482
+ self.length = length
483
+ self.desc = desc
484
+ self.unit = unit
485
+ self._tqdm = _tqdm
486
+ self.progress = progress
487
+
488
+
489
+ @document("__call__", "tqdm")
490
+ class Progress(Iterable):
491
+ """
492
+ The Progress class provides a custom progress tracker that is used in a function signature.
493
+ To attach a Progress tracker to a function, simply add a parameter right after the input parameters that has a default value set to a `gradio.Progress()` instance.
494
+ The Progress tracker can then be updated in the function by calling the Progress object or using the `tqdm` method on an Iterable.
495
+ The Progress tracker is currently only available with `queue()`.
496
+ Example:
497
+ import gradio as gr
498
+ import time
499
+ def my_function(x, progress=gr.Progress()):
500
+ progress(0, desc="Starting...")
501
+ time.sleep(1)
502
+ for i in progress.tqdm(range(100)):
503
+ time.sleep(0.1)
504
+ return x
505
+ gr.Interface(my_function, gr.Textbox(), gr.Textbox()).queue().launch()
506
+ Demos: progress
507
+ """
508
+
509
+ def __init__(
510
+ self,
511
+ track_tqdm: bool = False,
512
+ ):
513
+ """
514
+ Parameters:
515
+ track_tqdm: If True, the Progress object will track any tqdm.tqdm iterations with the tqdm library in the function.
516
+ """
517
+ if track_tqdm:
518
+ patch_tqdm()
519
+ self.track_tqdm = track_tqdm
520
+ self.iterables: list[TrackedIterable] = []
521
+
522
+ def __len__(self):
523
+ return self.iterables[-1].length
524
+
525
+ def __iter__(self):
526
+ return self
527
+
528
+ def __next__(self):
529
+ """
530
+ Updates progress tracker with next item in iterable.
531
+ """
532
+ callback = self._progress_callback()
533
+ if callback:
534
+ current_iterable = self.iterables[-1]
535
+ while (
536
+ not hasattr(current_iterable.iterable, "__next__")
537
+ and len(self.iterables) > 0
538
+ ):
539
+ current_iterable = self.iterables.pop()
540
+ callback(self.iterables)
541
+ if current_iterable.index is None:
542
+ raise IndexError("Index not set.")
543
+ current_iterable.index += 1
544
+ try:
545
+ return next(current_iterable.iterable) # type: ignore
546
+ except StopIteration:
547
+ self.iterables.pop()
548
+ raise
549
+ else:
550
+ return self
551
+
552
+ def __call__(
553
+ self,
554
+ progress: float | tuple[int, int | None] | None,
555
+ desc: str | None = None,
556
+ total: int | None = None,
557
+ unit: str = "steps",
558
+ _tqdm=None,
559
+ ):
560
+ """
561
+ Updates progress tracker with progress and message text.
562
+ Parameters:
563
+ progress: If float, should be between 0 and 1 representing completion. If Tuple, first number represents steps completed, and second value represents total steps or None if unknown. If None, hides progress bar.
564
+ desc: description to display.
565
+ total: estimated total number of steps.
566
+ unit: unit of iterations.
567
+ """
568
+ callback = self._progress_callback()
569
+ if callback:
570
+ if isinstance(progress, tuple):
571
+ index, total = progress
572
+ progress = None
573
+ else:
574
+ index = None
575
+ callback(
576
+ self.iterables
577
+ + [TrackedIterable(None, index, total, desc, unit, _tqdm, progress)]
578
+ )
579
+ else:
580
+ return progress
581
+
582
+ def tqdm(
583
+ self,
584
+ iterable: Iterable | None,
585
+ desc: str | None = None,
586
+ total: int | None = None,
587
+ unit: str = "steps",
588
+ _tqdm=None,
589
+ ):
590
+ """
591
+ Attaches progress tracker to iterable, like tqdm.
592
+ Parameters:
593
+ iterable: iterable to attach progress tracker to.
594
+ desc: description to display.
595
+ total: estimated total number of steps.
596
+ unit: unit of iterations.
597
+ """
598
+ callback = self._progress_callback()
599
+ if callback:
600
+ if iterable is None:
601
+ new_iterable = TrackedIterable(None, 0, total, desc, unit, _tqdm)
602
+ self.iterables.append(new_iterable)
603
+ callback(self.iterables)
604
+ return self
605
+ length = len(iterable) if hasattr(iterable, "__len__") else None # type: ignore
606
+ self.iterables.append(
607
+ TrackedIterable(iter(iterable), 0, length, desc, unit, _tqdm)
608
+ )
609
+ return self
610
+
611
+ def update(self, n=1):
612
+ """
613
+ Increases latest iterable with specified number of steps.
614
+ Parameters:
615
+ n: number of steps completed.
616
+ """
617
+ callback = self._progress_callback()
618
+ if callback and len(self.iterables) > 0:
619
+ current_iterable = self.iterables[-1]
620
+ if current_iterable.index is None:
621
+ raise IndexError("Index not set.")
622
+ current_iterable.index += n
623
+ callback(self.iterables)
624
+ else:
625
+ return
626
+
627
+ def close(self, _tqdm):
628
+ """
629
+ Removes iterable with given _tqdm.
630
+ """
631
+ callback = self._progress_callback()
632
+ if callback:
633
+ for i in range(len(self.iterables)):
634
+ if id(self.iterables[i]._tqdm) == id(_tqdm):
635
+ self.iterables.pop(i)
636
+ break
637
+ callback(self.iterables)
638
+ else:
639
+ return
640
+
641
+ @staticmethod
642
+ def _progress_callback():
643
+ blocks = LocalContext.blocks.get()
644
+ event_id = LocalContext.event_id.get()
645
+ if not (blocks and event_id):
646
+ return None
647
+ return partial(blocks._queue.set_progress, event_id)
648
+
649
+
650
+ def patch_tqdm() -> None:
651
+ try:
652
+ _tqdm = __import__("tqdm")
653
+ except ModuleNotFoundError:
654
+ return
655
+
656
+ def init_tqdm(
657
+ self, iterable=None, desc=None, total=None, unit="steps", *args, **kwargs
658
+ ):
659
+ self._progress = LocalContext.progress.get()
660
+ if self._progress is not None:
661
+ self._progress.tqdm(iterable, desc, total, unit, _tqdm=self)
662
+ kwargs["file"] = open(os.devnull, "w") # noqa: SIM115
663
+ self.__init__orig__(iterable, desc, total, *args, unit=unit, **kwargs)
664
+
665
+ def iter_tqdm(self):
666
+ if self._progress is not None:
667
+ return self._progress
668
+ return self.__iter__orig__()
669
+
670
+ def update_tqdm(self, n=1):
671
+ if self._progress is not None:
672
+ self._progress.update(n)
673
+ return self.__update__orig__(n)
674
+
675
+ def close_tqdm(self):
676
+ if self._progress is not None:
677
+ self._progress.close(self)
678
+ return self.__close__orig__()
679
+
680
+ def exit_tqdm(self, exc_type, exc_value, traceback):
681
+ if self._progress is not None:
682
+ self._progress.close(self)
683
+ return self.__exit__orig__(exc_type, exc_value, traceback)
684
+
685
+ # Backup
686
+ if not hasattr(_tqdm.tqdm, "__init__orig__"):
687
+ _tqdm.tqdm.__init__orig__ = _tqdm.tqdm.__init__
688
+ if not hasattr(_tqdm.tqdm, "__update__orig__"):
689
+ _tqdm.tqdm.__update__orig__ = _tqdm.tqdm.update
690
+ if not hasattr(_tqdm.tqdm, "__close__orig__"):
691
+ _tqdm.tqdm.__close__orig__ = _tqdm.tqdm.close
692
+ if not hasattr(_tqdm.tqdm, "__exit__orig__"):
693
+ _tqdm.tqdm.__exit__orig__ = _tqdm.tqdm.__exit__
694
+ if not hasattr(_tqdm.tqdm, "__iter__orig__"):
695
+ _tqdm.tqdm.__iter__orig__ = _tqdm.tqdm.__iter__
696
+
697
+ # Patch
698
+ _tqdm.tqdm.__init__ = init_tqdm
699
+ _tqdm.tqdm.update = update_tqdm
700
+ _tqdm.tqdm.close = close_tqdm
701
+ _tqdm.tqdm.__exit__ = exit_tqdm
702
+ _tqdm.tqdm.__iter__ = iter_tqdm
703
+
704
+ if hasattr(_tqdm, "auto") and hasattr(_tqdm.auto, "tqdm"):
705
+ _tqdm.auto.tqdm = _tqdm.tqdm
706
+
707
+
708
+ def create_tracker(fn, track_tqdm):
709
+ progress = Progress(track_tqdm=track_tqdm)
710
+ if not track_tqdm:
711
+ return progress, fn
712
+ return progress, utils.function_wrapper(
713
+ f=fn,
714
+ before_fn=LocalContext.progress.set,
715
+ before_args=(progress,),
716
+ after_fn=LocalContext.progress.set,
717
+ after_args=(None,),
718
+ )
719
+
720
+
721
+ def special_args(
722
+ fn: Callable,
723
+ inputs: list[Any] | None = None,
724
+ request: routes.Request | None = None,
725
+ event_data: EventData | None = None,
726
+ ) -> tuple[list, int | None, int | None]:
727
+ """
728
+ Checks if function has special arguments Request or EventData (via annotation) or Progress (via default value).
729
+ If inputs is provided, these values will be loaded into the inputs array.
730
+ Parameters:
731
+ fn: function to check.
732
+ inputs: array to load special arguments into.
733
+ request: request to load into inputs.
734
+ event_data: event-related data to load into inputs.
735
+ Returns:
736
+ updated inputs, progress index, event data index.
737
+ """
738
+ try:
739
+ signature = inspect.signature(fn)
740
+ except ValueError:
741
+ return inputs or [], None, None
742
+ type_hints = utils.get_type_hints(fn)
743
+ positional_args = []
744
+ for param in signature.parameters.values():
745
+ if param.kind not in (param.POSITIONAL_ONLY, param.POSITIONAL_OR_KEYWORD):
746
+ break
747
+ positional_args.append(param)
748
+ progress_index = None
749
+ event_data_index = None
750
+ for i, param in enumerate(positional_args):
751
+ type_hint = type_hints.get(param.name)
752
+ if isinstance(param.default, Progress):
753
+ progress_index = i
754
+ if inputs is not None:
755
+ inputs.insert(i, param.default)
756
+ elif type_hint == routes.Request:
757
+ if inputs is not None:
758
+ inputs.insert(i, request)
759
+ elif (
760
+ type_hint == Optional[oauth.OAuthProfile]
761
+ or type_hint == oauth.OAuthProfile
762
+ # Note: "OAuthProfile | None" is equals to Optional[OAuthProfile] in Python
763
+ # => it is automatically handled as well by the above condition
764
+ # (adding explicit "OAuthProfile | None" would break in Python3.9)
765
+ ):
766
+ if inputs is not None:
767
+ # Retrieve session from gr.Request, if it exists (i.e. if user is logged in)
768
+ session = (
769
+ # request.session (if fastapi.Request obj i.e. direct call)
770
+ getattr(request, "session", {})
771
+ or
772
+ # or request.request.session (if gr.Request obj i.e. websocket call)
773
+ getattr(getattr(request, "request", None), "session", {})
774
+ )
775
+ oauth_profile = (
776
+ session["oauth_profile"] if "oauth_profile" in session else None
777
+ )
778
+ if type_hint == oauth.OAuthProfile and oauth_profile is None:
779
+ raise Error(
780
+ "This action requires a logged in user. Please sign in and retry."
781
+ )
782
+ inputs.insert(i, oauth_profile)
783
+ elif (
784
+ type_hint
785
+ and inspect.isclass(type_hint)
786
+ and issubclass(type_hint, EventData)
787
+ ):
788
+ event_data_index = i
789
+ if inputs is not None and event_data is not None:
790
+ inputs.insert(i, type_hint(event_data.target, event_data._data))
791
+ elif (
792
+ param.default is not param.empty and inputs is not None and len(inputs) <= i
793
+ ):
794
+ inputs.insert(i, param.default)
795
+ if inputs is not None:
796
+ while len(inputs) < len(positional_args):
797
+ i = len(inputs)
798
+ param = positional_args[i]
799
+ if param.default == param.empty:
800
+ warnings.warn("Unexpected argument. Filling with None.")
801
+ inputs.append(None)
802
+ else:
803
+ inputs.append(param.default)
804
+ return inputs or [], progress_index, event_data_index
805
+
806
+
807
+ def update(
808
+ elem_id: str | None = None,
809
+ elem_classes: list[str] | str | None = None,
810
+ visible: bool | None = None,
811
+ **kwargs,
812
+ ) -> dict:
813
+ """
814
+ Updates a component's properties. When a function passed into a Gradio Interface or a Blocks events returns a value, it typically updates the value of the output component. But it is also possible to update the *properties* of an output component (such as the number of lines of a `Textbox` or the visibility of an `Row`) by returning a component and passing in the parameters to update in the constructor of the component. Alternatively, you can return `gr.update(...)` with any arbitrary parameters to update. (This is useful as a shorthand or if the same function can be called with different components to update.)
815
+
816
+ Parameters:
817
+ elem_id: Use this to update the id of the component in the HTML DOM
818
+ elem_classes: Use this to update the classes of the component in the HTML DOM
819
+ visible: Use this to update the visibility of the component
820
+ kwargs: Any other keyword arguments to update the component's properties.
821
+ Example:
822
+ import gradio as gr
823
+ with gr.Blocks() as demo:
824
+ radio = gr.Radio([1, 2, 4], label="Set the value of the number")
825
+ number = gr.Number(value=2, interactive=True)
826
+ radio.change(fn=lambda value: gr.update(value=value), inputs=radio, outputs=number)
827
+ demo.launch()
828
+ """
829
+ kwargs["__type__"] = "update"
830
+ if elem_id is not None:
831
+ kwargs["elem_id"] = elem_id
832
+ if elem_classes is not None:
833
+ kwargs["elem_classes"] = elem_classes
834
+ if visible is not None:
835
+ kwargs["visible"] = visible
836
+ return kwargs
837
+
838
+
839
+ def skip() -> dict:
840
+ return {"__type__": "update"}
841
+
842
+
843
+ @document()
844
+ def make_waveform(
845
+ audio: str | tuple[int, np.ndarray],
846
+ *,
847
+ bg_color: str = "#f3f4f6",
848
+ bg_image: str | None = None,
849
+ fg_alpha: float = 0.75,
850
+ bars_color: str | tuple[str, str] = ("#fbbf24", "#ea580c"),
851
+ bar_count: int = 50,
852
+ bar_width: float = 0.6,
853
+ animate: bool = False,
854
+ ) -> str:
855
+ """
856
+ Generates a waveform video from an audio file. Useful for creating an easy to share audio visualization. The output should be passed into a `gr.Video` component.
857
+ Parameters:
858
+ audio: Audio file path or tuple of (sample_rate, audio_data)
859
+ bg_color: Background color of waveform (ignored if bg_image is provided)
860
+ bg_image: Background image of waveform
861
+ fg_alpha: Opacity of foreground waveform
862
+ bars_color: Color of waveform bars. Can be a single color or a tuple of (start_color, end_color) of gradient
863
+ bar_count: Number of bars in waveform
864
+ bar_width: Width of bars in waveform. 1 represents full width, 0.5 represents half width, etc.
865
+ animate: If true, the audio waveform overlay will be animated, if false, it will be static.
866
+ Returns:
867
+ A filepath to the output video in mp4 format.
868
+ """
869
+ if isinstance(audio, str):
870
+ audio_file = audio
871
+ audio = processing_utils.audio_from_file(audio)
872
+ else:
873
+ tmp_wav = tempfile.NamedTemporaryFile(suffix=".wav", delete=False)
874
+ processing_utils.audio_to_file(audio[0], audio[1], tmp_wav.name, format="wav")
875
+ audio_file = tmp_wav.name
876
+
877
+ if not os.path.isfile(audio_file):
878
+ raise ValueError("Audio file not found.")
879
+
880
+ ffmpeg = shutil.which("ffmpeg")
881
+ if not ffmpeg:
882
+ raise RuntimeError("ffmpeg not found.")
883
+
884
+ duration = round(len(audio[1]) / audio[0], 4)
885
+
886
+ # Helper methods to create waveform
887
+ def hex_to_rgb(hex_str):
888
+ return [int(hex_str[i : i + 2], 16) for i in range(1, 6, 2)]
889
+
890
+ def get_color_gradient(c1, c2, n):
891
+ assert n > 1
892
+ c1_rgb = np.array(hex_to_rgb(c1)) / 255
893
+ c2_rgb = np.array(hex_to_rgb(c2)) / 255
894
+ mix_pcts = [x / (n - 1) for x in range(n)]
895
+ rgb_colors = [((1 - mix) * c1_rgb + (mix * c2_rgb)) for mix in mix_pcts]
896
+ return [
897
+ "#" + "".join(f"{int(round(val * 255)):02x}" for val in item)
898
+ for item in rgb_colors
899
+ ]
900
+
901
+ # Reshape audio to have a fixed number of bars
902
+ samples = audio[1]
903
+ if len(samples.shape) > 1:
904
+ samples = np.mean(samples, 1)
905
+ bins_to_pad = bar_count - (len(samples) % bar_count)
906
+ samples = np.pad(samples, [(0, bins_to_pad)])
907
+ samples = np.reshape(samples, (bar_count, -1))
908
+ samples = np.abs(samples)
909
+ samples = np.max(samples, 1)
910
+
911
+ with utils.MatplotlibBackendMananger():
912
+ plt.clf()
913
+ # Plot waveform
914
+ color = (
915
+ bars_color
916
+ if isinstance(bars_color, str)
917
+ else get_color_gradient(bars_color[0], bars_color[1], bar_count)
918
+ )
919
+
920
+ if animate:
921
+ fig = plt.figure(figsize=(5, 1), dpi=200, frameon=False)
922
+ fig.subplots_adjust(left=0, bottom=0, right=1, top=1)
923
+ plt.axis("off")
924
+ plt.margins(x=0)
925
+
926
+ bar_alpha = fg_alpha if animate else 1.0
927
+ barcollection = plt.bar(
928
+ np.arange(0, bar_count),
929
+ samples * 2,
930
+ bottom=(-1 * samples),
931
+ width=bar_width,
932
+ color=color,
933
+ alpha=bar_alpha,
934
+ )
935
+
936
+ tmp_img = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
937
+
938
+ savefig_kwargs: dict[str, Any] = {"bbox_inches": "tight"}
939
+ if bg_image is not None:
940
+ savefig_kwargs["transparent"] = True
941
+ if animate:
942
+ savefig_kwargs["facecolor"] = "none"
943
+ else:
944
+ savefig_kwargs["facecolor"] = bg_color
945
+ plt.savefig(tmp_img.name, **savefig_kwargs)
946
+
947
+ if not animate:
948
+ waveform_img = PIL.Image.open(tmp_img.name)
949
+ waveform_img = waveform_img.resize((1000, 400))
950
+
951
+ # Composite waveform with background image
952
+ if bg_image is not None:
953
+ waveform_array = np.array(waveform_img)
954
+ waveform_array[:, :, 3] = waveform_array[:, :, 3] * fg_alpha
955
+ waveform_img = PIL.Image.fromarray(waveform_array)
956
+
957
+ bg_img = PIL.Image.open(bg_image)
958
+ waveform_width, waveform_height = waveform_img.size
959
+ bg_width, bg_height = bg_img.size
960
+ if waveform_width != bg_width:
961
+ bg_img = bg_img.resize(
962
+ (
963
+ waveform_width,
964
+ 2 * int(bg_height * waveform_width / bg_width / 2),
965
+ )
966
+ )
967
+ bg_width, bg_height = bg_img.size
968
+ composite_height = max(bg_height, waveform_height)
969
+ composite = PIL.Image.new(
970
+ "RGBA", (waveform_width, composite_height), "#FFFFFF"
971
+ )
972
+ composite.paste(bg_img, (0, composite_height - bg_height))
973
+ composite.paste(
974
+ waveform_img, (0, composite_height - waveform_height), waveform_img
975
+ )
976
+ composite.save(tmp_img.name)
977
+ img_width, img_height = composite.size
978
+ else:
979
+ img_width, img_height = waveform_img.size
980
+ waveform_img.save(tmp_img.name)
981
+ else:
982
+
983
+ def _animate(_):
984
+ for idx, b in enumerate(barcollection):
985
+ rand_height = np.random.uniform(0.8, 1.2)
986
+ b.set_height(samples[idx] * rand_height * 2)
987
+ b.set_y((-rand_height * samples)[idx])
988
+
989
+ frames = int(duration * 10)
990
+ anim = animation.FuncAnimation(
991
+ fig, # type: ignore
992
+ _animate,
993
+ repeat=False,
994
+ blit=False,
995
+ frames=frames,
996
+ interval=100,
997
+ )
998
+ anim.save(
999
+ tmp_img.name,
1000
+ writer="pillow",
1001
+ fps=10,
1002
+ codec="png",
1003
+ savefig_kwargs=savefig_kwargs,
1004
+ )
1005
+
1006
+ # Convert waveform to video with ffmpeg
1007
+ output_mp4 = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
1008
+
1009
+ if animate and bg_image is not None:
1010
+ ffmpeg_cmd = [
1011
+ ffmpeg,
1012
+ "-loop",
1013
+ "1",
1014
+ "-i",
1015
+ bg_image,
1016
+ "-i",
1017
+ tmp_img.name,
1018
+ "-i",
1019
+ audio_file,
1020
+ "-filter_complex",
1021
+ "[0:v]scale=w=trunc(iw/2)*2:h=trunc(ih/2)*2[bg];[1:v]format=rgba,colorchannelmixer=aa=1.0[ov];[bg][ov]overlay=(main_w-overlay_w*0.9)/2:main_h-overlay_h*0.9/2[output]",
1022
+ "-t",
1023
+ str(duration),
1024
+ "-map",
1025
+ "[output]",
1026
+ "-map",
1027
+ "2:a",
1028
+ "-c:v",
1029
+ "libx264",
1030
+ "-c:a",
1031
+ "aac",
1032
+ "-shortest",
1033
+ "-y",
1034
+ output_mp4.name,
1035
+ ]
1036
+ elif animate and bg_image is None:
1037
+ ffmpeg_cmd = [
1038
+ ffmpeg,
1039
+ "-i",
1040
+ tmp_img.name,
1041
+ "-i",
1042
+ audio_file,
1043
+ "-filter_complex",
1044
+ "[0:v][1:a]concat=n=1:v=1:a=1[v];[v]scale=1000:400,format=yuv420p[v_scaled]",
1045
+ "-map",
1046
+ "[v_scaled]",
1047
+ "-map",
1048
+ "1:a",
1049
+ "-c:v",
1050
+ "libx264",
1051
+ "-c:a",
1052
+ "aac",
1053
+ "-shortest",
1054
+ "-y",
1055
+ output_mp4.name,
1056
+ ]
1057
+ else:
1058
+ ffmpeg_cmd = [
1059
+ ffmpeg,
1060
+ "-loop",
1061
+ "1",
1062
+ "-i",
1063
+ tmp_img.name,
1064
+ "-i",
1065
+ audio_file,
1066
+ "-vf",
1067
+ f"color=c=#FFFFFF77:s={img_width}x{img_height}[bar];[0][bar]overlay=-w+(w/{duration})*t:H-h:shortest=1", # type: ignore
1068
+ "-t",
1069
+ str(duration),
1070
+ "-y",
1071
+ output_mp4.name,
1072
+ ]
1073
+
1074
+ subprocess.check_call(ffmpeg_cmd)
1075
+ return output_mp4.name
1076
+
1077
+
1078
+ def log_message(message: str, level: Literal["info", "warning"] = "info"):
1079
+ from gradio.context import LocalContext
1080
+
1081
+ blocks = LocalContext.blocks.get()
1082
+ event_id = LocalContext.event_id.get()
1083
+ if blocks is None or event_id is None:
1084
+ # Function called outside of Gradio if blocks is None
1085
+ # Or from /api/predict if event_id is None
1086
+ if level == "info":
1087
+ print(message)
1088
+ elif level == "warning":
1089
+ warnings.warn(message)
1090
+ return
1091
+ blocks._queue.log_message(event_id=event_id, log=message, level=level)
1092
+
1093
+
1094
+ set_documentation_group("modals")
1095
+
1096
+
1097
+ @document()
1098
+ def Warning(message: str = "Warning issued."): # noqa: N802
1099
+ """
1100
+ This function allows you to pass custom warning messages to the user. You can do so simply by writing `gr.Warning('message here')` in your function, and when that line is executed the custom message will appear in a modal on the demo. The modal is yellow by default and has the heading: "Warning." Queue must be enabled for this behavior; otherwise, the warning will be printed to the console using the `warnings` library.
1101
+ Demos: blocks_chained_events
1102
+ Parameters:
1103
+ message: The warning message to be displayed to the user.
1104
+ Example:
1105
+ import gradio as gr
1106
+ def hello_world():
1107
+ gr.Warning('This is a warning message.')
1108
+ return "hello world"
1109
+ with gr.Blocks() as demo:
1110
+ md = gr.Markdown()
1111
+ demo.load(hello_world, inputs=None, outputs=[md])
1112
+ demo.queue().launch()
1113
+ """
1114
+ log_message(message, level="warning")
1115
+
1116
+
1117
+ @document()
1118
+ def Info(message: str = "Info issued."): # noqa: N802
1119
+ """
1120
+ This function allows you to pass custom info messages to the user. You can do so simply by writing `gr.Info('message here')` in your function, and when that line is executed the custom message will appear in a modal on the demo. The modal is gray by default and has the heading: "Info." Queue must be enabled for this behavior; otherwise, the message will be printed to the console.
1121
+ Demos: blocks_chained_events
1122
+ Parameters:
1123
+ message: The info message to be displayed to the user.
1124
+ Example:
1125
+ import gradio as gr
1126
+ def hello_world():
1127
+ gr.Info('This is some info.')
1128
+ return "hello world"
1129
+ with gr.Blocks() as demo:
1130
+ md = gr.Markdown()
1131
+ demo.load(hello_world, inputs=None, outputs=[md])
1132
+ demo.queue().launch()
1133
+ """
1134
+ log_message(message, level="info")
requirements.txt CHANGED
@@ -2,7 +2,7 @@ torch
2
  torchvision
3
  torchaudio
4
  transformers==4.32.1
5
- gradio==4.37.2
6
  ftfy
7
  tensorboard
8
  datasets
 
2
  torchvision
3
  torchaudio
4
  transformers==4.32.1
5
+ gradio==4.7.0
6
  ftfy
7
  tensorboard
8
  datasets