forked from amal-f5/eve
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webapp.py
156 lines (133 loc) · 4.67 KB
/
webapp.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# !pip install dash==2.0.0
# !pip install jupyter-dash
import os
import openai
import time
from jupyter_dash import JupyterDash
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State
import dash_bootstrap_components as dbc
openai.api_key = os.getenv("API_KEY")
# Presets: Define Open AI Requirements here
def run_preset(query):
result = ""
try :
response = openai.Completion.create(
engine="text-davinci-003",
prompt=query,
temperature=0.7,
max_tokens=800,
top_p=1.0,
frequency_penalty=0.0,
presence_penalty=0.0
# stop=["#", ";"]
)
result = response.choices[0].text
except openai.error.AuthenticationError as inst:
result = "openai AuthenticationError: Please verify your API KEY is valid (It may have been rotated by openai"
return result
# Build App
external_stylesheets = ['https://codepen.io/chriddyp/pen/bWLwgP.css']
app = JupyterDash(__name__, external_stylesheets=external_stylesheets)
app.layout = html.Div([
html.H5("F5 AI driven - Product Configuraton Playground"),
dcc.Dropdown(
id='dropdown-preset',
options=[
{'label': 'Generate an NGINX Configuraton', 'value': '01'},
{'label': 'Generate a BIG-IP Configuration', 'value': '02'},
{'label': 'Create a Web Application Firewall Policy', 'value': '03'},
{'label': 'Create a BIG-IP iRule', 'value': '04'},
{'label': 'Create a unique password', 'value': '05'},
{'label': 'Build a Terraform Operator', 'value': '06'},
{'label': 'Build a Helm Chart', 'value': '07'},
{'label': 'Build a Kubernetes Configuration', 'value': '08'},
{'label': 'Build your Open Telemetry code', 'value': '09'},
{'label': 'List F5 Products', 'value': '10'}
],
placeholder="Load a preset configuration"
),
dcc.Textarea(
id='textarea-query',
value='',
placeholder="Type a query in natural language or select a preset above",
style={'width': '100%', 'height': 200},
),
html.Div(id='textarea-query-output', style={'whiteSpace': 'pre-line', 'padding-top': '10px'}),
html.Button('Generate', id='button-generate', n_clicks=0),
html.Div(id='div-output-results', style={'padding-top': '10px'}),
html.Pre(
id='div-output-results2',
style={
'height': 400,
'overflow': 'auto',
'font-family': 'courier new',
'font-weight': 'bold',
'color': 'white',
'background-color': 'LightSlateGrey',
'padding': '10px',
'font-size': '100%',
'border': 'solid 1px #A2B1C6'
}
),
], style={
'border': 'solid 1px #A2B1C6',
'border-radius': '5px',
'padding': '20px',
'margin-top': '10px'
})
##
# Called when Preset dropdown is selected
##
@app.callback(
Output(component_id='textarea-query', component_property='value'),
Input(component_id='dropdown-preset', component_property='value'),
)
def update_output(dropdown):
# return 'You have selected query "{}"'.format(get_query_from_preset(dropdown))
return get_query_from_preset(dropdown)
def get_query_from_preset(preset):
query = ''
if preset == '01':
query = "Write an nginx config to do:"
elif preset == '02':
query = ""
elif preset == '03':
query = "Write a BIG-IP web application firewall config to do the following:"
elif preset == '04':
query = "Write a BIG-IP iRule for the following:"
elif preset == '05':
query = ""
elif preset == '06':
query = ""
elif preset == '07':
query = ""
elif preset == '08':
query = ""
elif preset == '09':
query = ""
elif preset == '10':
query = ""
return query
##
## Called when the Button 'Generate' is pushed
##
@app.callback(
Output(component_id='div-output-results2', component_property='children'),
State(component_id='textarea-query', component_property='value'),
State(component_id='dropdown-preset', component_property='value'),
Input('button-generate', 'n_clicks')
)
def update_output2(textarea, preset, n_clicks):
if n_clicks is None or n_clicks == 0:
return '(nothing generated yet)'
else:
# Execute dynamically the 'run_preset_nn' function (where 'nn' is the preset number)
# results = globals()['run_preset_%s' % preset](textarea)
results = globals()['run_preset'](textarea)
return results
if __name__ == "__main__":
app.run_server(port=8118, debug=False)
else:
application = app.server