Saripudin commited on
Commit
5b36232
β€’
1 Parent(s): 826817d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -25
app.py CHANGED
@@ -8,6 +8,13 @@ model = GLiNER.from_pretrained("numind/NuNER_Zero")
8
 
9
  classifier = pipeline("zero-shot-classification", model="MoritzLaurer/deberta-v3-base-zeroshot-v1")
10
 
 
 
 
 
 
 
 
11
  #define a function to process your input and output
12
  def zero_shot(doc, candidates):
13
  given_labels = candidates.split(", ")
@@ -16,12 +23,26 @@ def zero_shot(doc, candidates):
16
  scores = dictionary['scores']
17
  return dict(zip(labels, scores))
18
 
19
- examples = [
 
 
 
 
 
 
 
 
 
 
20
  [
21
  "The Moon is Earth's only natural satellite. It orbits at an average distance of 384,400 km (238,900 mi), about 30 times the diameter of Earth. Over time Earth's gravity has caused tidal locking, causing the same side of the Moon to always face Earth. Because of this, the lunar day and the lunar month are the same length, at 29.5 Earth days. The Moon's gravitational pull – and to a lesser extent, the Sun's – are the main drivers of Earth's tides.",
22
  "celestial body,quantity,physical concept",
23
- 0.3,
24
- False
 
 
 
 
25
  ],
26
  ]
27
 
