File size: 20,862 Bytes
ab10305
6026a72
ab10305
 
 
 
 
 
 
c5fd0c3
 
 
 
 
b64b140
c5483ea
4dbeeea
ab10305
e29b31c
 
 
ab10305
c5fd0c3
 
 
 
 
 
4dbeeea
6026a72
 
 
 
 
4dbeeea
 
 
 
 
 
c5fd0c3
 
4dbeeea
 
 
 
 
c5fd0c3
 
4dbeeea
c5fd0c3
4dbeeea
 
c5fd0c3
6026a72
 
 
 
 
 
 
 
 
 
 
c5fd0c3
 
 
 
 
 
ab10305
 
cb596e9
ab10305
 
b8f1b65
ab10305
79ae593
ab10305
79ae593
ab10305
 
b8f1b65
ab10305
79ae593
ab10305
 
 
 
 
 
4dbeeea
 
 
 
 
 
 
6026a72
 
 
 
 
4dbeeea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74bc439
4dbeeea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab10305
 
 
c5fd0c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab10305
c5fd0c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab10305
c5fd0c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
685045c
c5fd0c3
ab10305
 
 
74bc439
 
 
 
 
 
 
b7ed401
ab10305
74bc439
6026a72
 
 
 
74bc439
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7ed401
b64b140
c5483ea
74bc439
b7ed401
74bc439
 
 
 
 
 
 
 
 
 
 
 
 
 
b7ed401
caa0c08
e29b31c
ab4acd6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e29b31c
4032a87
e29b31c
c5fd0c3
e29b31c
 
 
ab4acd6
4dbeeea
ab4acd6
fec5de2
ab4acd6
fec5de2
 
e29b31c
 
bfda8d6
b7ed401
 
bfda8d6
b7ed401
 
 
 
ab10305
b7ed401
c5fd0c3
b7ed401
 
 
 
 
 
ab10305
b7ed401
 
 
b64b140
b7ed401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab4acd6
c5fd0c3
ab4acd6
 
 
 
 
 
 
 
 
 
b7ed401
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ab4acd6
ab10305
 
 
 
 
 
c5fd0c3
 
ab10305
 
 
c5fd0c3
 
 
ab10305
 
 
c5fd0c3
ab10305
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
import dash
from dash import dcc, html, Input, Output, State, ALL, callback_context
import dash_bootstrap_components as dbc
import base64
import io
import pandas as pd
import openai
import os
import time
import uuid
import threading
import tempfile
import shutil
import logging
import json
import ast
from flask import request, make_response, g
from dash.exceptions import PreventUpdate
import PyPDF2
import docx
import chardet

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger("maiko_matrix_app")

SESSION_DATA = {}
SESSION_LOCKS = {}

def get_session_id(set_cookie=False):
    sid = None
    try:
        sid = request.cookies.get('session-id')
    except Exception:
        pass
    if sid and sid in SESSION_DATA:
        return sid
    sid = str(uuid.uuid4())
    if set_cookie:
        g.set_cookie_sid = sid
    return sid

def get_session_data():
    sid = get_session_id()
    if sid not in SESSION_DATA:
        SESSION_DATA[sid] = {
            'uploaded_files': {},
            'file_texts': {},
            'current_matrix': None,
            'matrix_type': None,
            'temp_dir': tempfile.mkdtemp(prefix=f"maiko_{sid}_"),
        }
        SESSION_LOCKS[sid] = threading.Lock()
    return SESSION_DATA[sid], SESSION_LOCKS[sid]

def restore_session_files(session_data):
    temp_dir = session_data.get('temp_dir')
    if not temp_dir or not os.path.exists(temp_dir):
        return
    files = os.listdir(temp_dir)
    for filename in files:
        file_path = os.path.join(temp_dir, filename)
        if filename not in session_data['uploaded_files']:
            session_data['uploaded_files'][filename] = file_path
            session_data['file_texts'][filename] = parse_file_content(file_path, filename)

