Spaces:
Paused
Paused
File size: 40,090 Bytes
bb8558f 34fe234 fbca851 34fe234 d9604c9 9792dc9 e2acf18 947d651 e065311 d9604c9 34fe234 d9604c9 34fe234 e065311 34fe234 d57ffe2 da12aa3 d57ffe2 da12aa3 94db5a5 34fe234 947d651 b8c66ce 947d651 d9604c9 e2acf18 e065311 72252ab e2acf18 e065311 8ede9ae e065311 e2acf18 e065311 e2acf18 72252ab e065311 da12aa3 947d651 da12aa3 e065311 da12aa3 e065311 b8c66ce 947d651 da12aa3 e065311 947d651 72252ab 9792dc9 0092a89 17fb27b 34f1f65 947d651 b8c66ce d9604c9 72252ab 831a192 da12aa3 17fb27b 07728ee da12aa3 07728ee da12aa3 07728ee da12aa3 72252ab 831a192 d9604c9 fbca851 d9604c9 9b51a58 34fe234 831a192 da12aa3 9abebbc 947d651 d9604c9 947d651 72252ab 947d651 d9604c9 e065311 947d651 72252ab 947d651 d9604c9 9792dc9 947d651 d9604c9 947d651 d9604c9 9abebbc 72252ab 07728ee 59f24ac 228c612 fbca851 59f24ac 307700a 3f743eb 59f24ac 3f743eb 9b51a58 3f743eb 59f24ac 9abebbc 947d651 bd8f663 947d651 307700a 947d651 bd8f663 e065311 947d651 307700a 947d651 17fb27b 947d651 e065311 bd8f663 307700a e065311 307700a 947d651 bd8f663 9abebbc 3f743eb 831a192 17fb27b 34f1f65 17fb27b 34f1f65 17fb27b 34f1f65 17fb27b 34f1f65 831a192 fbca851 831a192 fbca851 831a192 fbca851 34fe234 07728ee 831a192 07728ee 307700a 17fb27b 307700a 17fb27b 307700a 17fb27b 307700a 07728ee 307700a 07728ee 94db5a5 307700a 94db5a5 307700a 94db5a5 07728ee 307700a 17fb27b 307700a 831a192 307700a 07728ee 831a192 34fe234 9abebbc 947d651 0bdc7cb 34fe234 d9604c9 07728ee d9604c9 bb8558f cc1c8fe 831a192 d9604c9 94db5a5 bb8558f 17fb27b 94db5a5 07728ee 17fb27b cc1c8fe 07728ee d57ffe2 07728ee 9792dc9 d57ffe2 07728ee 71b42cc efb8d18 71b42cc d57ffe2 947d651 d57ffe2 d9604c9 d57ffe2 d9604c9 34fe234 831a192 fbca851 831a192 fbca851 17fb27b 947d651 4dc0cab fbca851 34f1f65 4dc0cab fbca851 4dc0cab fbca851 4dc0cab fbca851 94db5a5 17fb27b 4dc0cab b8c66ce 84ddce1 4dc0cab 831a192 4dc0cab 34f1f65 07728ee 9792dc9 94db5a5 17fb27b b8c66ce 07728ee 831a192 4dc0cab d57ffe2 2fccc3f 4dc0cab 9abebbc 947d651 fbca851 34f1f65 17fb27b 2fccc3f 307700a 947d651 b8c66ce 17fb27b b8c66ce 17fb27b b8c66ce 17fb27b b8c66ce 17fb27b b8c66ce 947d651 17fb27b 947d651 17fb27b 947d651 4dc0cab 07728ee 831a192 307700a 831a192 da12aa3 e065311 831a192 07728ee da12aa3 07728ee 831a192 07728ee 4dc0cab 07728ee 4dc0cab 07728ee da12aa3 e065311 07728ee da12aa3 07728ee 4dc0cab 07728ee 2fccc3f 07728ee 831a192 34f1f65 831a192 da12aa3 e065311 da12aa3 831a192 148203e 4dc0cab 831a192 4dc0cab 2fccc3f 07728ee 34f1f65 07728ee da12aa3 e065311 da12aa3 07728ee 148203e 4dc0cab 07728ee 4dc0cab 2fccc3f 94db5a5 17fb27b 94db5a5 07728ee 94db5a5 4dc0cab 17fb27b 9792dc9 4dc0cab 34f1f65 947d651 17fb27b 947d651 34f1f65 947d651 34f1f65 947d651 34f1f65 17fb27b 947d651 fbca851 17fb27b 947d651 fbca851 9dfe505 fbca851 148203e 17fb27b 4dc0cab fbca851 17fb27b 947d651 fbca851 9dfe505 148203e 17fb27b 4dc0cab fbca851 94db5a5 17fb27b 947d651 4dc0cab 34fe234 d9604c9 34fe234 |
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 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 |
import os
import base64
import io
import dash
from dash import dcc, html, Input, Output, State, callback_context, MATCH, ALL
import dash_bootstrap_components as dbc
import pandas as pd
import logging
from docx import Document
import mimetypes
from threading import Thread, Lock, Event
import threading
import time
import google.generativeai as genai
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s] %(levelname)s - %(message)s'
)
app = dash.Dash(__name__, external_stylesheets=[dbc.themes.BOOTSTRAP])
server = app.server
GOOGLE_API_KEY = os.environ.get("GOOGLE_API_KEY", "")
genai.configure(api_key=GOOGLE_API_KEY)
GEMINI_MODEL = "models/gemini-2.5-pro-preview-03-25"
MAX_INPUT_TOKENS = 1048576
MAX_OUTPUT_TOKENS = 65536
uploaded_documents = {}
uploaded_documents_fileid = {}
uploaded_proposals = {}
uploaded_proposals_fileid = {}
shredded_documents = {}
shredded_document = None
generated_response = None
gemini_lock = Lock()
stream_buffer = {"preview": ""}
stream_event = Event()
def decode_document(decoded_bytes):
try:
content = decoded_bytes.decode('utf-8')
logging.info("Document decoded as UTF-8.")
return content
except UnicodeDecodeError as e_utf8:
try:
content = decoded_bytes.decode('latin-1')
logging.warning("Document decoded as Latin-1 due to utf-8 decode error: %s", e_utf8)
return content
except Exception as e:
logging.error("Document decode failed for both utf-8 and latin-1: %s", e)
return None
def guess_mime_type(filename):
mime_type, _ = mimetypes.guess_type(filename)
if not mime_type:
ext = filename.lower().split('.')[-1]
if ext == "pdf":
mime_type = "application/pdf"
elif ext == "docx":
mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
elif ext == "doc":
mime_type = "application/msword"
elif ext == "xlsx":
mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
elif ext == "xls":
mime_type = "application/vnd.ms-excel"
else:
mime_type = "application/octet-stream"
return mime_type
def upload_to_gemini_file(decoded_bytes, filename):
try:
mime_type = guess_mime_type(filename)
file_obj = io.BytesIO(decoded_bytes)
file_obj.name = filename
myfile = genai.upload_file(file_obj, display_name=filename, mime_type=mime_type)
if hasattr(myfile, "name"):
logging.info(f"File uploaded to Gemini: {filename}, file_id: {myfile.name}")
return myfile.name
else:
logging.error(f"Gemini file upload did not return file name/id. Response: {myfile}")
return None
except Exception as e:
logging.error(f"Exception during file upload to Gemini: {e}")
return None
def gemini_generate_content_stream(prompt, file_id=None, chat_input=None, cancel_event=None):
try:
files = []
if file_id:
try:
gemini_file = genai.get_file(file_id)
files.append(gemini_file)
except Exception as e:
logging.error(f"Could not fetch Gemini file for id {file_id}: {e}")
content_list = []
if files:
content_list.extend(files)
content_list.append("\n\n")
content_list.append(prompt)
model = genai.GenerativeModel(GEMINI_MODEL)
response = model.generate_content(
contents=content_list,
generation_config=genai.types.GenerationConfig(
max_output_tokens=MAX_OUTPUT_TOKENS
)
)
result = response.text if hasattr(response, "text") else str(response)
chunk_size = 400
for i in range(0, len(result), chunk_size):
if cancel_event is not None and cancel_event.is_set():
logging.info("Gemini stream cancelled by user.")
yield "[Cancelled by user]\n"
break
yield result[:i+chunk_size]
time.sleep(0.08)
if cancel_event is not None and cancel_event.is_set():
yield "[Cancelled by user]\n"
except Exception as e:
logging.error("Error during Gemini generate_content: %s", e)
yield f"Error during Gemini completion: {e}"
def save_shredded_as_docx(shredded_text, rfp_filename):
doc = Document()
doc.add_heading(f"Shredded Requirements for {rfp_filename}", 0)
for line in shredded_text.split('\n'):
doc.add_paragraph(line)
memf = io.BytesIO()
doc.save(memf)
memf.seek(0)
return memf.read()
def save_proposal_as_docx(proposal_text, base_filename):
doc = Document()
doc.add_heading(f"Proposal Response for {base_filename}", 0)
for line in proposal_text.split('\n'):
doc.add_paragraph(line)
memf = io.BytesIO()
doc.save(memf)
memf.seek(0)
return memf.read()
def save_compliance_as_docx(compliance_text, rfp_filename):
doc = Document()
doc.add_heading(f"Compliance Check for {rfp_filename}", 0)
for line in compliance_text.split('\n'):
doc.add_paragraph(line)
memf = io.BytesIO()
doc.save(memf)
memf.seek(0)
return memf.read()
def process_document(action, selected_filename=None, chat_input=None, rfp_decoded_bytes=None, cancel_event=None, stream=True, selected_generated_filename=None):
global shredded_document, generated_response, stream_buffer
logging.info(f"Process document called with action: {action}")
doc_content = None
doc_fileid = None
if action in ["shred", "proposal", "compliance"]:
if selected_filename and selected_filename in uploaded_documents:
doc_content = uploaded_documents[selected_filename]
doc_fileid = uploaded_documents_fileid.get(selected_filename)
elif uploaded_documents:
doc_content = next(iter(uploaded_documents.values()))
selected_filename = next(iter(uploaded_documents.keys()))
doc_fileid = uploaded_documents_fileid.get(selected_filename)
else:
doc_content = None
doc_fileid = None
if action == 'shred':
if not doc_content:
logging.warning("No uploaded document found for shredding.")
return "No document uploaded.", None, None, None
prompt = (
"Analyze the following RFP/PWS/SOW/RFI and generate a requirements spreadsheet. Return ONLY the markdown spreadsheet and NOTHING else"
"Identify requirements by action words like 'shall', 'will', 'perform', etc. Also analye the document and put a column for recommended win theme for each requirement identified. Organize by PWS section and requirement. "
"Do not write as if responding to the proposal Return the ONLY markdown spreadsheet and NOTHING else, no intro, no outro, no comments.\n"
)
if chat_input:
prompt += f"User additional instructions: {chat_input}\n"
prompt += f"\nFile Name: {selected_filename}\n\n"
result_holder = {"text": None, "docx_name": None}
def thread_shred():
global shredded_document, stream_buffer
shredded_document = ""
stream_buffer["preview"] = ""
try:
logging.info("Starting Gemini generate_content for shredding.")
for partial in gemini_generate_content_stream(prompt, file_id=doc_fileid, chat_input=chat_input, cancel_event=cancel_event):
stream_buffer["preview"] = partial
if cancel_event is not None and cancel_event.is_set():
break
result = stream_buffer["preview"]
shredded_document = result
if not cancel_event or not cancel_event.is_set():
docx_bytes = save_shredded_as_docx(result, selected_filename)
generated_docx_name = f"{os.path.splitext(selected_filename)[0]}_shredded.docx"
shredded_documents[generated_docx_name] = docx_bytes
result_holder["text"] = result
result_holder["docx_name"] = generated_docx_name
else:
result_holder["text"] = "[Cancelled by user]\n"
result_holder["docx_name"] = None
logging.info("Document shredded successfully.")
except Exception as e:
shredded_document = f"Error during shredding: {e}"
logging.error("Error in thread_shred: %s", e)
result_holder["text"] = shredded_document
shredded_document = "Shredding in progress..."
stream_buffer["preview"] = ""
t = Thread(target=thread_shred)
t.start()
t.join()
if result_holder["docx_name"] and result_holder["text"]:
if result_holder["docx_name"] not in uploaded_documents:
uploaded_documents[result_holder["docx_name"]] = result_holder["text"]
return result_holder["text"], None, result_holder["docx_name"], result_holder["text"]
elif action == 'proposal':
if not doc_content:
logging.warning("No RFP/SOW/PWS/RFI document selected for proposal action.")
return "No RFP/SOW/PWS/RFI document selected.", None, None, None
rfp_filename = selected_filename
rfp_fileid = uploaded_documents_fileid.get(selected_filename)
if not rfp_fileid and rfp_decoded_bytes is not None:
try:
fileid = upload_to_gemini_file(rfp_decoded_bytes, rfp_filename)
if fileid:
uploaded_documents_fileid[rfp_filename] = fileid
rfp_fileid = fileid
logging.info(f"RFP file {rfp_filename} uploaded to Gemini for proposal.")
except Exception as e:
logging.error(f"Failed to upload RFP file {rfp_filename} for proposal: {e}")
prompt = (
"Respond to the following RFP/SOW/PWS/RFI by creating a highly detailed proposal response that follows each section and subsection header. "
"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. "
"You must show innovation in the approach. Refer to research that validates the approach and cite sources with measurable outcome. "
"Be sure to respond in paragraph format only, never use numbering, or bullets, only paragraph describing in detail.Return only the proposal response in markdown and nothing else, no comments, no intro, no outro, just the proposal\n"
)
if chat_input:
prompt += f"User additional instructions: {chat_input}\n"
prompt += f"\n---\nRFP/SOW/PWS/RFI ({rfp_filename}):\n{doc_content}\n"
logging.info(f"Sending proposal prompt to Gemini. RFP: {rfp_filename}")
result_holder = {"text": None, "docx_name": None}
def thread_proposal():
global generated_response, stream_buffer
generated_response = ""
stream_buffer["preview"] = ""
try:
logging.info("Connecting to Gemini for proposal.")
for partial in gemini_generate_content_stream(prompt, file_id=rfp_fileid, chat_input=chat_input, cancel_event=cancel_event):
stream_buffer["preview"] = partial
if cancel_event is not None and cancel_event.is_set():
break
response = stream_buffer["preview"]
generated_response = response
if not cancel_event or not cancel_event.is_set():
docx_bytes = save_proposal_as_docx(response, rfp_filename)
generated_docx_name = f"{os.path.splitext(rfp_filename)[0]}_proposal.docx"
uploaded_proposals[generated_docx_name] = response
uploaded_proposals_fileid[generated_docx_name] = None
result_holder["text"] = response
result_holder["docx_name"] = generated_docx_name
else:
result_holder["text"] = "[Cancelled by user]\n"
result_holder["docx_name"] = None
logging.info("Received proposal results from Gemini.")
except Exception as e:
generated_response = f"Error during Gemini completion: {e}"
logging.error("Error during Gemini proposal request: %s", e)
result_holder["text"] = generated_response
stream_buffer["preview"] = ""
t = Thread(target=thread_proposal)
t.start()
t.join()
return result_holder["text"], None, result_holder["docx_name"], result_holder["text"]
elif action == 'compliance':
if not selected_generated_filename or selected_generated_filename not in uploaded_proposals:
return "No generated document selected for compliance.", None, None, None
if not selected_filename or selected_filename not in uploaded_documents:
return "No RFP/SOW/PWS/RFI document selected for compliance.", None, None, None
rfp_text = uploaded_documents[selected_filename]
proposal_text = uploaded_proposals[selected_generated_filename]
logging.info(f"Compliance check: comparing proposal [{selected_generated_filename}] to RFP [{selected_filename}]")
prompt = (
"You are a proposal compliance expert. Use the following RFP/SOW/PWS/RFI and the generated proposal response. "
"Compare the proposal to the RFP requirements and win themes. "
"For each PWS section/requirement, determine if the proposal fully, partially, or does not address the requirement and win theme. "
"For each, give a finding and a recommendation to recover for compliance or to strengthen the response. "
"Return ONLY a markdown table (NO comments, NO intro, NO outro, NO summary) with the following columns: "
"| PWS Section Number | Section Name | Requirement Description | Finding | Recommendation to Recover for Compliance |. "
"Be concise and focus on actionable compliance gaps. Only the table, nothing else.\n\n"
f"---\nRFP/SOW/PWS/RFI ({selected_filename}):\n{rfp_text}\n"
"---\nGenerated Proposal Document:\n"
f"{proposal_text}\n"
)
result_holder = {"text": None, "docx_name": None}
def thread_compliance():
global stream_buffer
stream_buffer["preview"] = ""
try:
logging.info("Starting Gemini generate_content for compliance check.")
for partial in gemini_generate_content_stream(prompt, file_id=None, chat_input=None, cancel_event=cancel_event):
stream_buffer["preview"] = partial
if cancel_event is not None and cancel_event.is_set():
break
result = stream_buffer["preview"]
result_holder["text"] = result
if not cancel_event or not cancel_event.is_set():
docx_bytes = save_compliance_as_docx(result, selected_filename)
compliance_docx_name = f"{os.path.splitext(selected_filename)[0]}_compliance_check.docx"
uploaded_documents[compliance_docx_name] = result
shredded_documents[compliance_docx_name] = docx_bytes
result_holder["docx_name"] = compliance_docx_name
else:
result_holder["docx_name"] = None
logging.info("Compliance check completed.")
except Exception as e:
logging.error("Error during compliance check: %s", e)
result_holder["text"] = f"Error during compliance check: {e}"
stream_buffer["preview"] = ""
t = Thread(target=thread_compliance)
t.start()
t.join()
return result_holder["text"], None, result_holder["docx_name"], result_holder["text"]
elif action == 'recover':
return "Recovery not implemented yet.", None, None, None
elif action == 'board':
return "Virtual board not implemented yet.", None, None, None
elif action == 'loe':
return "LOE estimation not implemented yet.", None, None, None
return "Action not implemented yet.", None, None, None
def get_uploaded_doc_list(docdict):
if not docdict:
return html.Div("No documents uploaded.", style={"wordWrap": "break-word"})
doc_list = []
for filename in docdict:
file_content = docdict[filename]
mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" if filename.lower().endswith('.docx') else "text/plain"
try:
if filename.lower().endswith('.docx') and filename in shredded_documents:
docx_bytes = shredded_documents[filename]
b64 = base64.b64encode(docx_bytes).decode('utf-8')
else:
b64 = base64.b64encode(file_content.encode('utf-8')).decode('utf-8')
except Exception:
b64 = base64.b64encode(file_content.encode('utf-8')).decode('utf-8')
download_link = html.A(
filename,
href=f"data:{mime};base64,{b64}",
download=filename,
target="_blank",
style={"wordWrap": "break-word", "marginRight": "10px", "textDecoration": "underline"}
)
doc_list.append(
dbc.ListGroupItem([
download_link,
dbc.Button("Delete", id={'type': 'delete-doc-btn', 'index': filename, 'group': 'rfp'}, size="sm", color="danger", className="float-end ms-2")
], className="d-flex justify-content-between align-items-center")
)
return dbc.ListGroup(doc_list, flush=True)
def get_shredded_doc_list(shreddict):
if not shreddict:
return html.Div("No shredded requirements yet.", style={"wordWrap": "break-word"})
doc_list = []
for filename in shreddict:
b64 = base64.b64encode(shreddict[filename]).decode('utf-8')
download_link = html.A(
filename,
href=f"data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64,{b64}",
download=filename,
target="_blank",
style={"wordWrap": "break-word", "marginRight": "10px", "textDecoration": "underline"}
)
doc_list.append(
dbc.ListGroupItem([
download_link,
dbc.Button("Delete", id={'type': 'delete-shredded-btn', 'index': filename, 'group': 'shredded'}, size="sm", color="danger", className="float-end ms-2")
], className="d-flex justify-content-between align-items-center")
)
return dbc.ListGroup(doc_list, flush=True)
def get_uploaded_proposal_list(docdict):
if not docdict:
return html.Div("No proposal documents uploaded.", style={"wordWrap": "break-word"})
doc_list = []
for filename in docdict:
file_content = docdict[filename]
mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
try:
docx_bytes = save_proposal_as_docx(file_content, filename)
b64 = base64.b64encode(docx_bytes).decode('utf-8')
except Exception:
b64 = base64.b64encode(file_content.encode('utf-8')).decode('utf-8')
download_link = html.A(
filename,
href=f"data:{mime};base64,{b64}",
download=filename,
target="_blank",
style={"wordWrap": "break-word", "marginRight": "10px", "textDecoration": "underline"}
)
doc_list.append(
dbc.ListGroupItem([
download_link,
dbc.Button("Delete", id={'type': 'delete-proposal-btn', 'index': filename, 'group': 'proposal'}, size="sm", color="danger", className="float-end ms-2")
], className="d-flex justify-content-between align-items-center")
)
return dbc.ListGroup(doc_list, flush=True)
app.layout = dbc.Container([
dcc.Store(id='shred-store', data={'text': None, 'docx_name': None}),
dcc.Store(id='proposal-store', data={'text': None, 'docx_name': None}),
dcc.Store(id='stream-status', data={'streaming': False}),
dcc.Interval(id='stream-interval', interval=5000, n_intervals=0, disabled=True),
dbc.Row([
dbc.Col([
dbc.Card([
dbc.CardHeader(html.H5("RFP/SOW/PWS/RFI")),
dbc.CardBody([
html.H6("Uploaded Documents"),
html.Div(get_uploaded_doc_list(uploaded_documents), id='uploaded-doc-list'),
dcc.Dropdown(
id='select-document-dropdown',
options=[{'label': fn, 'value': fn} for fn in uploaded_documents.keys()],
placeholder="Select a document to work with",
value=next(iter(uploaded_documents), None),
style={"marginBottom": "10px"}
),
dcc.Upload(
id='upload-document',
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'
},
multiple=False
),
html.Hr(style={"marginTop": "20px", "marginBottom": "10px"}),
html.H6("Shredded/Compliance Documents"),
html.Div(get_shredded_doc_list(shredded_documents), id='shredded-doc-list')
])
], className="mb-3"),
dbc.Card([
dbc.CardHeader(html.H5("Proposal")),
dbc.CardBody([
html.H6("Generated Proposals"),
html.Div(get_uploaded_proposal_list(uploaded_proposals), id='uploaded-proposal-list'),
dcc.Dropdown(
id='select-proposal-dropdown',
options=[{'label': fn, 'value': fn} for fn in uploaded_proposals.keys()],
placeholder="Select a proposal document",
value=next(iter(uploaded_proposals), None),
style={"marginBottom": "10px"}
),
dcc.Upload(
id='upload-proposal',
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'
},
multiple=False
)
])
], className="mb-3"),
], style={'minWidth': '260px', 'width':'30vw','maxWidth':'30vw'}, width=3),
dbc.Col([
dbc.Card([
dbc.CardHeader(html.H2("RFP Proposal Assistant", style={'wordWrap': 'break-word'})),
dbc.CardBody([
dbc.Form([
dbc.Textarea(id="chat-input", placeholder="Enter additional instructions...", style={"width":"100%", "wordWrap": "break-word"}, className="mb-2"),
]),
html.Div([
dbc.Button("Shred", id="shred-action-btn", className="me-3 mb-2 btn-primary"),
dbc.Button("Proposal", id="proposal-action-btn", className="me-3 mb-2 btn-secondary"),
dbc.Button("Compliance", id="compliance-action-btn", className="me-3 mb-2 btn-tertiary"),
dbc.Button("Recover", id="recover-action-btn", className="me-3 mb-2 btn-tertiary"),
dbc.Button("Virtual Board", id="board-action-btn", className="me-3 mb-2 btn-tertiary"),
dbc.Button("LOE", id="loe-action-btn", className="mb-2 btn-tertiary"),
dbc.Button("Cancel", id="cancel-action-btn", className="ms-3 mb-2 btn-danger", color="danger"),
], className="mt-3 mb-3 d-flex flex-wrap"),
dcc.Loading(
id="loading",
type="default",
children=html.Div(id="output-data-upload"),
style={"textAlign": "center"}
)
])
], style={'backgroundColor': 'white'})
], style={'width':'70vw','maxWidth':'70vw'}, width=9)
], style={'marginTop':'20px'})
], fluid=True)
@app.callback(
Output('shred-store', 'data'),
Output('proposal-store', 'data'),
Output('output-data-upload', 'children'),
Output('uploaded-doc-list', 'children'),
Output('select-document-dropdown', 'options'),
Output('select-document-dropdown', 'value'),
Output('shredded-doc-list', 'children'),
Output('uploaded-proposal-list', 'children'),
Output('select-proposal-dropdown', 'options'),
Output('select-proposal-dropdown', 'value'),
Output('stream-status', 'data'),
Output('stream-interval', 'disabled'),
[
Input('shred-action-btn', 'n_clicks'),
Input('proposal-action-btn', 'n_clicks'),
Input('compliance-action-btn', 'n_clicks'),
Input('upload-document', 'contents'),
State('upload-document', 'filename'),
Input({'type': 'delete-doc-btn', 'index': ALL, 'group': 'rfp'}, 'n_clicks'),
State('select-document-dropdown', 'value'),
Input('upload-proposal', 'contents'),
State('upload-proposal', 'filename'),
Input({'type': 'delete-proposal-btn', 'index': ALL, 'group': 'proposal'}, 'n_clicks'),
State('select-proposal-dropdown', 'value'),
Input({'type': 'delete-shredded-btn', 'index': ALL, 'group': 'shredded'}, 'n_clicks'),
State('shredded-doc-list', 'children'),
State('select-proposal-dropdown', 'value'),
State('chat-input', 'value'),
State('select-document-dropdown', 'value'),
State('select-proposal-dropdown', 'value'),
Input('cancel-action-btn', 'n_clicks'),
Input('stream-interval', 'n_intervals'),
State('stream-status', 'data') # <-- Moved this to the last State, matching the last argument
],
prevent_initial_call=True
)
def master_callback(
shred_clicks, proposal_clicks, compliance_clicks,
rfp_content, rfp_filename, rfp_delete_clicks, selected_doc,
proposal_content, proposal_filename, proposal_delete_clicks, selected_proposal,
shredded_delete_clicks, shredded_doc_children,
select_proposal_dropdown_value,
chat_input, selected_filename, selected_proposal_dropdown, selected_proposal_dropdown_state,
cancel_clicks,
stream_n_intervals, stream_status
):
ctx = callback_context
triggered_id = ctx.triggered[0]['prop_id'].split('.')[0] if ctx.triggered else None
def safe_get_n_clicks(ctx, idx):
try:
return ctx.inputs_list[idx]
except Exception:
return []
upload_triggered = False
shred_store = {'text': None, 'docx_name': None}
proposal_store = {'text': None, 'docx_name': None}
streaming = False
rfp_delete_clicks = safe_get_n_clicks(ctx, 5)
proposal_delete_clicks = safe_get_n_clicks(ctx, 9)
shredded_delete_clicks = safe_get_n_clicks(ctx, 11)
uploaded_rfp_decoded_bytes = None
global gemini_lock, stream_buffer, stream_event
if triggered_id == 'stream-interval':
if not stream_status or not stream_status.get('streaming'):
return dash.no_update, dash.no_update, dash.no_update, dash.no_update, dash.no_update, dash.no_update, dash.no_update, dash.no_update, dash.no_update, dash.no_update, stream_status, True
preview = stream_buffer.get("preview", "")
still_streaming = gemini_lock.locked()
if not still_streaming:
stream_status['streaming'] = False
output_data_upload = dcc.Markdown(preview, style={"whiteSpace": "pre-wrap", "wordWrap": "break-word"})
doc_options = [{'label': fn, 'value': fn} for fn in uploaded_documents.keys()]
doc_value = selected_doc if selected_doc in uploaded_documents else (next(iter(uploaded_documents), None) if uploaded_documents else None)
shredded_doc_list_items = get_shredded_doc_list(shredded_documents)
uploaded_doc_list = get_uploaded_doc_list(uploaded_documents)
uploaded_proposal_list = get_uploaded_proposal_list(uploaded_proposals)
proposal_options = [{'label': fn, 'value': fn} for fn in uploaded_proposals.keys()]
proposal_value = select_proposal_dropdown_value if select_proposal_dropdown_value in uploaded_proposals else (next(iter(uploaded_proposals), None) if uploaded_proposals else None)
return (
shred_store, proposal_store, output_data_upload,
uploaded_doc_list, doc_options, doc_value,
shredded_doc_list_items,
uploaded_proposal_list, proposal_options, proposal_value,
stream_status,
True
)
return dash.no_update, dash.no_update, dcc.Markdown(preview, style={"whiteSpace": "pre-wrap", "wordWrap": "break-word"}), dash.no_update, dash.no_update, dash.no_update, dash.no_update, dash.no_update, dash.no_update, dash.no_update, stream_status, False
if triggered_id == 'cancel-action-btn':
stream_event.set()
streaming = False
output_data_upload = html.Div("[Cancelled by user]\n", style={"wordWrap": "break-word"})
doc_options = [{'label': fn, 'value': fn} for fn in uploaded_documents.keys()]
doc_value = selected_doc if selected_doc in uploaded_documents else (next(iter(uploaded_documents), None) if uploaded_documents else None)
shredded_doc_list_items = get_shredded_doc_list(shredded_documents)
uploaded_doc_list = get_uploaded_doc_list(uploaded_documents)
uploaded_proposal_list = get_uploaded_proposal_list(uploaded_proposals)
proposal_options = [{'label': fn, 'value': fn} for fn in uploaded_proposals.keys()]
proposal_value = select_proposal_dropdown_value if select_proposal_dropdown_value in uploaded_proposals else (next(iter(uploaded_proposals), None) if uploaded_proposals else None)
return (
shred_store, proposal_store, output_data_upload,
uploaded_doc_list, doc_options, doc_value,
shredded_doc_list_items,
uploaded_proposal_list, proposal_options, proposal_value,
{'streaming': False},
True
)
if triggered_id == 'upload-document' and rfp_content is not None and rfp_filename:
content_type, content_string = rfp_content.split(',')
decoded = base64.b64decode(content_string)
uploaded_rfp_decoded_bytes = decoded
text = decode_document(decoded)
fileid = None
if rfp_filename.lower().endswith(('.pdf', '.docx', '.xlsx', '.xls')):
fileid = upload_to_gemini_file(decoded, rfp_filename)
if text is not None:
uploaded_documents[rfp_filename] = text
if fileid:
uploaded_documents_fileid[rfp_filename] = fileid
logging.info(f"Document uploaded: {rfp_filename}")
else:
logging.error(f"Failed to decode uploaded document: {rfp_filename}")
upload_triggered = True
if triggered_id == 'upload-proposal' and proposal_content is not None and proposal_filename:
content_type, content_string = proposal_content.split(',')
decoded = base64.b64decode(content_string)
text = decode_document(decoded)
fileid = None
if proposal_filename.lower().endswith(('.pdf', '.docx', '.xlsx', '.xls')):
fileid = upload_to_gemini_file(decoded, proposal_filename)
if text is not None:
uploaded_proposals[proposal_filename] = text
if fileid:
uploaded_proposals_fileid[proposal_filename] = fileid
logging.info(f"Proposal uploaded: {proposal_filename}")
else:
logging.error(f"Failed to decode uploaded proposal: {proposal_filename}")
upload_triggered = True
if triggered_id and isinstance(rfp_delete_clicks, list):
for i, n_click in enumerate(rfp_delete_clicks):
if n_click:
btn_id = ctx.inputs_list[5][i]['id']
del_filename = btn_id['index']
if del_filename in uploaded_documents:
del uploaded_documents[del_filename]
if del_filename in uploaded_documents_fileid:
try:
genai.delete_file(uploaded_documents_fileid[del_filename])
except Exception as e:
logging.warning(f"Failed to delete Gemini file {del_filename}: {e}")
del uploaded_documents_fileid[del_filename]
logging.info(f"Document deleted: {del_filename}")
if selected_doc == del_filename:
selected_doc = None
upload_triggered = True
break
if triggered_id and isinstance(proposal_delete_clicks, list):
for i, n_click in enumerate(proposal_delete_clicks):
if n_click:
btn_id = ctx.inputs_list[9][i]['id']
del_filename = btn_id['index']
if del_filename in uploaded_proposals:
del uploaded_proposals[del_filename]
if del_filename in uploaded_proposals_fileid:
try:
genai.delete_file(uploaded_proposals_fileid[del_filename])
except Exception as e:
logging.warning(f"Failed to delete Gemini proposal file {del_filename}: {e}")
del uploaded_proposals_fileid[del_filename]
logging.info(f"Proposal deleted: {del_filename}")
if selected_proposal == del_filename:
selected_proposal = None
upload_triggered = True
break
if triggered_id and isinstance(shredded_delete_clicks, list):
for i, n_click in enumerate(shredded_delete_clicks):
if n_click:
btn_id = ctx.inputs_list[11][i]['id']
del_filename = btn_id['index']
if del_filename in shredded_documents:
del shredded_documents[del_filename]
logging.info(f"Shredded doc deleted: {del_filename}")
upload_triggered = True
break
doc_options = [{'label': fn, 'value': fn} for fn in uploaded_documents.keys()]
doc_value = selected_doc if selected_doc in uploaded_documents else (next(iter(uploaded_documents), None) if uploaded_documents else None)
shredded_doc_list_items = get_shredded_doc_list(shredded_documents)
uploaded_doc_list = get_uploaded_doc_list(uploaded_documents)
uploaded_proposal_list = get_uploaded_proposal_list(uploaded_proposals)
proposal_options = [{'label': fn, 'value': fn} for fn in uploaded_proposals.keys()]
proposal_value = select_proposal_dropdown_value if select_proposal_dropdown_value in uploaded_proposals else (next(iter(uploaded_proposals), None) if uploaded_proposals else None)
output_data_upload = html.Div("No action taken yet.", style={"wordWrap": "break-word"})
if triggered_id in ['shred-action-btn', 'proposal-action-btn', 'compliance-action-btn']:
got_lock = gemini_lock.acquire(blocking=False)
if not got_lock:
output_data_upload = html.Div("Another Gemini operation is in progress. Please wait or cancel.", style={"wordWrap": "break-word"})
return (
shred_store, proposal_store, output_data_upload,
uploaded_doc_list, doc_options, doc_value,
shredded_doc_list_items,
uploaded_proposal_list, proposal_options, proposal_value,
{'streaming': False},
True
)
stream_event.clear()
stream_buffer["preview"] = ""
streaming = True
def stream_gemini_thread(action, doc_value, chat_input, rfp_decoded_bytes, selected_generated_filename):
try:
process_document(action, doc_value, chat_input, rfp_decoded_bytes, cancel_event=stream_event, stream=True, selected_generated_filename=selected_generated_filename)
finally:
gemini_lock.release()
action_name = "shred" if triggered_id=="shred-action-btn" else ("proposal" if triggered_id=="proposal-action-btn" else "compliance")
t = Thread(target=stream_gemini_thread, args=(action_name, doc_value, chat_input, uploaded_rfp_decoded_bytes, select_proposal_dropdown_value))
t.daemon = True
t.start()
output_data_upload = dcc.Markdown("Starting Gemini operation...", style={"whiteSpace": "pre-wrap", "wordWrap": "break-word"})
return (
shred_store, proposal_store, output_data_upload,
uploaded_doc_list, doc_options, doc_value,
shredded_doc_list_items,
uploaded_proposal_list, proposal_options, proposal_value,
{'streaming': True},
False
)
if upload_triggered:
doc_value = doc_value if doc_value in uploaded_documents else (next(iter(uploaded_documents), None) if uploaded_documents else None)
proposal_value = proposal_value if proposal_value in uploaded_proposals else (next(iter(uploaded_proposals), None) if uploaded_proposals else None)
output_data_upload = html.Div("Upload/Delete completed.", style={"wordWrap": "break-word"})
return (
shred_store, proposal_store, output_data_upload,
uploaded_doc_list, doc_options, doc_value,
shredded_doc_list_items,
uploaded_proposal_list, proposal_options, proposal_value,
{'streaming': False},
True
)
doc_value = doc_value if doc_value in uploaded_documents else (next(iter(uploaded_documents), None) if uploaded_documents else None)
proposal_value = proposal_value if proposal_value in uploaded_proposals else (next(iter(uploaded_proposals), None) if uploaded_proposals else None)
return (
shred_store, proposal_store, output_data_upload,
uploaded_doc_list, doc_options, doc_value,
shredded_doc_list_items,
uploaded_proposal_list, proposal_options, proposal_value,
{'streaming': False},
True
)
if __name__ == '__main__':
print("Starting the Dash application...")
app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)
print("Dash application has finished running.") |