bluenevus commited on
Commit
b4a006c
·
verified ·
1 Parent(s): bfda8d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -32
app.py CHANGED
@@ -188,40 +188,25 @@ def generate_matrix_with_gpt(matrix_type, file_contents):
188
  [Input(f'btn-{matrix_type.lower().replace(" ", "-")}', 'n_clicks') for matrix_type in matrix_types.keys()],
189
  prevent_initial_call=True
190
  )
191
- def generate_matrix_with_gpt(matrix_type, file_contents):
192
- prompt = f"Generate a {matrix_type} based on the following project artifacts:\n\n"
193
- prompt += "\n\n".join(file_contents)
194
- prompt += f"\n\nCreate a {matrix_type} in a format that can be represented as a pandas DataFrame. Use '|' to separate columns. Do not include any separator lines, headers, or formatting characters. Start directly with the column names separated by '|'."
195
-
196
- response = openai.ChatCompletion.create(
197
- model="gpt-3.5-turbo",
198
- messages=[
199
- {"role": "system", "content": "You are a helpful assistant that generates project management matrices without any formatting or separator lines."},
200
- {"role": "user", "content": prompt}
201
- ]
202
- )
203
-
204
- matrix_text = response.choices[0].message.content
205
- print("Raw matrix text from GPT:", matrix_text) # For debugging
206
-
207
- # Filter out any lines that don't contain the '|' character
208
- lines = [line.strip() for line in matrix_text.strip().split('\n') if '|' in line]
209
-
210
- # More robust parsing of the matrix text
211
- data = [line.split('|') for line in lines]
212
-
213
- # Strip whitespace from each cell
214
- data = [[cell.strip() for cell in row] for row in data]
215
-
216
- # Ensure all rows have the same number of columns
217
- max_columns = max(len(row) for row in data)
218
- data = [row + [''] * (max_columns - len(row)) for row in data]
219
 
220
- # Use the first row as headers, and the rest as data
221
- headers = data[0]
222
- data = data[1:]
 
223
 
224
- return pd.DataFrame(data, columns=headers)
 
 
 
 
 
225
 
226
  @app.callback(
227
  Output('chat-output', 'children'),
 
188
  [Input(f'btn-{matrix_type.lower().replace(" ", "-")}', 'n_clicks') for matrix_type in matrix_types.keys()],
189
  prevent_initial_call=True
190
  )
191
+ def generate_matrix(*args):
192
+ global current_matrix, matrix_type
193
+ ctx = dash.callback_context
194
+ if not ctx.triggered:
195
+ raise PreventUpdate
196
+ button_id = ctx.triggered[0]['prop_id'].split('.')[0]
197
+ matrix_type = button_id.replace('btn-', '').replace('-', ' ').title()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
198
 
199
+ if not uploaded_files:
200
+ return html.Div("Please upload project artifacts before generating a matrix."), ""
201
+
202
+ file_contents = list(uploaded_files.values())
203
 
204
+ try:
205
+ current_matrix = generate_matrix_with_gpt(matrix_type, file_contents)
206
+ return dbc.Table.from_dataframe(current_matrix, striped=True, bordered=True, hover=True), f"{matrix_type} generated"
207
+ except Exception as e:
208
+ print(f"Error generating matrix: {str(e)}")
209
+ return html.Div(f"Error generating matrix: {str(e)}"), "Error"
210
 
211
  @app.callback(
212
  Output('chat-output', 'children'),