def cleanup_session_tempdirs():
    for sess in SESSION_DATA.values():
        try:
            shutil.rmtree(sess['temp_dir'])
        except Exception as e:
            logger.warning(f"Failed to cleanup tempdir: {e}")

matrix_types = {
    "Project Deliverables Matrix": "Generate a project deliverables matrix all presumed and actual deliverables based on tasks, requirements and scope.",
    "Communications Plan Matrix": "Create a matrix showing stakeholders, communication methods, frequency, and responsibilities.",
    "Project Kick-off Matrix": "Generate a matrix outlining key project details, goals, team roles, and initial timelines.",
    "Decision Matrix": "Develop a matrix for evaluating options against criteria, with weighted scores analysis of alternatives style.",
    "Lessons Learned Matrix": "Create a matrix capturing project experiences, challenges, solutions, and recommendations.",
    "Key Performance Indicator Matrix": "Generate a matrix of KPIs, their measurable targets, actual performance, and status.",
    "Prioritization Matrix": "Develop a matrix for ranking tasks or features based on importance and urgency.",
    "Risk Matrix": "Create a matrix identifying tasks with potential risks, their likelihood, impact, and mitigation strategies.",
    "RACI Matrix": "Generate a matrix showing team members and their roles (Responsible, Accountable, Consulted, Informed) for each task.",
    "Project Schedule Matrix": "Develop a matrix showing project phases, tasks, durations, and dependencies.",
    "Quality Control Matrix": "Create a matrix outlining measurable quality standards, testing methods, and acceptance criteria.",
    "Requirements Traceability Matrix": "Generate a matrix linking requirements to their sources, test cases, and status.",
    "Sprint Planning Matrix": "Develop a matrix for sprint nubmer, sprint tasks in that sprint number, story points, assignees, and status.",
    "Test Traceability Matrix": "Create a matrix linking test cases to requirements, execution status, and results.",
    "Sprint Backlog": "Generate a matrix of user stories, tasks, estimates, and priorities for the sprint.",
    "Sprint Retrospective": "Develop a matrix capturing what went well, what didn't, and action items from the sprint.",
    "SWOT Matrix": "Create a matrix analyzing Strengths, Weaknesses, Opportunities, and Threats."
}

app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server

openai.api_key = os.environ.get('OPENAI_API_KEY')

@server.before_request
def ensure_session_cookie():
    sid = None
    try:
        sid = request.cookies.get('session-id')
    except Exception:
        sid = None
    if not sid or sid not in SESSION_DATA:
        sid_new = str(uuid.uuid4())
        g.set_cookie_sid = sid_new
        get_session_id(set_cookie=True)
    else:
        g.set_cookie_sid = None

@server.after_request
def set_session_cookie(response):
    sid = getattr(g, 'set_cookie_sid', None)
    if sid:
        response.set_cookie('session-id', sid, max_age=60*60*48, httponly=True)
    return response

def parse_file_content(file_path, filename):
    try:
        with open(file_path, "rb") as f:
            decoded = f.read()
        if filename.endswith('.pdf'):
            with io.BytesIO(decoded) as pdf_file:
                reader = PyPDF2.PdfReader(pdf_file)
                return ' '.join([page.extract_text() or "" for page in reader.pages])
        elif filename.endswith('.docx'):
            with io.BytesIO(decoded) as docx_file:
                doc = docx.Document(docx_file)
                return ' '.join([para.text for para in doc.paragraphs])
        elif filename.endswith('.txt') or filename.endswith('.rtf'):
            encoding = chardet.detect(decoded)['encoding']
            return decoded.decode(encoding)
        else:
            return "Unsupported file format"
    except Exception as e:
        logger.exception(f"Error processing file {filename}: {str(e)}")
        return "Error processing file"

def truncate_filename(filename, max_length=24):
    if len(filename) <= max_length:
        return filename
    else:
        return filename[:max_length - 3] + '...'

