Update app.py
Browse files
app.py
CHANGED
|
@@ -14,7 +14,6 @@ import chardet
|
|
| 14 |
|
| 15 |
# Initialize the Dash app
|
| 16 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
| 17 |
-
uploaded_files = {}
|
| 18 |
|
| 19 |
# Get OpenAI API key from Hugging Face Spaces environment variable
|
| 20 |
openai.api_key = os.environ.get('OPENAI_API_KEY')
|
|
@@ -26,7 +25,7 @@ matrix_type = None
|
|
| 26 |
|
| 27 |
# Matrix types and their descriptions
|
| 28 |
matrix_types = {
|
| 29 |
-
"Project Mangement Matrix": "Generate a project management matrix
|
| 30 |
"Communications Plan Matrix": "Create a matrix showing stakeholders, communication methods, frequency, and responsibilities.",
|
| 31 |
"Project Kick-off Matrix": "Generate a matrix outlining key project details, goals, team roles, and initial timelines.",
|
| 32 |
"Decision Matrix": "Develop a matrix for evaluating options against criteria, with weighted scores analysis of alternatives style.",
|
|
@@ -45,10 +44,6 @@ matrix_types = {
|
|
| 45 |
"SWOT Matrix": "Create a matrix analyzing Strengths, Weaknesses, Opportunities, and Threats."
|
| 46 |
}
|
| 47 |
|
| 48 |
-
import dash
|
| 49 |
-
from dash import dcc, html
|
| 50 |
-
import dash_bootstrap_components as dbc
|
| 51 |
-
|
| 52 |
app.layout = dbc.Container([
|
| 53 |
dbc.Row([
|
| 54 |
dbc.Col([
|
|
@@ -71,10 +66,7 @@ app.layout = dbc.Container([
|
|
| 71 |
},
|
| 72 |
multiple=True
|
| 73 |
),
|
| 74 |
-
html.Div(
|
| 75 |
-
html.H5("Uploaded Files"),
|
| 76 |
-
dbc.ListGroup(id='file-list')
|
| 77 |
-
], className="mt-3"),
|
| 78 |
html.Hr(),
|
| 79 |
html.Div([
|
| 80 |
dbc.Button(
|
|
@@ -87,7 +79,7 @@ app.layout = dbc.Container([
|
|
| 87 |
])
|
| 88 |
], width=3),
|
| 89 |
dbc.Col([
|
| 90 |
-
html.Div(style={"height": "20px"}), #
|
| 91 |
dcc.Loading(
|
| 92 |
id="loading-indicator",
|
| 93 |
type="dot",
|
|
@@ -97,7 +89,7 @@ app.layout = dbc.Container([
|
|
| 97 |
dbc.Button("Download Matrix", id="btn-download", color="success", className="mt-3"),
|
| 98 |
dcc.Download(id="download-matrix"),
|
| 99 |
html.Hr(),
|
| 100 |
-
html.Div(style={"height": "20px"}), #
|
| 101 |
dcc.Loading(
|
| 102 |
id="chat-loading",
|
| 103 |
type="dot",
|
|
@@ -142,19 +134,18 @@ def update_output(list_of_contents, list_of_names, existing_files):
|
|
| 142 |
global uploaded_files
|
| 143 |
if list_of_contents is not None:
|
| 144 |
new_files = []
|
| 145 |
-
for content, name in zip(list_of_contents, list_of_names):
|
| 146 |
file_content = parse_file_content(content, name)
|
| 147 |
uploaded_files[name] = file_content
|
| 148 |
-
new_files.append(
|
| 149 |
-
|
| 150 |
-
color="danger", size="sm", className="me-2"),
|
| 151 |
html.Span(name)
|
| 152 |
]))
|
| 153 |
-
|
|
|
|
| 154 |
return existing_files + new_files
|
| 155 |
-
return existing_files
|
| 156 |
|
| 157 |
-
# Update the remove file callback
|
| 158 |
@app.callback(
|
| 159 |
Output('file-list', 'children', allow_duplicate=True),
|
| 160 |
Input({'type': 'remove-file', 'index': dash.ALL}, 'n_clicks'),
|
|
@@ -166,23 +157,13 @@ def remove_file(n_clicks, existing_files):
|
|
| 166 |
ctx = dash.callback_context
|
| 167 |
if not ctx.triggered:
|
| 168 |
raise PreventUpdate
|
| 169 |
-
|
| 170 |
-
triggered_id = ctx.triggered[0]['prop_id']
|
| 171 |
-
print(f"Triggered ID: {triggered_id}")
|
| 172 |
-
|
| 173 |
-
removed_file = triggered_id.split('"index":')[1].split('"')[1]
|
| 174 |
-
print(f"Attempting to remove file: {removed_file}")
|
| 175 |
-
|
| 176 |
uploaded_files.pop(removed_file, None)
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
return [file for file in existing_files if file['props']['children'][1]['children'] != removed_file]
|
| 180 |
-
|
| 181 |
def generate_matrix_with_gpt(matrix_type, file_contents):
|
| 182 |
prompt = f"""Generate a {matrix_type} based on the following project artifacts:
|
| 183 |
-
|
| 184 |
{' '.join(file_contents)}
|
| 185 |
-
|
| 186 |
Instructions:
|
| 187 |
1. Create the {matrix_type} as a table.
|
| 188 |
2. Use ONLY pipe symbols (|) to separate columns.
|
|
@@ -191,19 +172,17 @@ Instructions:
|
|
| 191 |
5. The first row should be the column headers.
|
| 192 |
6. Start the output immediately with the column headers.
|
| 193 |
7. Each subsequent row should represent a single item in the matrix.
|
| 194 |
-
|
| 195 |
Example format:
|
| 196 |
Header1|Header2|Header3
|
| 197 |
Item1A|Item1B|Item1C
|
| 198 |
Item2A|Item2B|Item2C
|
| 199 |
-
|
| 200 |
Now, generate the {matrix_type}:
|
| 201 |
"""
|
| 202 |
|
| 203 |
response = openai.ChatCompletion.create(
|
| 204 |
model="gpt-4-turbo",
|
| 205 |
messages=[
|
| 206 |
-
{"role": "system", "content": "You are a precise matrix generator that outputs only the requested matrix without any additional text.
|
| 207 |
{"role": "user", "content": prompt}
|
| 208 |
]
|
| 209 |
)
|
|
@@ -227,7 +206,6 @@ Now, generate the {matrix_type}:
|
|
| 227 |
prevent_initial_call=True
|
| 228 |
)
|
| 229 |
def generate_matrix(*args):
|
| 230 |
-
print(f"Current uploaded files: {uploaded_files}")
|
| 231 |
global current_matrix, matrix_type
|
| 232 |
ctx = dash.callback_context
|
| 233 |
if not ctx.triggered:
|
|
@@ -260,10 +238,8 @@ def update_matrix_via_chat(n_clicks, chat_input):
|
|
| 260 |
raise PreventUpdate
|
| 261 |
|
| 262 |
prompt = f"""Update the following {matrix_type} based on this instruction: {chat_input}
|
| 263 |
-
|
| 264 |
Current matrix:
|
| 265 |
{current_matrix.to_string(index=False)}
|
| 266 |
-
|
| 267 |
Instructions:
|
| 268 |
1. Provide ONLY the updated matrix as a table.
|
| 269 |
2. Use ONLY pipe symbols (|) to separate columns.
|
|
@@ -272,7 +248,6 @@ Instructions:
|
|
| 272 |
5. The first row should be the column headers.
|
| 273 |
6. Start the output immediately with the column headers.
|
| 274 |
7. Each subsequent row should represent a single item in the matrix.
|
| 275 |
-
|
| 276 |
Now, provide the updated {matrix_type}:
|
| 277 |
"""
|
| 278 |
|
|
|
|
| 14 |
|
| 15 |
# Initialize the Dash app
|
| 16 |
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
|
|
|
|
| 17 |
|
| 18 |
# Get OpenAI API key from Hugging Face Spaces environment variable
|
| 19 |
openai.api_key = os.environ.get('OPENAI_API_KEY')
|
|
|
|
| 25 |
|
| 26 |
# Matrix types and their descriptions
|
| 27 |
matrix_types = {
|
| 28 |
+
"Project Mangement Matrix": "Generate a project management matrix outlining all aspects of project management that summarizes components such as scope, schedule, budget, quality, resources, communications, risk, procurement, and stakeholder management.",
|
| 29 |
"Communications Plan Matrix": "Create a matrix showing stakeholders, communication methods, frequency, and responsibilities.",
|
| 30 |
"Project Kick-off Matrix": "Generate a matrix outlining key project details, goals, team roles, and initial timelines.",
|
| 31 |
"Decision Matrix": "Develop a matrix for evaluating options against criteria, with weighted scores analysis of alternatives style.",
|
|
|
|
| 44 |
"SWOT Matrix": "Create a matrix analyzing Strengths, Weaknesses, Opportunities, and Threats."
|
| 45 |
}
|
| 46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
app.layout = dbc.Container([
|
| 48 |
dbc.Row([
|
| 49 |
dbc.Col([
|
|
|
|
| 66 |
},
|
| 67 |
multiple=True
|
| 68 |
),
|
| 69 |
+
html.Div(id='file-list'),
|
|
|
|
|
|
|
|
|
|
| 70 |
html.Hr(),
|
| 71 |
html.Div([
|
| 72 |
dbc.Button(
|
|
|
|
| 79 |
])
|
| 80 |
], width=3),
|
| 81 |
dbc.Col([
|
| 82 |
+
html.Div(style={"height": "20px"}), # Added small gap
|
| 83 |
dcc.Loading(
|
| 84 |
id="loading-indicator",
|
| 85 |
type="dot",
|
|
|
|
| 89 |
dbc.Button("Download Matrix", id="btn-download", color="success", className="mt-3"),
|
| 90 |
dcc.Download(id="download-matrix"),
|
| 91 |
html.Hr(),
|
| 92 |
+
html.Div(style={"height": "20px"}), # Added small gap
|
| 93 |
dcc.Loading(
|
| 94 |
id="chat-loading",
|
| 95 |
type="dot",
|
|
|
|
| 134 |
global uploaded_files
|
| 135 |
if list_of_contents is not None:
|
| 136 |
new_files = []
|
| 137 |
+
for i, (content, name) in enumerate(zip(list_of_contents, list_of_names)):
|
| 138 |
file_content = parse_file_content(content, name)
|
| 139 |
uploaded_files[name] = file_content
|
| 140 |
+
new_files.append(html.Div([
|
| 141 |
+
html.Button('×', id={'type': 'remove-file', 'index': name}, style={'marginRight': '5px', 'fontSize': '10px'}),
|
|
|
|
| 142 |
html.Span(name)
|
| 143 |
]))
|
| 144 |
+
if existing_files is None:
|
| 145 |
+
existing_files = []
|
| 146 |
return existing_files + new_files
|
| 147 |
+
return existing_files
|
| 148 |
|
|
|
|
| 149 |
@app.callback(
|
| 150 |
Output('file-list', 'children', allow_duplicate=True),
|
| 151 |
Input({'type': 'remove-file', 'index': dash.ALL}, 'n_clicks'),
|
|
|
|
| 157 |
ctx = dash.callback_context
|
| 158 |
if not ctx.triggered:
|
| 159 |
raise PreventUpdate
|
| 160 |
+
removed_file = ctx.triggered[0]['prop_id'].split(',')[0].split(':')[-1].strip('}')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
uploaded_files.pop(removed_file, None)
|
| 162 |
+
return [file for file in existing_files if file['props']['children'][1]['props']['children'] != removed_file]
|
| 163 |
+
|
|
|
|
|
|
|
| 164 |
def generate_matrix_with_gpt(matrix_type, file_contents):
|
| 165 |
prompt = f"""Generate a {matrix_type} based on the following project artifacts:
|
|
|
|
| 166 |
{' '.join(file_contents)}
|
|
|
|
| 167 |
Instructions:
|
| 168 |
1. Create the {matrix_type} as a table.
|
| 169 |
2. Use ONLY pipe symbols (|) to separate columns.
|
|
|
|
| 172 |
5. The first row should be the column headers.
|
| 173 |
6. Start the output immediately with the column headers.
|
| 174 |
7. Each subsequent row should represent a single item in the matrix.
|
|
|
|
| 175 |
Example format:
|
| 176 |
Header1|Header2|Header3
|
| 177 |
Item1A|Item1B|Item1C
|
| 178 |
Item2A|Item2B|Item2C
|
|
|
|
| 179 |
Now, generate the {matrix_type}:
|
| 180 |
"""
|
| 181 |
|
| 182 |
response = openai.ChatCompletion.create(
|
| 183 |
model="gpt-4-turbo",
|
| 184 |
messages=[
|
| 185 |
+
{"role": "system", "content": "You are a precise matrix generator that outputs only the requested matrix without any additional text."},
|
| 186 |
{"role": "user", "content": prompt}
|
| 187 |
]
|
| 188 |
)
|
|
|
|
| 206 |
prevent_initial_call=True
|
| 207 |
)
|
| 208 |
def generate_matrix(*args):
|
|
|
|
| 209 |
global current_matrix, matrix_type
|
| 210 |
ctx = dash.callback_context
|
| 211 |
if not ctx.triggered:
|
|
|
|
| 238 |
raise PreventUpdate
|
| 239 |
|
| 240 |
prompt = f"""Update the following {matrix_type} based on this instruction: {chat_input}
|
|
|
|
| 241 |
Current matrix:
|
| 242 |
{current_matrix.to_string(index=False)}
|
|
|
|
| 243 |
Instructions:
|
| 244 |
1. Provide ONLY the updated matrix as a table.
|
| 245 |
2. Use ONLY pipe symbols (|) to separate columns.
|
|
|
|
| 248 |
5. The first row should be the column headers.
|
| 249 |
6. Start the output immediately with the column headers.
|
| 250 |
7. Each subsequent row should represent a single item in the matrix.
|
|
|
|
| 251 |
Now, provide the updated {matrix_type}:
|
| 252 |
"""
|
| 253 |
|