maettubfh commited on
Commit
7408f28
1 Parent(s): 41de85c

created new generic class WrappedGradioObjects, and GradioTabWrapper

Browse files
app.py CHANGED
@@ -1,44 +1,32 @@
1
  import gradio as gr
2
 
3
- from src.nlp_circle_demo.interface import GradioElementWrapper
4
 
5
- GradioElementWrapper.interface_from_yaml("resources/qa_interface.yml")
6
 
7
-
8
- robertaGer = GradioElementWrapper.interface_from_yaml(
9
  "resources/legal_german_roberta_interface.yml"
10
- ).gradio_element
11
- gbert = GradioElementWrapper.interface_from_yaml(
12
- "resources/gbert_interface.yml"
13
- ).gradio_element
14
-
15
- ner = GradioElementWrapper.interface_from_yaml(
16
- "resources/ner_interface.yml"
17
- ).gradio_element
18
 
 
19
 
20
- zeroShot = GradioElementWrapper.interface_from_yaml(
21
- "resources/zero_shot_interface.yml"
22
- ).gradio_element
23
 
24
- legalInterface = gr.TabbedInterface([robertaGer, gbert], ["Roberta Legal", "Bert"])
25
 
 
26
 
27
- qaInterface = GradioElementWrapper.interface_from_yaml(
28
- "resources/qa_interface.yml"
29
- ).gradio_element
30
- simplicationInterface = GradioElementWrapper.interface_from_yaml(
31
  "resources/simplification_interface.yml"
32
- ).gradio_element
33
- gptInterface = GradioElementWrapper.interface_from_yaml(
34
- "resources/gpt2_interface.yml"
35
- ).gradio_element
36
- summarizationInterface = GradioElementWrapper.interface_from_yaml(
37
  "resources/summarization_interface.yml"
38
- ).gradio_element
39
 
40
 
41
- demo = gr.TabbedInterface(
 
42
  [
43
  gptInterface,
44
  legalInterface,
@@ -48,15 +36,5 @@ demo = gr.TabbedInterface(
48
  ner,
49
  zeroShot,
50
  ],
51
- [
52
- "GPT",
53
- "Legal",
54
- "Question Answering",
55
- "Summarization",
56
- "Simplification",
57
- "Named Entity Recognition",
58
- "Zero-Shot-Klassifizierung",
59
- ],
60
  )
61
-
62
  demo.launch()
 
1
  import gradio as gr
2
 
3
+ from src.nlp_circle_demo.interface import GradioInterfaceWrapper, GradioTabWrapper
4
 
 
5
 
6
+ robertaGer = GradioInterfaceWrapper.from_yaml(
 
7
  "resources/legal_german_roberta_interface.yml"
8
+ )
9
+ gbert = GradioInterfaceWrapper.from_yaml("resources/gbert_interface.yml")
 
 
 
 
 
 
10
 
11
+ legalInterface = GradioTabWrapper.from_interface_list("Legal", [robertaGer, gbert])
12
 
 
 
 
13
 
14
+ ner = GradioInterfaceWrapper.from_yaml("resources/ner_interface.yml")
15
 
16
+ zeroShot = GradioInterfaceWrapper.from_yaml("resources/zero_shot_interface.yml")
17
 
18
+ qaInterface = GradioInterfaceWrapper.from_yaml("resources/qa_interface.yml")
19
+ simplicationInterface = GradioInterfaceWrapper.from_yaml(
 
 
20
  "resources/simplification_interface.yml"
21
+ )
22
+ gptInterface = GradioInterfaceWrapper.from_yaml("resources/gpt2_interface.yml")
23
+ summarizationInterface = GradioInterfaceWrapper.from_yaml(
 
 
24
  "resources/summarization_interface.yml"
25
+ )
26
 
27
 
28
+ demo = GradioTabWrapper.from_interface_list(
29
+ "NLP Demo-Seite",
30
  [
31
  gptInterface,
32
  legalInterface,
 
36
  ner,
37
  zeroShot,
38
  ],
 
 
 
 
 
 
 
 
 
39
  )
 
40
  demo.launch()
src/nlp_circle_demo/__pycache__/interface.cpython-310.pyc CHANGED
Binary files a/src/nlp_circle_demo/__pycache__/interface.cpython-310.pyc and b/src/nlp_circle_demo/__pycache__/interface.cpython-310.pyc differ
 
src/nlp_circle_demo/interface.py CHANGED
@@ -1,15 +1,19 @@
1
  import gradio as gr
2
- import json
3
  import yaml
4
 
5
 
6
- class GradioElementWrapper:
7
  def __init__(self, title, gradio_element):
8
  self.title = title
9
  self.gradio_element = gradio_element
10
 
 
 
 
 
 
11
  @classmethod
12
- def interface_from_yaml(cls, path):
13
  """Initializes Interface from YAML file."""
14
  with open(path) as f:
15
  content_dict = yaml.safe_load(f)
@@ -19,16 +23,31 @@ class GradioElementWrapper:
19
  def create_interface(cls, name, title, description, examples=None):
20
  """Creates Gradio-Element containing an interface."""
21
  description = cls._prepend_link_to_description(name, title, description)
22
- element = gr.load(
23
  name,
24
  title=None, # Having the Tab-Name is sufficient.
25
  description=description,
26
  examples=examples,
27
  )
28
- return cls(title, element)
29
 
30
  @staticmethod
31
  def _prepend_link_to_description(name, title, description):
32
  without_huggingface = name.removeprefix("huggingface/")
33
  link = f"https://huggingface.co/{without_huggingface}"
34
  return f'<a href="{link}">{title}</a> </br> {description}'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
 
2
  import yaml
3
 
4
 
5
+ class WrappedGradioObject:
6
  def __init__(self, title, gradio_element):
7
  self.title = title
8
  self.gradio_element = gradio_element
9
 
10
+ def launch(self):
11
+ return self.gradio_element.launch()
12
+
13
+
14
+ class GradioInterfaceWrapper(WrappedGradioObject):
15
  @classmethod
16
+ def from_yaml(cls, path):
17
  """Initializes Interface from YAML file."""
18
  with open(path) as f:
19
  content_dict = yaml.safe_load(f)
 
23
  def create_interface(cls, name, title, description, examples=None):
24
  """Creates Gradio-Element containing an interface."""
25
  description = cls._prepend_link_to_description(name, title, description)
26
+ interface = gr.load(
27
  name,
28
  title=None, # Having the Tab-Name is sufficient.
29
  description=description,
30
  examples=examples,
31
  )
32
+ return cls(title, interface)
33
 
34
  @staticmethod
35
  def _prepend_link_to_description(name, title, description):
36
  without_huggingface = name.removeprefix("huggingface/")
37
  link = f"https://huggingface.co/{without_huggingface}"
38
  return f'<a href="{link}">{title}</a> </br> {description}'
39
+
40
+
41
+ class GradioTabWrapper(WrappedGradioObject):
42
+ @classmethod
43
+ def from_interface_list(cls, title, gradio_objects):
44
+ """Constructs a GradioTabWrapper from a title and a list of WrappedGradioObjects."""
45
+ interface = gr.TabbedInterface(
46
+ [obj.gradio_element for obj in gradio_objects],
47
+ [obj.title for obj in gradio_objects],
48
+ )
49
+ return cls(title, interface)
50
+
51
+ @classmethod
52
+ def from_yaml(cls, path):
53
+ pass