def get_file_cards(file_dict):
    cards = []
    for name in file_dict:
        cards.append(
            dbc.Card(
                dbc.CardBody(
                    dbc.Row([
                        dbc.Col(
                            html.Span(
                                truncate_filename(name),
                                title=name,
                                style={
                                    'display': 'inline-block',
                                    'overflow': 'hidden',
                                    'textOverflow': 'ellipsis',
                                    'whiteSpace': 'nowrap',
                                    'maxWidth': '90%',
                                    'verticalAlign': 'middle',
                                }
                            ),
                            width='auto',
                            style={'display': 'flex', 'alignItems': 'center', 'padding': '0'}
                        ),
                        dbc.Col(
                            dbc.Button(
                                "Delete",
                                id={'type': 'delete-file-btn', 'index': name},
                                color="danger",
                                size="sm",
                                style={'marginLeft': 'auto', 'float': 'right'}
                            ),
                            width='auto',
                            style={'display': 'flex', 'alignItems': 'center', 'justifyContent': 'flex-end', 'padding': '0'}
                        ),
                    ],
                    justify="between",
                    align="center",
                    style={"margin": "0", "padding": "0"}
                    ),
                    style={'padding': '6px 8px', 'margin': '0', 'display': 'flex', 'alignItems': 'center', 'background': 'none', 'boxShadow': 'none'}
                ),
                style={'border': 'none', 'boxShadow': 'none', 'background': 'none', 'marginBottom': '2px'}
            )
        )
    return cards

app.layout = dbc.Container([
    dbc.Row([
        dbc.Col([
            dbc.Card([
                dbc.CardBody([
                    html.H4("Project Artifacts", className="mb-3 mt-1"),
                    dcc.Upload(
                        id='upload-files',
                        children=html.Div([
                            'Drag and Drop or ',
                            html.A('Select Files')
                        ]),
                        style={
                            'width': '100%',
                            'height': '60px',
                            'lineHeight': '60px',
                            'borderWidth': '1px',
                            'borderStyle': 'dashed',
                            'borderRadius': '5px',
                            'textAlign': 'center',
                            'margin': '10px 0'
                        },
                        multiple=True
                    ),
                    html.Div(id='file-list'),
                    html.Hr(),
                    html.Div([
                        dbc.Button(
                            matrix_label,
                            id={'type': 'matrix-btn', 'index': matrix_label},
                            color="link",
                            className="mb-2 w-100 text-left custom-button",
                            style={'overflow': 'hidden', 'text-overflow': 'ellipsis', 'white-space': 'nowrap'}
                        ) for matrix_label in matrix_types.keys()
                    ])
                ])
            ], className="mb-2")
        ], width=3, style={'minWidth': '260px', 'background': '#f8f9fa', 'height': '100vh', 'position': 'fixed', 'overflowY': 'auto'}),
        dbc.Col([
            dbc.Row([
                dbc.Col([
                    html.H2("Maiko Project Matrix Generator", className="mb-3 mt-2")
                ])
            ]),
            dbc.Row([
                dbc.Col([
                    dbc.Card([
                        dbc.CardBody([
                            dcc.Loading(
                                id="loading",
                                type="default",
                                children=[
                                    html.Div(id="loading-output"),
                                    html.Div(id='matrix-preview', className="border p-3 mb-3"),
                                    dbc.Button("Download Matrix", id="btn-download", color="success", className="mt-3"),
                                    dcc.Download(id="download-matrix"),
                                ]
                            )
                        ])
                    ])
                ])
            ]),
            html.Hr(),
            dbc.Row([
                dbc.Col([
                    dbc.Card([
                        dbc.CardBody([
                            dcc.Loading(
                                id="chat-loading",
                                type="default",
                                children=[
                                    dbc.Textarea(id="chat-input", placeholder="Chat with Maiko to update matrix...", className="mb-2", style={'width': '100%', 'wordWrap': 'break-word'}),
                                    dbc.Button("Send", id="btn-send-chat", color="primary", className="mb-3"),
                                    html.Div(id="chat-output")
                                ]
                            )
                        ])
                    ])
                ])
            ])
        ], width=9, style={'marginLeft': '30%'})
    ])
], fluid=True, style={'padding': '0'})

