File size: 14,708 Bytes
ab10305 e29b31c 60052d6 5199415 ab10305 e29b31c ab10305 e0af9ed ab10305 b8f1b65 ab10305 79ae593 ab10305 79ae593 ab10305 b8f1b65 ab10305 79ae593 ab10305 b4a6fb4 ab10305 b4a6fb4 ab10305 b4a6fb4 ab10305 b4a6fb4 623d471 9f88b96 ab10305 623d471 ab10305 f62109e ab10305 b4a6fb4 803301e f62109e 803301e ab10305 b4a6fb4 ab10305 e29b31c ab10305 e29b31c ab10305 e29b31c 5199415 e29b31c 5199415 e29b31c a0d2fac 5199415 edd7603 5199415 ab10305 e29b31c 15ca67d e29b31c 5199415 717943b 5199415 e29b31c 9f88b96 717943b e29b31c ab4acd6 e29b31c 4032a87 e29b31c e0af9ed e29b31c ab4acd6 fec5de2 ab4acd6 fec5de2 ab4acd6 e29b31c 5199415 ab10305 623d471 ab10305 623d471 b4a006c 8fb1aea 623d471 b4a006c 5199415 8fb1aea b4a006c 5199415 b4a006c bfda8d6 ab10305 bfda8d6 e29b31c ab10305 ab4acd6 e29b31c 9b44b20 e29b31c ab4acd6 bfda8d6 ab4acd6 bfda8d6 ab4acd6 bfda8d6 ab4acd6 e29b31c ab10305 e29b31c bfda8d6 ab10305 e285981 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 |
import dash
from dash import dcc, html, Input, Output, State, callback
import dash_bootstrap_components as dbc
import base64
import io
import pandas as pd
import openai
import os
import time
from dash.exceptions import PreventUpdate
import PyPDF2
import docx
import chardet
import json
from dash.exceptions import PreventUpdate
# Initialize the Dash app
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
# Get OpenAI API key from Hugging Face Spaces environment variable
openai.api_key = os.environ.get('OPENAI_API_KEY')
# Global variables
uploaded_files = {}
current_matrix = None
matrix_type = None
# Matrix types and their descriptions
matrix_types = {
"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.",
"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.layout = dbc.Container([
dbc.Row([
dbc.Col([
html.H4("Project Artifacts", className="mt-3 mb-4"),
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([
html.H5("Uploaded Files"),
dbc.ListGroup(id='file-list')
], className="mt-3"),
html.Hr(),
html.Div([
dbc.Button(
matrix_type,
id={'type': 'matrix-button', 'index': matrix_type},
color="link",
className="mb-2 w-100 text-left",
style={
'textAlign': 'left',
'textDecoration': 'none',
'padding': '0.375rem 0.75rem',
'transition': 'background-color 0.3s'
}
) for matrix_type in matrix_types.keys()
], className="left-buttons-container")
], width=3),
dbc.Col([
html.Div(style={"height": "20px"}), # Small gap
html.Div(id="loading-output"), # Text status box
html.Div([ # New row for progress indicator
dcc.Loading(
id="loading-indicator",
type="dot",
children=[html.Div(style={"height": "50px"})] # Increased height for visibility
),
], className="my-3"), # Add margin for separation
html.Div(id='matrix-preview', className="border p-3 mb-3"),
html.Div(id="loading-output"), # Text status box
dcc.Loading(
id="loading-indicator",
type="dot",
children=[html.Div()] # Empty div for the blinking dots
),
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(),
html.Div(style={"height": "20px"}), # Small gap
dcc.Loading(
id="chat-loading",
type="dot",
children=[
dbc.Input(id="chat-input", type="text", placeholder="Chat with GPT to update matrix...", className="mb-2"),
dbc.Button("Send", id="btn-send-chat", color="primary", className="mb-3"),
html.Div(id="chat-output")
]
)
], width=9)
]),
html.Div(id='uploaded-files-state', style={'display': 'none'}),
dcc.Store(id='matrix-type-store'),
html.Div(id='matrix-button-clicks', style={'display': 'none'})
], fluid=True)
def parse_file_content(contents, filename):
content_type, content_string = contents.split(',')
decoded = base64.b64decode(content_string)
try:
if filename.endswith('.pdf'):
with io.BytesIO(decoded) as pdf_file:
reader = PyPDF2.PdfReader(pdf_file)
return ' '.join([page.extract_text() 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:
print(f"Error processing file {filename}: {str(e)}")
return "Error processing file"
@app.callback(
Output('file-list', 'children'),
Input('upload-files', 'contents'),
State('upload-files', 'filename'),
State('file-list', 'children')
)
def update_output(list_of_contents, list_of_names, existing_files):
global uploaded_files
if list_of_contents is not None:
new_files = []
for content, name in zip(list_of_contents, list_of_names):
file_content = parse_file_content(content, name)
uploaded_files[name] = file_content
new_files.append(dbc.ListGroupItem([
dbc.Button("×", id={'type': 'remove-file', 'index': name},
color="danger", size="sm", className="me-2"),
html.Span(name)
]))
existing_files = existing_files or []
return existing_files + new_files
return existing_files or []
@app.callback(
Output('file-list', 'children', allow_duplicate=True),
Input({'type': 'remove-file', 'index': dash.ALL}, 'n_clicks'),
State('file-list', 'children'),
prevent_initial_call=True
)
def remove_file(n_clicks, existing_files):
global uploaded_files
ctx = dash.callback_context
if not ctx.triggered:
raise PreventUpdate
triggered_id = ctx.triggered[0]['prop_id']
removed_file = triggered_id.split('"index":')[1].split('}')[0].strip('"')
uploaded_files.pop(removed_file, None)
# Updated file filtering logic
return [
file for file in existing_files
if isinstance(file, dict) and
'props' in file and
'children' in file['props'] and
isinstance(file['props']['children'], list) and
len(file['props']['children']) > 1 and
isinstance(file['props']['children'][1], dict) and
'children' in file['props']['children'][1] and
file['props']['children'][1]['children'] != removed_file
]
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."},
{"role": "user", "content": prompt}
]
)
matrix_text = response.choices[0].message.content.strip()
print("Raw matrix text from GPT:", matrix_text) # For debugging
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('uploaded-files-state', 'children'),
Input('file-list', 'children')
)
def update_uploaded_files_state(file_list):
global uploaded_files
return json.dumps(list(uploaded_files.keys()))
@app.callback(
Output('matrix-preview', 'children'),
Output('loading-output', 'children'),
Input({'type': 'matrix-button', 'index': dash.ALL}, 'n_clicks'),
State('uploaded-files-state', 'children'),
prevent_initial_call=True
)
def generate_matrix(n_clicks, uploaded_files_json):
global current_matrix, matrix_type
ctx = dash.callback_context
if not ctx.triggered:
raise PreventUpdate
button_id = ctx.triggered[0]['prop_id']
matrix_type = json.loads(button_id.split('.')[0])['index']
if not uploaded_files_json:
return html.Div("Please upload project artifacts before generating a matrix."), ""
uploaded_files_list = json.loads(uploaded_files_json)
if not uploaded_files_list:
return html.Div("Please upload project artifacts before generating a matrix."), ""
try:
current_matrix = generate_matrix_with_gpt(matrix_type, [uploaded_files[file] for file in uploaded_files_list])
return dbc.Table.from_dataframe(current_matrix, striped=True, bordered=True, hover=True), f"{matrix_type} generated"
except Exception as e:
print(f"Error generating matrix: {str(e)}")
return html.Div(f"Error generating matrix: {str(e)}"), "Error"
@app.callback(
Output('chat-output', 'children'),
Output('matrix-preview', 'children', allow_duplicate=True),
Input('btn-send-chat', 'n_clicks'),
State('chat-input', 'value'),
prevent_initial_call=True
)
def update_matrix_via_chat(n_clicks, chat_input):
global current_matrix, matrix_type
if not chat_input or current_matrix is None:
raise PreventUpdate
prompt = f"""Update the following {matrix_type} based on this instruction: {chat_input}
Current matrix:
{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-3.5-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 informaton provided"},
{"role": "user", "content": prompt}
]
)
updated_matrix_text = response.choices[0].message.content.strip()
print("Raw updated matrix text from GPT:", updated_matrix_text) # For debugging
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:]
current_matrix = pd.DataFrame(data, columns=headers)
return f"Matrix updated based on: {chat_input}", dbc.Table.from_dataframe(current_matrix, striped=True, bordered=True, hover=True)
@app.callback(
Output("download-matrix", "data"),
Input("btn-download", "n_clicks"),
prevent_initial_call=True
)
def download_matrix(n_clicks):
global current_matrix, matrix_type
if current_matrix is None:
raise PreventUpdate
# Create an in-memory Excel file
output = io.BytesIO()
with pd.ExcelWriter(output, engine='xlsxwriter') as writer:
current_matrix.to_excel(writer, sheet_name='Sheet1', index=False)
return dcc.send_bytes(output.getvalue(), f"{matrix_type}.xlsx")
if __name__ == '__main__':
print("Starting the Dash application...")
app.run(debug=False, host='0.0.0.0', port=7860)
print("Dash application has finished running.") |