saujasv commited on
Commit
678feb3
·
1 Parent(s): 87c604f

literal model demo

Browse files
Files changed (1) hide show
  1. app.py +84 -25
app.py CHANGED
@@ -1,40 +1,99 @@
1
  from typing import Any
2
  import gradio as gr
 
3
  from listener import Listener
4
 
5
- pragmatic_listener = Listener("pragmatic-programs/pragmatic-ft-listener", {"do_sample": True, "num_return_sequences": 100, "num_beams": 1, "temperature": 1, "top_p": 0.9, "max_new_tokens": 128})
6
- literal_listener = Listener("pragmatic-programs/listener-suffix-idx-300k", {"do_sample": True, "num_return_sequences": 100, "num_beams": 1, "temperature": 1, "top_p": 0.9, "max_new_tokens": 128})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
- def synthesize(context):
9
- if len(context.strip()) == 0:
10
- return "Empty specification", "Empty specification"
11
 
12
- spec = [[[s[:-1], s[-1]] for s in context.strip().split(' ')]]
13
- if not all([len(s) > 0 and l in ['+', '-'] for s, l in spec[0]]):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  return "Invalid specification", "Invalid specification"
15
 
16
- pragmatic_outputs = pragmatic_listener.synthesize(spec).programs
17
- literal_outputs = literal_listener.synthesize(spec).programs
18
 
19
- if len(pragmatic_outputs[0]) > 0:
20
- pragmatic_program = pragmatic_outputs[0][0]
21
- else:
22
- pragmatic_program = "No program found"
23
-
24
- if len(literal_outputs[0]) > 0:
25
- literal_program = literal_outputs[0][0]
 
 
 
 
 
 
 
 
 
26
  else:
27
- literal_program = "No program found"
28
 
29
- return pragmatic_program, literal_program
30
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  iface = gr.Interface(
33
- fn=synthesize,
34
- inputs=gr.Textbox(lines=1, label="Examples", info="Enter a list of examples, separated by spaces. Each example is the string followed by a + or - indicating whether it should be accepted or rejected by the synthesized regex."),
35
- outputs=[gr.Textbox(lines=1, label="Pragmatic model"), gr.Textbox(lines=1, label="Literal model")],
36
- examples=["ab+ aabb+ abb+ abab-", "b0b+ aa0000bb+"],
 
 
 
 
37
  title="Synthesizing regular expressions from examples",
38
-
39
- )
40
- iface.launch()
 
1
  from typing import Any
2
  import gradio as gr
3
+ import itertools
4
  from listener import Listener
5
 
6
+ # pragmatic_listener = Listener(
7
+ # "pragmatic-programs/pragmatic-ft-listener",
8
+ # {
9
+ # "do_sample": True,
10
+ # "num_return_sequences": 100,
11
+ # "num_beams": 1,
12
+ # "temperature": 1,
13
+ # "top_p": 0.9,
14
+ # "max_new_tokens": 128,
15
+ # },
16
+ # )
17
+ listener = Listener(
18
+ "pragmatic-programs/listener-suffix-idx-300k",
19
+ {
20
+ "do_sample": True,
21
+ "num_return_sequences": 100,
22
+ "num_beams": 1,
23
+ "temperature": 1,
24
+ "top_p": 0.9,
25
+ "max_new_tokens": 128,
26
+ },
27
+ )
28
 
29
+ N_EXAMPLES = 3
 
 
30
 
31
+
32
+ def synthesize(*inps):
33
+ strings = [inps[2 * i] for i in range(N_EXAMPLES) if len(inps[2 * i]) > 0]
34
+ labels = [
35
+ "+" if inps[2 * i + 1] == "match" else "-"
36
+ for i in range(N_EXAMPLES)
37
+ if inps[2 * i + 1] is not None
38
+ ]
39
+ spec = list(zip(strings, labels))
40
+
41
+ # if len(context.strip()) == 0:
42
+ # return "Empty specification", "Empty specification"
43
+
44
+ # spec = [[[s[:-1], s[-1]] for s in context.strip().split(" ")]]
45
+
46
+ if len(spec) == 0:
47
+ return "Empty specification"
48
+ if not all([len(s) > 0 and l in ["+", "-"] for s, l in spec[0]]):
49
  return "Invalid specification", "Invalid specification"
50
 
51
+ # pragmatic_outputs = pragmatic_listener.synthesize(spec).programs
52
+ # literal_outputs = literal_listener.synthesize(spec).programs
53
 
54
+ # if len(pragmatic_outputs[0]) > 0:
55
+ # pragmatic_program = pragmatic_outputs[0][0]
56
+ # else:
57
+ # pragmatic_program = "No program found"
58
+
59
+ # if len(literal_outputs[0]) > 0:
60
+ # literal_program = literal_outputs[0][0]
61
+ # else:
62
+ # literal_program = "No program found"
63
+
64
+ # return pragmatic_program, literal_program
65
+
66
+ outputs = listener.synthesize(spec).programs
67
+
68
+ if len(outputs[0]) > 0:
69
+ return outputs[0][0]
70
  else:
71
+ return "No program found"
72
 
 
73
 
74
+ input_fields = [
75
+ (
76
+ gr.Textbox(
77
+ lines=1,
78
+ label=f"Example {i + 1}",
79
+ # info="Enter a list of examples, separated by spaces. Each example is the string followed by a + or - indicating whether it should be accepted or rejected by the synthesized regex.",
80
+ container=True,
81
+ ),
82
+ gr.Radio(["match", "not match"], container=False, label="Label"),
83
+ )
84
+ for i in range(N_EXAMPLES)
85
+ ]
86
 
87
  iface = gr.Interface(
88
+ fn=synthesize,
89
+ inputs=list(itertools.chain.from_iterable(input_fields)),
90
+ outputs=gr.Textbox(lines=1, label="Synthesizer output"),
91
+ # [
92
+ # gr.Textbox(lines=1, label="Pragmatic model"),
93
+ # gr.Textbox(lines=1, label="Literal model"),
94
+ # ],
95
+ # examples=["ab+ aabb+ abb+ abab-", "b0b+ aa0000bb+"],
96
  title="Synthesizing regular expressions from examples",
97
+ theme=gr.themes.Soft(primary_hue="blue"),
98
+ )
99
+ iface.launch()