@app.callback(
    Output('file-list', 'children'),
    [
        Input('upload-files', 'contents'),
        Input({'type': 'delete-file-btn', 'index': ALL}, 'n_clicks')
    ],
    [
        State('upload-files', 'filename'),
    ],
    prevent_initial_call='initial_duplicate'
)
def handle_file_upload_and_delete(list_of_contents, delete_clicks, list_of_names):
    ctx = callback_context
    session_data, lock = get_session_data()
    with lock:
        restore_session_files(session_data)
        triggered = ctx.triggered
        if triggered:
            prop_id = triggered[0]['prop_id']
            # Handle file upload
            if prop_id.startswith("upload-files.contents"):
                logger.info("Uploading files...")
                if list_of_contents is not None and list_of_names is not None:
                    for content, name in zip(list_of_contents, list_of_names):
                        content_type, content_string = content.split(',')
                        decoded = base64.b64decode(content_string)
                        temp_path = os.path.join(session_data['temp_dir'], name)
                        with open(temp_path, 'wb') as f:
                            f.write(decoded)
                        session_data['uploaded_files'][name] = temp_path
                        session_data['file_texts'][name] = parse_file_content(temp_path, name)
                    logger.info(f"Files after upload: {list(session_data['uploaded_files'].keys())}")
                    return get_file_cards(session_data['uploaded_files'])
            # Handle delete button click
            elif "delete-file-btn" in prop_id:
                try:
                    btn_id = prop_id.split('.')[0]
                    btn_id_dict = ast.literal_eval(btn_id)
                    filename = btn_id_dict['index']
                except Exception as e:
                    logger.warning(f"Could not extract filename from delete prop_id: {prop_id} error: {e}")
                    raise PreventUpdate
                if filename in session_data['uploaded_files']:
                    filepath = session_data['uploaded_files'][filename]
                    try:
                        os.remove(filepath)
                        logger.info(f"Deleted file from disk: {filename}")
                    except Exception as e:
                        logger.warning(f"Failed to delete temp file {filename}: {e}")
                    session_data['uploaded_files'].pop(filename, None)
                    session_data['file_texts'].pop(filename, None)
                    logger.info(f"Files after deletion: {list(session_data['uploaded_files'].keys())}")
                return get_file_cards(session_data['uploaded_files'])
        # On initial load or no trigger, show files from session
        return get_file_cards(session_data['uploaded_files'])

def generate_matrix_with_gpt(matrix_type, file_contents):
    prompt = f"""Generate a {matrix_type} based on the following project artifacts:
{' '.join(file_contents)}
Instructions:
1. Create the {matrix_type} as a table.
2. Use ONLY pipe symbols (|) to separate columns.
3. Do NOT include any introductory text, descriptions, or explanations.
4. Do NOT use any dashes (-) or other formatting characters.
5. The first row should be the column headers.
6. Start the output immediately with the column headers.
7. Each subsequent row should represent a single item in the matrix.
Example format:
Header1|Header2|Header3
Item1A|Item1B|Item1C
Item2A|Item2B|Item2C
Now, generate the {matrix_type}:
"""
    response = openai.ChatCompletion.create(
        model="gpt-4-turbo",
        messages=[
            {"role": "system", "content": "You are a precise matrix generator that outputs only the requested matrix without any additional text. Based on the files uploaded, as the project manager you perform the analysis and make appropriate assumptions to populate the matrix like roles, tasks, timelines, logically sequencing the matrix etc."},
            {"role": "user", "content": prompt}
        ]
    )
    matrix_text = response.choices[0].message.content.strip()
    logger.info(f"Raw matrix text from GPT: {matrix_text[:200]}...")
    lines = [line.strip() for line in matrix_text.split('\n') if '|' in line]
    data = [line.split('|') for line in lines]
    data = [[cell.strip() for cell in row] for row in data]
    headers = data[0]
    data = data[1:]
    return pd.DataFrame(data, columns=headers)

