bluenevus commited on
Commit
0092a89
·
1 Parent(s): bd8f663

Update app.py via AI Editor

Browse files
Files changed (1) hide show
  1. app.py +30 -6
app.py CHANGED
@@ -72,6 +72,16 @@ def save_shredded_as_docx(shredded_text, rfp_filename):
72
  memf.seek(0)
73
  return memf.read()
74
 
 
 
 
 
 
 
 
 
 
 
75
  def process_document(action, selected_filename=None, chat_input=None, selected_proposal=None, selected_generated=None):
76
  global shredded_document, generated_response
77
  logging.info(f"Process document called with action: {action}")
@@ -144,7 +154,7 @@ def process_document(action, selected_filename=None, chat_input=None, selected_p
144
  if chat_input:
145
  prompt += f"User additional instructions: {chat_input}\n"
146
  prompt += f"\nFile Name: {selected_filename}\n\n{shredded_document}"
147
- result_holder = {"text": None}
148
  def thread_generate():
149
  global generated_response
150
  generated_response = ""
@@ -153,7 +163,11 @@ def process_document(action, selected_filename=None, chat_input=None, selected_p
153
  result = anthropic_stream_generate(prompt)
154
  generated_response = result
155
  logging.info("Proposal response generated successfully.")
 
 
156
  result_holder["text"] = result
 
 
157
  except Exception as e:
158
  generated_response = f"Error during generation: {e}"
159
  logging.error("Error in thread_generate: %s", e)
@@ -163,7 +177,7 @@ def process_document(action, selected_filename=None, chat_input=None, selected_p
163
  t = Thread(target=thread_generate)
164
  t.start()
165
  t.join()
166
- return result_holder["text"], None, None
167
 
168
  elif action == 'proposal':
169
  rfp_content = None
@@ -194,18 +208,22 @@ def process_document(action, selected_filename=None, chat_input=None, selected_p
194
  "Respond to the following RFP/SOW/PWS/RFI provided and focus on the Generated Document provided by creating a highly detailed proposal response that follows each section and subsection header and numbering. "
195
  "The response to each section and subsection will be compliant and compelling, focusing on describing the approach, and how the labor category uses a specific industry standard process in a workflow described in steps, and how technology is used. "
196
  "You must show innovation in the approach. Refer to research that validates the approach and cite sources with measurable outcome. "
197
- "Be sure to respond in paragraph format, significantly limiting the use of bullets.\n"
198
  )
199
  if chat_input:
200
  prompt += f"User additional instructions: {chat_input}\n"
201
  prompt += f"\n---\nRFP/SOW/PWS/RFI ({rfp_filename}):\n{rfp_content}\n"
202
  prompt += f"\n---\nGenerated Document ({generated_docname}):\n{generated_doc_content}\n"
203
  logging.info("Sending proposal prompt to anthropic. This is a streaming call.")
204
- result_holder = {"text": None}
205
  def thread_proposal():
206
  try:
207
  result = anthropic_stream_generate(prompt)
 
 
208
  result_holder["text"] = result
 
 
209
  except Exception as e:
210
  logging.error("Error during streaming proposal request: %s", e)
211
  result_holder["text"] = f"Error during streaming: {e}"
@@ -213,7 +231,7 @@ def process_document(action, selected_filename=None, chat_input=None, selected_p
213
  t = Thread(target=thread_proposal)
214
  t.start()
215
  t.join()
216
- return result_holder["text"], None, None
217
 
218
  elif action == 'compliance':
219
  return "Compliance checking not implemented yet.", None, None
@@ -514,8 +532,14 @@ def master_callback(
514
  generated_documents[generated_docx_name] = generated_docx_bytes
515
  logging.info(f"Generated docx saved: {generated_docx_name}")
516
  new_selected_generated = generated_docx_name
 
517
  elif triggered_id == 'generate-action-btn':
518
- result, _, _ = process_document('proposal', selected_filename, chat_input, selected_proposal_dropdown, selected_generated_dropdown_state)
 
 
 
 
 
519
  elif triggered_id == 'compliance-action-btn':
520
  result, _, _ = process_document('compliance', selected_filename, chat_input)
521
  elif triggered_id == 'recover-action-btn':
 
72
  memf.seek(0)
73
  return memf.read()
74
 
75
+ def save_proposal_as_docx(proposal_text, base_filename):
76
+ doc = Document()
77
+ doc.add_heading(f"Proposal Response for {base_filename}", 0)
78
+ for line in proposal_text.split('\n'):
79
+ doc.add_paragraph(line)
80
+ memf = io.BytesIO()
81
+ doc.save(memf)
82
+ memf.seek(0)
83
+ return memf.read()
84
+
85
  def process_document(action, selected_filename=None, chat_input=None, selected_proposal=None, selected_generated=None):
86
  global shredded_document, generated_response
87
  logging.info(f"Process document called with action: {action}")
 
154
  if chat_input:
155
  prompt += f"User additional instructions: {chat_input}\n"
156
  prompt += f"\nFile Name: {selected_filename}\n\n{shredded_document}"
157
+ result_holder = {"text": None, "docx_bytes": None, "docx_name": None}
158
  def thread_generate():
159
  global generated_response
160
  generated_response = ""
 
163
  result = anthropic_stream_generate(prompt)
164
  generated_response = result
165
  logging.info("Proposal response generated successfully.")
166
+ docx_bytes = save_proposal_as_docx(result, selected_filename)
167
+ generated_docx_name = f"{os.path.splitext(selected_filename)[0]}_proposal.docx"
168
  result_holder["text"] = result
169
+ result_holder["docx_bytes"] = docx_bytes
170
+ result_holder["docx_name"] = generated_docx_name
171
  except Exception as e:
172
  generated_response = f"Error during generation: {e}"
173
  logging.error("Error in thread_generate: %s", e)
 
177
  t = Thread(target=thread_generate)
178
  t.start()
179
  t.join()
180
+ return result_holder["text"], result_holder["docx_bytes"], result_holder["docx_name"]
181
 
182
  elif action == 'proposal':
183
  rfp_content = None
 
208
  "Respond to the following RFP/SOW/PWS/RFI provided and focus on the Generated Document provided by creating a highly detailed proposal response that follows each section and subsection header and numbering. "
209
  "The response to each section and subsection will be compliant and compelling, focusing on describing the approach, and how the labor category uses a specific industry standard process in a workflow described in steps, and how technology is used. "
210
  "You must show innovation in the approach. Refer to research that validates the approach and cite sources with measurable outcome. "
211
+ "Be sure to respond in paragraph format only, no numbering, no bullets, only paragraph describing in detail.\n"
212
  )
213
  if chat_input:
214
  prompt += f"User additional instructions: {chat_input}\n"
215
  prompt += f"\n---\nRFP/SOW/PWS/RFI ({rfp_filename}):\n{rfp_content}\n"
216
  prompt += f"\n---\nGenerated Document ({generated_docname}):\n{generated_doc_content}\n"
217
  logging.info("Sending proposal prompt to anthropic. This is a streaming call.")
218
+ result_holder = {"text": None, "docx_bytes": None, "docx_name": None}
219
  def thread_proposal():
220
  try:
221
  result = anthropic_stream_generate(prompt)
222
+ docx_bytes = save_proposal_as_docx(result, rfp_filename)
223
+ generated_docx_name = f"{os.path.splitext(rfp_filename)[0]}_{os.path.splitext(generated_docname)[0]}_proposal.docx"
224
  result_holder["text"] = result
225
+ result_holder["docx_bytes"] = docx_bytes
226
+ result_holder["docx_name"] = generated_docx_name
227
  except Exception as e:
228
  logging.error("Error during streaming proposal request: %s", e)
229
  result_holder["text"] = f"Error during streaming: {e}"
 
231
  t = Thread(target=thread_proposal)
232
  t.start()
233
  t.join()
234
+ return result_holder["text"], result_holder["docx_bytes"], result_holder["docx_name"]
235
 
236
  elif action == 'compliance':
237
  return "Compliance checking not implemented yet.", None, None
 
532
  generated_documents[generated_docx_name] = generated_docx_bytes
533
  logging.info(f"Generated docx saved: {generated_docx_name}")
534
  new_selected_generated = generated_docx_name
535
+
536
  elif triggered_id == 'generate-action-btn':
537
+ result, generated_docx_bytes, generated_docx_name = process_document('generate', selected_filename, chat_input)
538
+ if generated_docx_bytes and generated_docx_name:
539
+ generated_documents[generated_docx_name] = generated_docx_bytes
540
+ logging.info(f"Generated proposal docx saved: {generated_docx_name}")
541
+ new_selected_generated = generated_docx_name
542
+
543
  elif triggered_id == 'compliance-action-btn':
544
  result, _, _ = process_document('compliance', selected_filename, chat_input)
545
  elif triggered_id == 'recover-action-btn':