@@ -62,37 +83,57 @@ def ner(
62
  r["entities"] = merge_entities(r["entities"])
63
  return r
64
 
65
- with gr.Blocks(title="Zero-Shot Demo") as demo: #, theme=gr.themes.Soft()
 
 
 
 
 
 
 
66
 
67
  #create input and output objects
68
  with gr.Tab("Zero-Shot Text Classification"):
69
- #input object1
70
- input1 = gr.Textbox(label="Text")
71
- #input object 2
72
- input2 = gr.Textbox(label="Labels")
73
- #output object
 
 
 
 
74
  output = gr.Label(label="Output")
75
- #create interface
76
  gui = gr.Interface(
77
- title="Zero-Shot Text Classification",
78
  fn=zero_shot,
79
  inputs=[input1, input2],
80
  outputs=[output]
81
  )
82
 
 
 
 
 
 
 
 
 
 
83
  with gr.Tab("Zero-Shot NER"):
84
  gr.Markdown(
85
  """
86
- # Zero-Shot Named Entity Recognition (NER)
87
  """
88
  )
89
 
90
  input_text = gr.Textbox(
91
- value=examples[0][0], label="Text input", placeholder="Enter your text here", lines=3
92
  )
93
  with gr.Row() as row:
94
  labels = gr.Textbox(
95
- value=examples[0][1],
96
  label="Labels",
97
  placeholder="Enter your labels here (comma separated)",
98
  scale=2,
@@ -100,7 +141,7 @@ with gr.Blocks(title="Zero-Shot Demo") as demo: #, theme=gr.themes.Soft()
100
  threshold = gr.Slider(
101
  0,
102
  1,
103
- value=0.3,
104
  step=0.01,
105
  label="Threshold",
106
  info="Lower the threshold to increase how many entities get predicted.",
@@ -111,16 +152,14 @@ with gr.Blocks(title="Zero-Shot Demo") as demo: #, theme=gr.themes.Soft()
111
 
112
  submit_btn = gr.Button("Submit")
113
 
114
- # Submitting
115
- # input_text.submit(
116
- # fn=ner, inputs=[input_text, labels, threshold], outputs=output
117
- # )
118
- # labels.submit(
119
- # fn=ner, inputs=[input_text, labels, threshold], outputs=output
120
- # )
121
- # threshold.release(
122
- # fn=ner, inputs=[input_text, labels, threshold], outputs=output
123
- # )
124
  submit_btn.click(
125
  fn=ner, inputs=[input_text, labels, threshold], outputs=output
126
  )
 
8
 
9
  classifier = pipeline("zero-shot-classification", model="MoritzLaurer/deberta-v3-base-zeroshot-v1")
10
 
11
+ css = """
12
+ h1 {
13
+ text-align: center;
14
+ display:block;
15
+ }
16
+ """
17
+
18
  #define a function to process your input and output
19
  def zero_shot(doc, candidates):
20
  given_labels = candidates.split(", ")
 
23
  scores = dictionary['scores']
24
  return dict(zip(labels, scores))
25
 
26
+ examples_text = [
27
+ [
28
+ "I am very happy today",
29
+ "positive, negative"
30
+ ],
31
+ [
32
+ "This is a news about Soccer",
33
+ "world, politics, technology, sport"
34
+ ]
35
+ ]
36
+ examples_ner = [
37
  [
38
  "The Moon is Earth's only natural satellite. It orbits at an average distance of 384,400 km (238,900 mi), about 30 times the diameter of Earth. Over time Earth's gravity has caused tidal locking, causing the same side of the Moon to always face Earth. Because of this, the lunar day and the lunar month are the same length, at 29.5 Earth days. The Moon's gravitational pull – and to a lesser extent, the Sun's – are the main drivers of Earth's tides.",
39
  "celestial body,quantity,physical concept",
40
+ 0.3
41
+ ],
42
+ [
43
+ "test aja",
44
+ "celestial body,quantity,physical concept",
45
+ 0.3
46
  ],
47
  ]
48
 
 
83
  r["entities"] = merge_entities(r["entities"])
84
  return r
85
 
86
+
87
+ with gr.Blocks(title="Zero-Shot Demo", css=css) as demo: #, theme=gr.themes.Soft()
88
+
89
+ gr.Markdown(
90
+ """
91
+ # Zero-Shot Model Demo
92
+ """
93
+ )
94
 
95
  #create input and output objects
96
  with gr.Tab("Zero-Shot Text Classification"):
97
+
98
+ gr.Markdown(
99
+ """
100
+ ## Zero-Shot Text Classification
101
+ """
102
+ )
103
+
104
+ input1 = gr.Textbox(label="Text", value=examples_text[0][0])
105
+ input2 = gr.Textbox(label="Labels",value=examples_text[0][1])
106
  output = gr.Label(label="Output")
107
+
108
  gui = gr.Interface(
109
+ # title="Zero-Shot Text Classification",
110
  fn=zero_shot,
111
  inputs=[input1, input2],
112
  outputs=[output]
113
  )
114
 
115
+ examples = gr.Examples(
116
+ examples_text,
117
+ fn=zero_shot,
118
+ inputs=[input1, input2],
119
+ outputs=output,
120
+ cache_examples=True,
121
+ )
122
+
123
+
124
  with gr.Tab("Zero-Shot NER"):
125
  gr.Markdown(
126
  """
127
+ ## Zero-Shot Named Entity Recognition (NER)
128
  """
129
  )
130
 
131
  input_text = gr.Textbox(
132
+ value=examples_ner[0][0], label="Text input", placeholder="Enter your text here", lines=3
133
  )
134
  with gr.Row() as row:
135
  labels = gr.Textbox(
136
+ value=examples_ner[0][1],
137
  label="Labels",
138
  placeholder="Enter your labels here (comma separated)",
139
  scale=2,
 
141
  threshold = gr.Slider(
142
  0,
143
  1,
144
+ value=examples_ner[0][2],
145
  step=0.01,
146
  label="Threshold",
147
  info="Lower the threshold to increase how many entities get predicted.",
 
152
 
153
  submit_btn = gr.Button("Submit")
154
 
155
+ examples = gr.Examples(
156
+ examples_ner,
157
+ fn=ner,
158
+ inputs=[input_text, labels, threshold],
159
+ outputs=output,
160
+ cache_examples=True,
161
+ )
162
+
 
 
163
  submit_btn.click(
164
  fn=ner, inputs=[input_text, labels, threshold], outputs=output
165
  )