Lazarus commited on
Commit
3673342
1 Parent(s): b43a58a

Refactor media output components and setup listeners

Browse files
Files changed (2) hide show
  1. app.py +8 -5
  2. functions.py +32 -100
app.py CHANGED
@@ -122,12 +122,15 @@ with gr.Blocks(
122
  audio_button = gr.Button("Audio")
123
  file_button = gr.Button("File")
124
  media_output_audio = gr.Audio(
125
- type="filepath", label="Output", visible=True, interactive=False
 
 
 
126
  )
127
  media_output_video = gr.Video(
128
- label="Output", visible=False, height=300
129
  )
130
- media_output_file = gr.File(label="Output", visible=False)
131
  with gr.Row() as command_output:
132
  output_textbox = gr.Code(
133
  value="$ echo 'Hello, World!'",
@@ -250,9 +253,9 @@ with gr.Blocks(
250
  ffmpeg_commands = CommandBuilder(
251
  inputs_clip, video_inputs, audio_inputs, filter_inputs, filter_inputs_1
252
  )
253
- ffmpeg_commands.do()
254
  # pprint(ffmpeg_commands.commands)
255
  ffmpeg_commands.update(output_textbox)
256
 
257
  if __name__ == "__main__":
258
- demo.launch(show_error=True)
 
122
  audio_button = gr.Button("Audio")
123
  file_button = gr.Button("File")
124
  media_output_audio = gr.Audio(
125
+ type="filepath",
126
+ label="Audio",
127
+ visible=False,
128
+ interactive=False,
129
  )
130
  media_output_video = gr.Video(
131
+ label="Video", visible=True, height=300
132
  )
133
+ media_output_file = gr.File(label="File", visible=False)
134
  with gr.Row() as command_output:
135
  output_textbox = gr.Code(
136
  value="$ echo 'Hello, World!'",
 
253
  ffmpeg_commands = CommandBuilder(
254
  inputs_clip, video_inputs, audio_inputs, filter_inputs, filter_inputs_1
255
  )
256
+ ffmpeg_commands.setup_listener()
257
  # pprint(ffmpeg_commands.commands)
258
  ffmpeg_commands.update(output_textbox)
259
 
260
  if __name__ == "__main__":
261
+ demo.launch(show_error=True, max_threads=300)
functions.py CHANGED
@@ -58,25 +58,6 @@ class CommandBuilder:
58
  a function to each component in the context
59
  to build an array of ffmpeg commands"""
60
 
61
- def __call__(self, *args, **kwds):
62
- return [i.value for i in self._component]
63
-
64
- def do(self, *inputs, **kwds):
65
- for comp in self._component:
66
- if comp.label is not None:
67
- # self.changefunc(comp, comp.value)
68
- # print(
69
- # comp.label,
70
- # comp,
71
- # )
72
- state = gr.State(value=comp.label)
73
- comp.change(fn=self.changefunc, inputs=[state, comp], outputs=[])
74
-
75
- def reset(self):
76
- self.output_dict = {"vf": {}, "af": {}}
77
- self.commands = ""
78
- self.vf, self.af, self.extra = ([] for _ in range(3))
79
-
80
  def __init__(self, *inputs: gr.Row | gr.Column) -> None:
81
  """
82
  Parameters:
@@ -85,8 +66,6 @@ class CommandBuilder:
85
 
86
  self.output_dict = {"vf": {}, "af": {}}
87
  self.formatoutputdict = {"vf": {}, "af": {}}
88
- # state=gr.Variable()
89
- # state2=gr.Variable()
90
 
91
  self._component: List[Component] = []
92
  self.vf, self.af, self.extra = ([] for _ in range(3))
@@ -95,8 +74,6 @@ class CommandBuilder:
95
  return None
96
  for i in inputs:
97
  self._component += self._get_component_instance(i)
98
- # print(self._component, "component")
99
- # print(len(self._component), "length")
100
  # for comp in self._component:
101
  # state = gr.State()
102
  # if comp.label is not None:
@@ -105,6 +82,29 @@ class CommandBuilder:
105
  # self.changefunc(state.value, comp.value)
106
  # comp.change(fn=self.changefunc, inputs=[state, comp], outputs=[])
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  def changefunc(self, component_label: str | None, new_value=""):
109
  label, *_ = (
110
  component_label.strip(": \n").lower().split()
@@ -224,16 +224,11 @@ class CommandBuilder:
224
  if not hasattr(i, "children"):
225
  # res.append(gr.components.get_component_instance(i,render=True))
226
  # if isinstance(i, gr.components.Component):
227
- # print(gr.components.get_component_instance(i, render=True))
228
- res += [gr.components.get_component_instance(i, render=True)]
229
- # print(res)
230
- # elif hasattr(i, "children"):
231
  else:
232
- # if isinstance(i, gr.Blocks):
233
  res += self._get_component_instance(i)
234
  # print(res)
235
  return res
236
- # return [gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i, "children")]
237
 
238
  def set_video_filters(self, options):
239
  value = self.output_dict.get(options, "-")
@@ -339,34 +334,6 @@ class CommandBuilder:
339
  self.build()
340
 
341
 
342
- # def somefunc(input: gr.components.IOComponent, c_label=""):
343
- # label = ""
344
- # output = {}
345
- # print(input, c_label)
346
- # label, *_ = input.label.strip(": ").lower().split(
347
- # ) if type(input.label) != list else "".join(input.label).strip(": ").lower().split()
348
- # label += "".join(_).title()
349
- # print(newoutputMap.get(label), label, c_label)
350
- # if c_label not in [None, "Source", "Auto", ""]:
351
- # print(input.value)
352
- # output.update({label: c_label})
353
- # else:
354
- # output.pop(label, "No Key Exists")
355
- # pprint(output)
356
-
357
- # def mediaChange(option):
358
- # no_=gr.update(visible=False)
359
- # if option in video_containers:
360
- # output=gr.update(visible=True)
361
- # return [no_,output]
362
- # elif option in audio_containers:
363
- # output=gr.update(visible=True)
364
- # return [output,no_]
365
- # else:
366
- # output=gr.update(visible=False)
367
- # return [no_,no_]
368
-
369
-
370
  def media_change(option: str, state) -> List[Component]:
371
  """
372
  Allows playing the media in various options,
@@ -389,20 +356,6 @@ def media_change(option: str, state) -> List[Component]:
389
  return [chosen(ops), chosen(ops2), chosen(ops3)]
390
 
391
 
392
- # def videoChange(value):
393
- # print(value.name)
394
-
395
- # if option in video_containers:
396
- # output=gr.update(visible=True)
397
- # return [no_,output]
398
- # elif option in audio_containers:
399
- # output=gr.update(visible=True)
400
- # return [output,no_]
401
- # else:
402
- # output=gr.update(visible=False)
403
- # return [no_,no_]
404
-
405
-
406
  """Helper Functions for Processing """
407
 
408
 
@@ -492,8 +445,6 @@ def change_clipbox(choice: str) -> List[Component]:
492
  print(choice, " now choice")
493
  if choice == "Enabled":
494
  return [
495
- # gr.update(visible=True, value="00:00"),
496
- # gr.update(visible=True, value="00:10"),
497
  gr.Textbox(
498
  label="Start Time:", placeholder="00:00", visible=True, value="00:00"
499
  ),
@@ -514,29 +465,9 @@ def change_clipbox(choice: str) -> List[Component]:
514
  # return gr.update(value=file.name)
515
 
516
 
517
- def get_component_instance(inputs: gr.Blocks) -> List[Component]:
518
- """returns only components
519
-
520
- Args:
521
- inputs: layout elements
522
-
523
- Returns:
524
- List[Component]: components
525
- """
526
- return [
527
- gr.components.get_component_instance(i, render=True) for i in inputs.children
528
- ]
529
-
530
-
531
  class Clear(CommandBuilder):
532
  """Class for clearing components in layouts"""
533
 
534
- def __call__(self, *args, **kwds):
535
- return self._component
536
-
537
- def __str__(self):
538
- return f"{self._component} __clear__ class"
539
-
540
  def __init__(self, *input_component: gr.Row | gr.Column) -> None:
541
  """
542
  Parameters:
@@ -549,24 +480,25 @@ class Clear(CommandBuilder):
549
  # self._component += super()._get_component_instance(i)
550
  self._component += self.__get_component_instance(i)
551
 
 
 
 
 
 
 
552
  def __get_component_instance(self, inputs: gr.Row | gr.Column) -> list:
553
- # print(inputs, " class instance")
554
  res = []
555
- # print(*inputs.children)
556
  for i in inputs.children:
557
  # print(i,hasattr(i,"children"))
558
  if not hasattr(i, "children"):
559
  # res.append(gr.components.get_component_instance(i,render=True))
560
- res += [gr.components.get_component_instance(i, render=True)]
561
  # print(i)
562
- elif hasattr(i, "children"):
563
- # print(*i.children)
564
  res += self.__get_component_instance(i)
565
  # res=[gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i, "children")]
566
- # print(res,"__ result")
567
  # print(res)
568
  return res
569
- # return [gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i, "children")]
570
 
571
  def add(self, *args):
572
  print(args, type(args))
 
58
  a function to each component in the context
59
  to build an array of ffmpeg commands"""
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  def __init__(self, *inputs: gr.Row | gr.Column) -> None:
62
  """
63
  Parameters:
 
66
 
67
  self.output_dict = {"vf": {}, "af": {}}
68
  self.formatoutputdict = {"vf": {}, "af": {}}
 
 
69
 
70
  self._component: List[Component] = []
71
  self.vf, self.af, self.extra = ([] for _ in range(3))
 
74
  return None
75
  for i in inputs:
76
  self._component += self._get_component_instance(i)
 
 
77
  # for comp in self._component:
78
  # state = gr.State()
79
  # if comp.label is not None:
 
82
  # self.changefunc(state.value, comp.value)
83
  # comp.change(fn=self.changefunc, inputs=[state, comp], outputs=[])
84
 
85
+ def __call__(self, *args, **kwds):
86
+ return [i.value for i in self._component]
87
+
88
+ def setup_listener(self, *inputs, **kwds):
89
+ """
90
+ Sets up listeners for component updates in the current instance.
91
+
92
+ """
93
+ for comp in self._component:
94
+ if comp.label is not None:
95
+ # self.changefunc(comp, comp.value)
96
+ # print(
97
+ # comp.label,
98
+ # comp,
99
+ # )
100
+ state = gr.State(value=comp.label)
101
+ comp.change(fn=self.changefunc, inputs=[state, comp], outputs=[])
102
+
103
+ def reset(self):
104
+ self.output_dict = {"vf": {}, "af": {}}
105
+ self.commands = ""
106
+ self.vf, self.af, self.extra = ([] for _ in range(3))
107
+
108
  def changefunc(self, component_label: str | None, new_value=""):
109
  label, *_ = (
110
  component_label.strip(": \n").lower().split()
 
224
  if not hasattr(i, "children"):
225
  # res.append(gr.components.get_component_instance(i,render=True))
226
  # if isinstance(i, gr.components.Component):
227
+ res += [gr.components.get_component_instance(i)]
 
 
 
228
  else:
 
229
  res += self._get_component_instance(i)
230
  # print(res)
231
  return res
 
232
 
233
  def set_video_filters(self, options):
234
  value = self.output_dict.get(options, "-")
 
334
  self.build()
335
 
336
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
337
  def media_change(option: str, state) -> List[Component]:
338
  """
339
  Allows playing the media in various options,
 
356
  return [chosen(ops), chosen(ops2), chosen(ops3)]
357
 
358
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
  """Helper Functions for Processing """
360
 
361
 
 
445
  print(choice, " now choice")
446
  if choice == "Enabled":
447
  return [
 
 
448
  gr.Textbox(
449
  label="Start Time:", placeholder="00:00", visible=True, value="00:00"
450
  ),
 
465
  # return gr.update(value=file.name)
466
 
467
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
468
  class Clear(CommandBuilder):
469
  """Class for clearing components in layouts"""
470
 
 
 
 
 
 
 
471
  def __init__(self, *input_component: gr.Row | gr.Column) -> None:
472
  """
473
  Parameters:
 
480
  # self._component += super()._get_component_instance(i)
481
  self._component += self.__get_component_instance(i)
482
 
483
+ def __call__(self, *args, **kwds):
484
+ return self._component
485
+
486
+ def __str__(self):
487
+ return f"{self._component} __clear__ class"
488
+
489
  def __get_component_instance(self, inputs: gr.Row | gr.Column) -> list:
 
490
  res = []
 
491
  for i in inputs.children:
492
  # print(i,hasattr(i,"children"))
493
  if not hasattr(i, "children"):
494
  # res.append(gr.components.get_component_instance(i,render=True))
495
+ res += [gr.components.get_component_instance(i)]
496
  # print(i)
497
+ else:
 
498
  res += self.__get_component_instance(i)
499
  # res=[gr.components.get_component_instance(i, render=True) for i in inputs.children if not hasattr(i, "children")]
 
500
  # print(res)
501
  return res
 
502
 
503
  def add(self, *args):
504
  print(args, type(args))