Spaces:
Paused
Paused
Update app.py via AI Editor
Browse files
app.py
CHANGED
@@ -86,10 +86,17 @@ def upload_to_gemini_file(decoded_bytes, filename):
|
|
86 |
logging.error(f"Exception during file upload to Gemini: {e}")
|
87 |
return None
|
88 |
|
89 |
-
def gemini_generate_content(prompt, file_id=None, chat_input=None):
|
90 |
try:
|
91 |
files = []
|
92 |
-
if
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
93 |
try:
|
94 |
gemini_file = genai.get_file(file_id)
|
95 |
files.append(gemini_file)
|
@@ -266,13 +273,15 @@ def process_document(action, selected_filename=None, chat_input=None, rfp_decode
|
|
266 |
findings_fileid = uploaded_documents_fileid.get(selected_filename)
|
267 |
proposal_fileid = proposals_fileid.get(selected_proposal_filename)
|
268 |
|
|
|
269 |
if not findings_fileid:
|
|
|
270 |
if selected_filename in shredded_documents:
|
271 |
findings_bytes = shredded_documents[selected_filename]
|
272 |
elif selected_filename in uploaded_documents_bytes:
|
273 |
findings_bytes = uploaded_documents_bytes[selected_filename]
|
274 |
else:
|
275 |
-
findings_bytes =
|
276 |
if findings_bytes:
|
277 |
try:
|
278 |
findings_fileid = upload_to_gemini_file(findings_bytes, selected_filename)
|
@@ -283,9 +292,7 @@ def process_document(action, selected_filename=None, chat_input=None, rfp_decode
|
|
283 |
logging.error(f"Failed to upload findings doc {selected_filename} for recover: {e}")
|
284 |
|
285 |
if not proposal_fileid:
|
286 |
-
proposal_bytes =
|
287 |
-
if selected_proposal_filename in proposals:
|
288 |
-
proposal_bytes = save_proposal_as_docx(proposals[selected_proposal_filename], selected_proposal_filename)
|
289 |
if proposal_bytes:
|
290 |
try:
|
291 |
proposal_fileid = upload_to_gemini_file(proposal_bytes, selected_proposal_filename)
|
@@ -297,28 +304,24 @@ def process_document(action, selected_filename=None, chat_input=None, rfp_decode
|
|
297 |
|
298 |
logging.info(f"Recovery: fixing proposal [{selected_proposal_filename}] based on findings [{selected_filename}]")
|
299 |
prompt = (
|
300 |
-
"You are a proposal compliance recovery expert. Use the
|
301 |
"Address ONLY those sections of the proposal that have a finding and a recommendation for improvement. "
|
302 |
"Do NOT change sections that are marked as fully compliant or already fully address the requirements. "
|
303 |
"For each section that needs fixing, revise it in the proposal to address the recommendation for compliance or to strengthen the response. "
|
304 |
"Return the full proposal with only the necessary sections revised, and leave all other sections untouched. "
|
305 |
-
"Do not add any introduction, summary, or comments. Return only the revised proposal in markdown, nothing else.\n
|
306 |
)
|
307 |
if chat_input:
|
308 |
prompt += f"User additional instructions: {chat_input}\n"
|
309 |
-
|
310 |
-
|
311 |
-
|
312 |
-
"---\nOriginal Proposal:\n"
|
313 |
-
f"{proposal_text}\n"
|
314 |
-
)
|
315 |
-
used_fileid = None
|
316 |
if findings_fileid:
|
317 |
-
|
318 |
-
|
319 |
-
|
320 |
|
321 |
-
result = gemini_generate_content(prompt,
|
322 |
if result and not result.startswith("Error"):
|
323 |
base_name = os.path.splitext(selected_proposal_filename)[0]
|
324 |
recovered_docx_name = f"{base_name}_recovered.docx"
|
|
|
86 |
logging.error(f"Exception during file upload to Gemini: {e}")
|
87 |
return None
|
88 |
|
89 |
+
def gemini_generate_content(prompt, file_id=None, chat_input=None, file_ids=None):
|
90 |
try:
|
91 |
files = []
|
92 |
+
if file_ids:
|
93 |
+
for fid in file_ids:
|
94 |
+
try:
|
95 |
+
gemini_file = genai.get_file(fid)
|
96 |
+
files.append(gemini_file)
|
97 |
+
except Exception as e:
|
98 |
+
logging.error(f"Could not fetch Gemini file for id {fid}: {e}")
|
99 |
+
elif file_id:
|
100 |
try:
|
101 |
gemini_file = genai.get_file(file_id)
|
102 |
files.append(gemini_file)
|
|
|
273 |
findings_fileid = uploaded_documents_fileid.get(selected_filename)
|
274 |
proposal_fileid = proposals_fileid.get(selected_proposal_filename)
|
275 |
|
276 |
+
# Ensure both findings and proposal are uploaded as files in Gemini
|
277 |
if not findings_fileid:
|
278 |
+
findings_bytes = None
|
279 |
if selected_filename in shredded_documents:
|
280 |
findings_bytes = shredded_documents[selected_filename]
|
281 |
elif selected_filename in uploaded_documents_bytes:
|
282 |
findings_bytes = uploaded_documents_bytes[selected_filename]
|
283 |
else:
|
284 |
+
findings_bytes = save_compliance_as_docx(findings_content, selected_filename)
|
285 |
if findings_bytes:
|
286 |
try:
|
287 |
findings_fileid = upload_to_gemini_file(findings_bytes, selected_filename)
|
|
|
292 |
logging.error(f"Failed to upload findings doc {selected_filename} for recover: {e}")
|
293 |
|
294 |
if not proposal_fileid:
|
295 |
+
proposal_bytes = save_proposal_as_docx(proposals[selected_proposal_filename], selected_proposal_filename)
|
|
|
|
|
296 |
if proposal_bytes:
|
297 |
try:
|
298 |
proposal_fileid = upload_to_gemini_file(proposal_bytes, selected_proposal_filename)
|
|
|
304 |
|
305 |
logging.info(f"Recovery: fixing proposal [{selected_proposal_filename}] based on findings [{selected_filename}]")
|
306 |
prompt = (
|
307 |
+
"You are a proposal compliance recovery expert. Use the findings and recommendations table and the original proposal response provided in the attached files. "
|
308 |
"Address ONLY those sections of the proposal that have a finding and a recommendation for improvement. "
|
309 |
"Do NOT change sections that are marked as fully compliant or already fully address the requirements. "
|
310 |
"For each section that needs fixing, revise it in the proposal to address the recommendation for compliance or to strengthen the response. "
|
311 |
"Return the full proposal with only the necessary sections revised, and leave all other sections untouched. "
|
312 |
+
"Do not add any introduction, summary, or comments. Return only the revised proposal in markdown, nothing else.\n"
|
313 |
)
|
314 |
if chat_input:
|
315 |
prompt += f"User additional instructions: {chat_input}\n"
|
316 |
+
|
317 |
+
# Compose file_ids and pass both files to Gemini in order
|
318 |
+
file_ids = []
|
|
|
|
|
|
|
|
|
319 |
if findings_fileid:
|
320 |
+
file_ids.append(findings_fileid)
|
321 |
+
if proposal_fileid:
|
322 |
+
file_ids.append(proposal_fileid)
|
323 |
|
324 |
+
result = gemini_generate_content(prompt, file_ids=file_ids)
|
325 |
if result and not result.startswith("Error"):
|
326 |
base_name = os.path.splitext(selected_proposal_filename)[0]
|
327 |
recovered_docx_name = f"{base_name}_recovered.docx"
|