imamnurby commited on
Commit
a27e544
1 Parent(s): 125c76e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +91 -0
app.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from transformers import RobertaTokenizer, EncoderDecoderModel
2
+ import pandas as pd
3
+ import gradio as gr
4
+
5
+
6
+ model = EncoderDecoderModel.from_pretrained("imamnurby/rob2rand_chen_w_prefix_c_fc")
7
+ tokenizer = RobertaTokenizer.from_pretrained("imamnurby/rob2rand_chen_w_prefix_c_fc")
8
+
9
+ def generate_preds(gen_mode, desc):
10
+ desc = desc.lower()
11
+ if gen_mode=="Channel":
12
+ desc = "GENERATE TRIGGER AND ACTION CHANNEL ONLY <pf> " + desc
13
+ elif gen_mode=="Function":
14
+ desc = "GENERATE BOTH CHANNEL AND FUNCTION FOR TRIGGER AND ACTION <pf> " + desc
15
+
16
+
17
+
18
+ input_ids = tokenizer.encode(desc, return_tensors='pt')
19
+
20
+ # activate beam search and early_stopping
21
+ preds = model.generate(
22
+ input_ids,
23
+ max_length=100,
24
+ num_beams=10,
25
+ num_return_sequences=10,
26
+ early_stopping=True
27
+ )
28
+
29
+ output_list = []
30
+ for item in preds:
31
+ output_list.append(tokenizer.decode(item, skip_special_tokens=True))
32
+
33
+ if gen_mode=="Channel":
34
+ trigger = [x.split("<sep>")[0].strip() for x in output_list]
35
+ # trigger_desc = ["dummy" for x in output_list]
36
+ action = [x.split("<sep>")[1].strip() for x in output_list]
37
+ # action_desc = ["dummy" for x in output_list]
38
+ df = {"Trigger": trigger,
39
+ # "Trigger Description": trigger_desc,
40
+ "Action": action,
41
+ # "Action Description": action_desc
42
+ }
43
+ elif gen_mode=="Function":
44
+ trigger = [x.split("<sep>")[1].strip() for x in output_list]
45
+ trigger_desc = ["dummy" for x in output_list]
46
+ action = [x.split("<sep>")[3].strip() for x in output_list]
47
+ action_desc = ["dummy" for x in output_list]
48
+ df = {"Trigger": trigger,
49
+ # "Trigger Description": trigger_desc,
50
+ "Action": action,
51
+ # "Action Description": action_desc
52
+ }
53
+ return pd.DataFrame(df)
54
+
55
+ demo = gr.Blocks()
56
+ with demo:
57
+ gr.Markdown("<h1><center>RecipeGen: Automated TAPs Generation Tool</center></h1>")
58
+ gr.Markdown("<center>This demo allows you to generate TAPs (Trigger Action Programs) using functionality description described in English. You can learn the working detail of our tool from our paper<center>")
59
+ gr.Markdown("<h3>Instructions<h3>")
60
+ gr.Markdown("""
61
+ 1. Select the generation granularity (i.e., Channel, Function, or Field)
62
+ 2. Describe your intended functionality
63
+ 3. Specify the beam width
64
+ 4. Specify the number of returned sequences
65
+ 5. Click **Generate**; the generated TAPs along with the description of each component will show in the **Results**
66
+ """)
67
+ gr.Markdown("NOTE: **#Returned Sequences** should be LESS THAN OR EQUAL **Beam Width**")
68
+ with gr.Tabs():
69
+ with gr.TabItem("Channel/Function"):
70
+ with gr.Column():
71
+ gen_mode = gr.Radio(label="Granularity", choices=["Channel", "Function"])
72
+ desc = gr.Textbox(label="Functionality Description", placeholder="Describe the functionality here")
73
+ num_beams = gr.Slider(minimum=2, maximum=500, value=2, step=1, label="Beam Width")
74
+ num_returned_seqs = gr.Slider(minimum=2, maximum=500, value=2, step=1, label="#Returned Sequences")
75
+ with gr.Row():
76
+ generate = gr.Button("Generate")
77
+ gr.Markdown("<h1><center>Results</center></h1>")
78
+ results = gr.Dataframe(headers=["Trigger", "Trigger Description", "Action", "Action Description"], row_count=100)
79
+
80
+ with gr.TabItem("Field"):
81
+ with gr.Column():
82
+ desc = gr.Textbox(label="Functionality Description", placeholder="Describe the functionality here")
83
+ num_beams = gr.Slider(minimum=2, maximum=500, value=2, step=1, label="Beam Width")
84
+ num_returned_seqs = gr.Slider(minimum=2, maximum=500, value=2, step=1, label="#Returned Sequences")
85
+ with gr.Row():
86
+ generate_field = gr.Button("Generate")
87
+ gr.Markdown("<h1><center>Results</center></h1>")
88
+ results_field = gr.Dataframe(headers=["Trigger", "Trigger Description", "Trigger Fields", "Action", "Action Description", "Action Fields"])
89
+
90
+ generate.click(generate_preds, inputs=[gen_mode, desc], outputs=[results])
91
+ demo.launch()