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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -33
app.py CHANGED
@@ -154,32 +154,45 @@ def remove_file(n_clicks, existing_files):
154
  return [file for file in existing_files if file['props']['children'][1]['props']['children'] != removed_file]
155
 
156
  def generate_matrix_with_gpt(matrix_type, file_contents):
157
- prompt = f"Generate a {matrix_type} based on the following project artifacts:\n\n"
158
- prompt += "\n\n".join(file_contents)
159
- prompt += f"\n\nCreate a {matrix_type} in a format that can be represented as a pandas DataFrame. Use '|' to separate columns and ensure each row has the same number of columns."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
 
161
  response = openai.ChatCompletion.create(
162
  model="gpt-3.5-turbo",
163
  messages=[
164
- {"role": "system", "content": "You are a helpful assistant that generates project management matrices."},
165
  {"role": "user", "content": prompt}
166
  ]
167
  )
168
 
169
- matrix_text = response.choices[0].message.content
170
-
171
- # More robust parsing of the matrix text
172
- lines = [line.strip() for line in matrix_text.strip().split('\n') if line.strip()]
173
  data = [line.split('|') for line in lines]
174
-
175
- # Ensure all rows have the same number of columns
176
- max_columns = max(len(row) for row in data)
177
- data = [row + [''] * (max_columns - len(row)) for row in data]
178
-
179
- # Use the first row as headers, and the rest as data
180
  headers = data[0]
181
  data = data[1:]
182
-
183
  return pd.DataFrame(data, columns=headers)
184
 
185
  @app.callback(
@@ -220,38 +233,41 @@ def update_matrix_via_chat(n_clicks, chat_input):
220
  if not chat_input or current_matrix is None:
221
  raise PreventUpdate
222
 
223
- prompt = f"Update the following {matrix_type} based on this instruction: {chat_input}\n\n"
224
- prompt += current_matrix.to_string(index=False)
225
- prompt += "\n\nProvide the updated matrix using '|' to separate columns. Do not include any separator lines, headers, or formatting characters. Start directly with the column names separated by '|'."
 
 
 
 
 
 
 
 
 
 
 
 
 
226
 
227
  response = openai.ChatCompletion.create(
228
  model="gpt-3.5-turbo",
229
  messages=[
230
- {"role": "system", "content": "You are a helpful assistant that updates project management matrices without any formatting or separator lines."},
231
  {"role": "user", "content": prompt}
232
  ]
233
  )
234
 
235
- updated_matrix_text = response.choices[0].message.content
236
  print("Raw updated matrix text from GPT:", updated_matrix_text) # For debugging
237
 
238
- # Filter out any lines that don't contain the '|' character
239
- lines = [line.strip() for line in updated_matrix_text.strip().split('\n') if '|' in line]
240
-
241
- # Parse the matrix text
242
  data = [line.split('|') for line in lines]
243
-
244
- # Strip whitespace from each cell
245
  data = [[cell.strip() for cell in row] for row in data]
246
-
247
- # Ensure all rows have the same number of columns
248
- max_columns = max(len(row) for row in data)
249
- data = [row + [''] * (max_columns - len(row)) for row in data]
250
-
251
- # Use the first row as headers, and the rest as data
252
  headers = data[0]
253
  data = data[1:]
254
-
255
  current_matrix = pd.DataFrame(data, columns=headers)
256
 
257
  return f"Matrix updated based on: {chat_input}", dbc.Table.from_dataframe(current_matrix, striped=True, bordered=True, hover=True)
 
154
  return [file for file in existing_files if file['props']['children'][1]['props']['children'] != removed_file]
155
 
156
  def generate_matrix_with_gpt(matrix_type, file_contents):
157
+ prompt = f"""Generate a {matrix_type} based on the following project artifacts:
158
+
159
+ {' '.join(file_contents)}
160
+
161
+ Instructions:
162
+ 1. Create the {matrix_type} as a table.
163
+ 2. Use ONLY pipe symbols (|) to separate columns.
164
+ 3. Do NOT include any introductory text, descriptions, or explanations.
165
+ 4. Do NOT use any dashes (-) or other formatting characters.
166
+ 5. The first row should be the column headers.
167
+ 6. Start the output immediately with the column headers.
168
+ 7. Each subsequent row should represent a single item in the matrix.
169
+
170
+ Example format:
171
+ Header1|Header2|Header3
172
+ Item1A|Item1B|Item1C
173
+ Item2A|Item2B|Item2C
174
+
175
+ Now, generate the {matrix_type}:
176
+ """
177
 
178
  response = openai.ChatCompletion.create(
179
  model="gpt-3.5-turbo",
180
  messages=[
181
+ {"role": "system", "content": "You are a precise matrix generator that outputs only the requested matrix without any additional text."},
182
  {"role": "user", "content": prompt}
183
  ]
184
  )
185
 
186
+ matrix_text = response.choices[0].message.content.strip()
187
+ print("Raw matrix text from GPT:", matrix_text) # For debugging
188
+
189
+ lines = [line.strip() for line in matrix_text.split('\n') if '|' in line]
190
  data = [line.split('|') for line in lines]
191
+ data = [[cell.strip() for cell in row] for row in data]
192
+
 
 
 
 
193
  headers = data[0]
194
  data = data[1:]
195
+
196
  return pd.DataFrame(data, columns=headers)
197
 
198
  @app.callback(
 
233
  if not chat_input or current_matrix is None:
234
  raise PreventUpdate
235
 
236
+ prompt = f"""Update the following {matrix_type} based on this instruction: {chat_input}
237
+
238
+ Current matrix:
239
+ {current_matrix.to_string(index=False)}
240
+
241
+ Instructions:
242
+ 1. Provide ONLY the updated matrix as a table.
243
+ 2. Use ONLY pipe symbols (|) to separate columns.
244
+ 3. Do NOT include any introductory text, descriptions, or explanations.
245
+ 4. Do NOT use any dashes (-) or other formatting characters.
246
+ 5. The first row should be the column headers.
247
+ 6. Start the output immediately with the column headers.
248
+ 7. Each subsequent row should represent a single item in the matrix.
249
+
250
+ Now, provide the updated {matrix_type}:
251
+ """
252
 
253
  response = openai.ChatCompletion.create(
254
  model="gpt-3.5-turbo",
255
  messages=[
256
+ {"role": "system", "content": "You are a precise matrix updater that outputs only the requested matrix without any additional text."},
257
  {"role": "user", "content": prompt}
258
  ]
259
  )
260
 
261
+ updated_matrix_text = response.choices[0].message.content.strip()
262
  print("Raw updated matrix text from GPT:", updated_matrix_text) # For debugging
263
 
264
+ lines = [line.strip() for line in updated_matrix_text.split('\n') if '|' in line]
 
 
 
265
  data = [line.split('|') for line in lines]
 
 
266
  data = [[cell.strip() for cell in row] for row in data]
267
+
 
 
 
 
 
268
  headers = data[0]
269
  data = data[1:]
270
+
271
  current_matrix = pd.DataFrame(data, columns=headers)
272
 
273
  return f"Matrix updated based on: {chat_input}", dbc.Table.from_dataframe(current_matrix, striped=True, bordered=True, hover=True)