@app.callback(
    Output('matrix-preview', 'children'),
    Output('loading-output', 'children'),
    Output('chat-output', 'children'),
    [Input({'type': 'matrix-btn', 'index': matrix_label}, 'n_clicks') for matrix_label in matrix_types.keys()] +
    [Input('btn-send-chat', 'n_clicks')],
    [State('chat-input', 'value')],
    prevent_initial_call='initial_duplicate'
)
def handle_matrix_and_chat(*args):
    session_data, lock = get_session_data()
    ctx = callback_context
    matrix_btns_len = len(matrix_types)
    matrix_btn_inputs = args[:matrix_btns_len]
    chat_n_clicks = args[matrix_btns_len]
    chat_input_value = args[matrix_btns_len + 1]
    if not ctx.triggered:
        raise PreventUpdate
    triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
    if "matrix-btn" in triggered_id:
        try:
            triggered = json.loads(triggered_id)
            matrix_type = triggered['index']
        except Exception:
            raise PreventUpdate
        if not session_data['uploaded_files']:
            return html.Div("Please upload project artifacts before generating a matrix."), "", ""
        file_contents = list(session_data['file_texts'].values())
        with lock:
            try:
                session_data['matrix_type'] = matrix_type
                session_data['current_matrix'] = generate_matrix_with_gpt(matrix_type, file_contents)
                logger.info(f"{matrix_type} generated for session.")
                return dbc.Table.from_dataframe(session_data['current_matrix'], striped=True, bordered=True, hover=True), f"{matrix_type} generated", ""
            except Exception as e:
                logger.exception(f"Error generating matrix: {str(e)}")
                return html.Div(f"Error generating matrix: {str(e)}"), "Error", ""
    elif "btn-send-chat" in triggered_id:
        if not chat_input_value or session_data['current_matrix'] is None or session_data['matrix_type'] is None:
            raise PreventUpdate
        matrix_type = session_data['matrix_type']
        with lock:
            prompt = f"""Update the following {matrix_type} based on this instruction: {chat_input_value}
Current matrix:
{session_data['current_matrix'].to_string(index=False)}
Instructions:
1. Provide ONLY the updated matrix as a table.
2. Use ONLY pipe symbols (|) to separate columns.
3. Do NOT include any introductory text, descriptions, or explanations.
4. Do NOT use any dashes (-) or other formatting characters.
5. The first row should be the column headers.
6. Start the output immediately with the column headers.
7. Each subsequent row should represent a single item in the matrix.
Now, provide the updated {matrix_type}:
"""
            response = openai.ChatCompletion.create(
                model="gpt-4-turbo",
                messages=[
                    {"role": "system", "content": "You are a precise matrix updater that outputs only the requested matrix without any additional text. You will make assumptions as a project manager to produce the matrix based on the limited information provided"},
                    {"role": "user", "content": prompt}
                ]
            )
            updated_matrix_text = response.choices[0].message.content.strip()
            logger.info(f"Raw updated matrix text from GPT: {updated_matrix_text[:200]}...")
            lines = [line.strip() for line in updated_matrix_text.split('\n') if '|' in line]
            data = [line.split('|') for line in lines]
            data = [[cell.strip() for cell in row] for row in data]
            headers = data[0]
            data = data[1:]
            session_data['current_matrix'] = pd.DataFrame(data, columns=headers)
            return dbc.Table.from_dataframe(session_data['current_matrix'], striped=True, bordered=True, hover=True), "", f"Matrix updated based on: {chat_input_value}"
    else:
        raise PreventUpdate

@app.callback(
    Output("download-matrix", "data"),
    Input("btn-download", "n_clicks"),
    prevent_initial_call=True
)
def download_matrix(n_clicks):
    session_data, lock = get_session_data()
    if session_data['current_matrix'] is None or session_data['matrix_type'] is None:
        raise PreventUpdate
    output = io.BytesIO()
    with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
        session_data['current_matrix'].to_excel(writer, sheet_name='Sheet1', index=False)
    logger.info(f"Matrix downloaded: {session_data['matrix_type']}")
    return dcc.send_bytes(output.getvalue(), f"{session_data['matrix_type']}.xlsx")

if __name__ == '__main__':
    print("Starting the Dash application...")
    app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)
    print("Dash application has finished running.")