proposal-writer / app.py
bluenevus's picture
Update app.py via AI Editor
e70fe8e
raw
history blame
44.8 kB
# Generated code appears here# Generated code appears hereimport 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 Lock
import tempfile
import shutil
import uuid
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
SESSION_STORE = {}
def get_session_id():
ctx = dash.callback_context
sid = None
# Dash >=2.9.0 supports flask request in ctx.request
if hasattr(ctx, "request") and hasattr(ctx.request, "cookies"):
cookies = ctx.request.cookies
sid = cookies.get('dash_session', None)
if not sid:
# fallback
sid = str(uuid.uuid4())
return sid
def get_session_data(session_id):
if session_id not in SESSION_STORE:
tempdir = tempfile.mkdtemp(prefix="rfp_session_")
SESSION_STORE[session_id] = {
"uploaded_documents": {},
"uploaded_documents_fileid": {},
"uploaded_documents_bytes": {},
"proposals": {},
"proposals_fileid": {},
"shredded_documents": {},
"generated_response": None,
"gemini_lock": Lock(),
"session_tempdir": tempdir
}
return SESSION_STORE[session_id]
def truncate_filename(filename, maxlen=30):
if len(filename) <= maxlen:
return filename
else:
partlen = (maxlen - 3) // 2
return filename[:partlen] + "..." + filename[-partlen:]
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(prompt, file_id=None, chat_input=None, file_ids=None):
try:
files = []
if file_ids:
for fid in file_ids:
try:
gemini_file = genai.get_file(fid)
files.append(gemini_file)
except Exception as e:
logging.error(f"Could not fetch Gemini file for id {fid}: {e}")
elif 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)
return result
except Exception as e:
logging.error("Error during Gemini generate_content: %s", e)
return 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 save_virtual_board_as_docx(board_text, base_filename):
doc = Document()
doc.add_heading(f"Evaluation Board for {base_filename}", 0)
for line in board_text.split('\n'):
doc.add_paragraph(line)
memf = io.BytesIO()
doc.save(memf)
memf.seek(0)
return memf.read()
def save_loe_as_docx(loe_text, proposal_filename):
doc = Document()
doc.add_heading(f"Level of Effort for {proposal_filename}", 0)
for line in loe_text.split('\n'):
doc.add_paragraph(line)
memf = io.BytesIO()
doc.save(memf)
memf.seek(0)
return memf.read()
def process_document(sess_data, action, selected_filename=None, chat_input=None, rfp_decoded_bytes=None, selected_proposal_filename=None):
doc_content = None
doc_fileid = None
if action in ["shred", "compliance", "virtual_board", "proposal", "recover"]:
if selected_filename and selected_filename in sess_data["uploaded_documents"]:
doc_content = sess_data["uploaded_documents"][selected_filename]
doc_fileid = sess_data["uploaded_documents_fileid"].get(selected_filename)
elif sess_data["uploaded_documents"]:
doc_content = next(iter(sess_data["uploaded_documents"].values()))
selected_filename = next(iter(sess_data["uploaded_documents"].keys()))
doc_fileid = sess_data["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, 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"
prompt += doc_content
result = gemini_generate_content(prompt, file_id=doc_fileid, chat_input=chat_input)
if result and not result.startswith("Error"):
docx_bytes = save_shredded_as_docx(result, selected_filename)
generated_docx_name = f"{os.path.splitext(selected_filename)[0]}_shredded.docx"
sess_data["uploaded_documents"][generated_docx_name] = result
sess_data["shredded_documents"][generated_docx_name] = docx_bytes
return result, generated_docx_name, docx_bytes, None, None
else:
return result, None, None, None, None
elif action == 'compliance':
if not selected_proposal_filename or selected_proposal_filename not in sess_data["proposals"]:
return "No proposal document selected for compliance.", None, None, None, None
if not selected_filename or selected_filename not in sess_data["uploaded_documents"]:
return "No RFP/SOW/PWS/RFI document selected for compliance.", None, None, None, None
rfp_text = sess_data["uploaded_documents"][selected_filename]
proposal_text = sess_data["proposals"][selected_proposal_filename]
logging.info(f"Compliance check: comparing proposal [{selected_proposal_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 = gemini_generate_content(prompt, file_id=None, chat_input=None)
if result and not result.startswith("Error"):
docx_bytes = save_compliance_as_docx(result, selected_filename)
compliance_docx_name = f"{os.path.splitext(selected_filename)[0]}_compliance_check.docx"
sess_data["uploaded_documents"][compliance_docx_name] = result
sess_data["shredded_documents"][compliance_docx_name] = docx_bytes
return result, compliance_docx_name, docx_bytes, None, None
else:
return result, None, None, None, None
elif action == 'virtual_board':
if not selected_proposal_filename or selected_proposal_filename not in sess_data["proposals"]:
return "No proposal document selected for evaluation board.", None, None, None, None
if not selected_filename or selected_filename not in sess_data["uploaded_documents"]:
return "No RFP/SOW/PWS/RFI document selected for evaluation board.", None, None, None, None
rfp_text = sess_data["uploaded_documents"][selected_filename]
proposal_text = sess_data["proposals"][selected_proposal_filename]
logging.info(f"Evaluation Board: extracting criteria from RFP [{selected_filename}], evaluating proposal [{selected_proposal_filename}]")
prompt = (
"You are a federal acquisition evaluation board. Your job is to extract Section L and Section M (evaluation criteria) from the following RFP/SOW/PWS/RFI if they exist. "
"If Section L or Section M are not present, then read all requirements and develop a set of evaluation criteria that would be appropriate for this type of acquisition. "
"List each evaluation criterion exactly as it appears in the document or as you have developed. "
"For each extracted or developed evaluation criterion, evaluate the provided proposal and rate it as exactly one of: unacceptable, acceptable, good, or outstanding (no other ratings allowed). Add a column to explain the reason for the rating. Also include a column for recommendations to remediate the document so that it gets a better rating "
"Return ONLY a markdown spreadsheet/table (NO intro, NO outro, NO comments, NO summary) with exactly these columns: | Evaluation Criteria | Evaluation | Reason | Recommendation |"
"Each row should have the criterion exactly as extracted or developed, and the evaluation for how well the proposal meets or exceeds it. Only the table, nothing else.\n\n"
f"---\nRFP/SOW/PWS/RFI ({selected_filename}):\n{rfp_text}\n"
"---\nProposal Document:\n"
f"{proposal_text}\n"
)
result = gemini_generate_content(prompt, file_id=None, chat_input=None)
if result and not result.startswith("Error"):
docx_bytes = save_virtual_board_as_docx(result, selected_filename)
board_docx_name = f"{os.path.splitext(selected_filename)[0]}_evaluation_board.docx"
sess_data["uploaded_documents"][board_docx_name] = result
sess_data["shredded_documents"][board_docx_name] = docx_bytes
return result, board_docx_name, docx_bytes, None, None
else:
return result, None, None, None, None
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, None
rfp_filename = selected_filename
rfp_fileid = sess_data["uploaded_documents_fileid"].get(selected_filename)
if not rfp_fileid and rfp_filename in sess_data["uploaded_documents_bytes"]:
try:
fileid = upload_to_gemini_file(sess_data["uploaded_documents_bytes"][rfp_filename], rfp_filename)
if fileid:
sess_data["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"
result = gemini_generate_content(prompt, file_id=rfp_fileid, chat_input=chat_input)
sess_data["generated_response"] = result
if result and not result.startswith("Error"):
docx_bytes = save_proposal_as_docx(result, rfp_filename)
generated_docx_name = f"{os.path.splitext(rfp_filename)[0]}_proposal.docx"
sess_data["proposals"][generated_docx_name] = result
sess_data["proposals_fileid"][generated_docx_name] = None
return result, None, None, generated_docx_name, docx_bytes
else:
return result, None, None, None, None
elif action == 'recover':
if not selected_proposal_filename or selected_proposal_filename not in sess_data["proposals"]:
logging.error("No proposal document selected for recovery.")
return "No proposal document selected for recovery.", None, None, None, None
if not selected_filename or selected_filename not in sess_data["uploaded_documents"]:
logging.error("No compliance check or shredded requirements document selected for recovery.")
return "No compliance check or shredded requirements document selected for recovery.", None, None, None, None
findings_content = sess_data["uploaded_documents"][selected_filename]
proposal_text = sess_data["proposals"][selected_proposal_filename]
prompt = (
"You are a proposal compliance recovery expert. Use the findings and recommendations table and the original proposal response provided below. "
"Address ONLY those sections of the proposal that have a finding and a recommendation for improvement. "
"Do NOT change sections that are marked as fully compliant or already fully address the requirements. "
"For each section that needs fixing, revise it in the proposal to address the recommendation for compliance or to strengthen the response. "
"Return the full proposal with only the necessary sections revised, and leave all other sections untouched. "
"Do not add any introduction, summary, or comments. Return only the revised proposal in markdown, nothing else.\n"
)
if chat_input:
prompt += f"User additional instructions: {chat_input}\n"
prompt += f"\n---\nFindings and Recommendations Table ({selected_filename}):\n{findings_content}\n"
prompt += f"\n---\nOriginal Proposal Document ({selected_proposal_filename}):\n{proposal_text}\n"
result = gemini_generate_content(prompt, file_id=None, chat_input=chat_input)
if result and not result.startswith("Error"):
base_name = os.path.splitext(selected_proposal_filename)[0]
recovered_docx_name = f"{base_name}_recovered.docx"
docx_bytes = save_proposal_as_docx(result, base_name)
sess_data["proposals"][recovered_docx_name] = result
sess_data["proposals_fileid"][recovered_docx_name] = None
logging.info(f"Recovered proposal generated and saved as {recovered_docx_name}.")
return result, None, None, recovered_docx_name, docx_bytes
else:
logging.error(f"Error in Gemini recover: {result}")
return result, None, None, None, None
elif action == 'loe':
if not selected_proposal_filename or selected_proposal_filename not in sess_data["proposals"]:
logging.warning("No proposal document selected for LOE estimation.")
return "No proposal document selected for LOE estimation.", None, None, None, None
proposal_text = sess_data["proposals"][selected_proposal_filename]
proposal_base_name = os.path.splitext(selected_proposal_filename)[0]
prompt = (
"You are a federal proposal cost and level of effort estimator. "
"Analyze the following proposal response which is structured to map to a government RFP, PWS, or SOW. "
"For each PWS task or requirement referenced in the proposal, estimate the labor categories and a conservative, overestimated number of hours for each labor category required to accomplish that task. "
"Include a management reserve to ensure the estimate is not underestimated. "
"Return ONLY a markdown table (NO comments, NO intro, NO outro, NO summary) with the following columns: "
"| PWS Task Number | Brief Task Description | Labor Category | Estimated Hours (including management reserve) | "
"For each task, if more than one labor category is needed, list each category on its own row under the same task. "
"Be sure to break down by detailed task, not just high level sections. "
"Be detailed and thorough in your estimation and make sure to overestimate hours to ensure sufficient coverage. "
"If the proposal references any assumed task number or section, use it in the table. "
"Return ONLY the markdown table, no other text, no intro, no outro.\n\n"
)
if chat_input:
prompt += f"User additional instructions: {chat_input}\n"
prompt += f"\n---\nProposal Document ({selected_proposal_filename}):\n{proposal_text}\n"
result = gemini_generate_content(prompt, file_id=None, chat_input=chat_input)
if result and not result.startswith("Error"):
loe_docx_name = f"{proposal_base_name}_loe.docx"
sess_data["proposals"][loe_docx_name] = result
sess_data["proposals_fileid"][loe_docx_name] = None
docx_bytes = save_loe_as_docx(result, proposal_base_name)
logging.info(f"LOE generated and saved as {loe_docx_name}")
return result, None, None, loe_docx_name, docx_bytes
else:
return result, None, None, None, None
return "Action not implemented yet.", None, None, None, None
def get_documents_list(docdict, shreddedict):
all_docs = {}
for filename, text in docdict.items():
all_docs[filename] = text
for filename, docx_bytes in shreddedict.items():
if filename not in all_docs:
all_docs[filename] = None
if not all_docs:
return html.Div("No documents uploaded or generated.", style={"wordWrap": "break-word"})
doc_list = []
for filename in all_docs:
truncated = truncate_filename(filename)
if filename.lower().endswith('.docx') and filename in shreddedict:
b64 = base64.b64encode(shreddedict[filename]).decode('utf-8')
mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
else:
content = docdict.get(filename, "")
b64 = base64.b64encode(content.encode('utf-8')).decode('utf-8')
mime = "text/plain"
download_link = html.A(
truncated,
href=f"data:{mime};base64,{b64}",
download=filename,
target="_blank",
style={"wordWrap": "break-word", "marginRight": "10px", "textDecoration": "underline", "maxWidth": "calc(100% - 70px)", "display": "inline-block", "verticalAlign": "middle"}
)
doc_list.append(
dbc.ListGroupItem([
download_link,
dbc.Button("Delete", id={'type': 'delete-doc-btn', 'index': filename, 'group': 'doc'}, 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_proposals_list(proposaldict):
if not proposaldict:
return html.Div("No proposals uploaded or generated.", style={"wordWrap": "break-word"})
doc_list = []
for filename in proposaldict:
truncated = truncate_filename(filename)
file_content = proposaldict[filename]
try:
if filename.lower().endswith('_loe.docx'):
docx_bytes = save_loe_as_docx(file_content, filename)
else:
docx_bytes = save_proposal_as_docx(file_content, filename)
b64 = base64.b64encode(docx_bytes).decode('utf-8')
mime = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
except Exception:
b64 = base64.b64encode(file_content.encode('utf-8')).decode('utf-8')
mime = "text/plain"
download_link = html.A(
truncated,
href=f"data:{mime};base64,{b64}",
download=filename,
target="_blank",
style={"wordWrap": "break-word", "marginRight": "10px", "textDecoration": "underline", "maxWidth": "calc(100% - 70px)", "display": "inline-block", "verticalAlign": "middle"}
)
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='preview-window-state', data='expanded'),
dbc.Row([
dbc.Col([
dbc.Card([
dbc.CardHeader(html.H5("Documents")),
dbc.CardBody([
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.Div(id='documents-list'),
dcc.Dropdown(
id='select-document-dropdown',
options=[],
placeholder="Select a document to work with",
value=None,
style={"marginBottom": "10px"}
),
])
], className="mb-3"),
dbc.Card([
dbc.CardHeader(html.H5("Proposals")),
dbc.CardBody([
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
),
html.Div(id='proposals-list'),
dcc.Dropdown(
id='select-proposal-dropdown',
options=[],
placeholder="Select a proposal document",
value=None,
style={"marginBottom": "10px"}
),
])
], 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-preview-container",
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('output-preview-container', 'style'),
[Input('preview-window-state', 'data')]
)
def update_preview_window_style(state):
if state == "shrunk":
return {
"maxHeight": "8em",
"overflow": "hidden",
"transition": "max-height 0.4s",
"border": "1px solid #ddd",
"borderRadius": "4px"
}
else:
return {
"maxHeight": "none",
"overflow": "visible",
"transition": "max-height 0.4s",
"border": "1px solid #ddd",
"borderRadius": "4px"
}
@app.callback(
Output('output-data-upload', 'children'),
Output('documents-list', 'children'),
Output('select-document-dropdown', 'options'),
Output('select-document-dropdown', 'value'),
Output('proposals-list', 'children'),
Output('select-proposal-dropdown', 'options'),
Output('select-proposal-dropdown', 'value'),
Output('preview-window-state', 'data'),
[
Input('shred-action-btn', 'n_clicks'),
Input('proposal-action-btn', 'n_clicks'),
Input('compliance-action-btn', 'n_clicks'),
Input('recover-action-btn', 'n_clicks'),
Input('board-action-btn', 'n_clicks'),
Input('loe-action-btn', 'n_clicks'),
Input('upload-document', 'contents'),
State('upload-document', 'filename'),
Input({'type': 'delete-doc-btn', 'index': ALL, 'group': 'doc'}, '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'),
State('chat-input', 'value'),
Input('cancel-action-btn', 'n_clicks'),
State('preview-window-state', 'data')
],
prevent_initial_call=True
)
def master_callback(
shred_clicks, proposal_clicks, compliance_clicks, recover_clicks, board_clicks, loe_clicks,
rfp_content, rfp_filename, doc_delete_clicks, selected_doc,
proposal_content, proposal_filename, proposal_delete_clicks, selected_proposal,
chat_input, cancel_clicks, preview_window_state
):
session_id = get_session_id()
sess_data = get_session_data(session_id)
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 []
doc_delete_clicks = safe_get_n_clicks(ctx, 8)
proposal_delete_clicks = safe_get_n_clicks(ctx, 12)
uploaded_rfp_decoded_bytes = None
# Cancel action
if triggered_id == 'cancel-action-btn':
output_data_upload = html.Div("[Cancelled by user]\n", style={"wordWrap": "break-word"})
doc_options = [{'label': truncate_filename(fn), 'value': fn} for fn in sess_data["uploaded_documents"].keys()]
doc_value = selected_doc if selected_doc in sess_data["uploaded_documents"] else (next(iter(sess_data["uploaded_documents"]), None) if sess_data["uploaded_documents"] else None)
proposals_list = get_proposals_list(sess_data["proposals"])
proposal_options = [{'label': truncate_filename(fn), 'value': fn} for fn in sess_data["proposals"].keys()]
proposal_value = selected_proposal if selected_proposal in sess_data["proposals"] else (next(iter(sess_data["proposals"]), None) if sess_data["proposals"] else None)
documents_list = get_documents_list(sess_data["uploaded_documents"], sess_data["shredded_documents"])
return (
output_data_upload,
documents_list, doc_options, doc_value,
proposals_list, proposal_options, proposal_value,
"expanded"
)
# Handle uploads
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:
sess_data["uploaded_documents"][rfp_filename] = text
sess_data["uploaded_documents_bytes"][rfp_filename] = decoded
if fileid:
sess_data["uploaded_documents_fileid"][rfp_filename] = fileid
logging.info(f"[{session_id}] Document uploaded: {rfp_filename}")
else:
logging.error(f"[{session_id}] Failed to decode uploaded document: {rfp_filename}")
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:
sess_data["proposals"][proposal_filename] = text
if fileid:
sess_data["proposals_fileid"][proposal_filename] = fileid
logging.info(f"[{session_id}] Proposal uploaded: {proposal_filename}")
else:
logging.error(f"[{session_id}] Failed to decode uploaded proposal: {proposal_filename}")
# Handle deletes
if triggered_id and isinstance(doc_delete_clicks, list):
for i, n_click in enumerate(doc_delete_clicks):
if n_click:
btn_id = ctx.inputs_list[8][i]['id']
del_filename = btn_id['index']
if del_filename in sess_data["uploaded_documents"]:
del sess_data["uploaded_documents"][del_filename]
if del_filename in sess_data["uploaded_documents_fileid"]:
try:
genai.delete_file(sess_data["uploaded_documents_fileid"][del_filename])
except Exception as e:
logging.warning(f"[{session_id}] Failed to delete Gemini file {del_filename}: {e}")
del sess_data["uploaded_documents_fileid"][del_filename]
if del_filename in sess_data["uploaded_documents_bytes"]:
del sess_data["uploaded_documents_bytes"][del_filename]
logging.info(f"[{session_id}] Document deleted: {del_filename}")
if del_filename in sess_data["shredded_documents"]:
del sess_data["shredded_documents"][del_filename]
logging.info(f"[{session_id}] Shredded doc deleted: {del_filename}")
if selected_doc == del_filename:
selected_doc = None
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[12][i]['id']
del_filename = btn_id['index']
if del_filename in sess_data["proposals"]:
del sess_data["proposals"][del_filename]
if del_filename in sess_data["proposals_fileid"]:
try:
genai.delete_file(sess_data["proposals_fileid"][del_filename])
except Exception as e:
logging.warning(f"[{session_id}] Failed to delete Gemini proposal file {del_filename}: {e}")
del sess_data["proposals_fileid"][del_filename]
logging.info(f"[{session_id}] Proposal deleted: {del_filename}")
if selected_proposal == del_filename:
selected_proposal = None
break
doc_options = [{'label': truncate_filename(fn), 'value': fn} for fn in sess_data["uploaded_documents"].keys()]
doc_value = selected_doc if selected_doc in sess_data["uploaded_documents"] else (next(iter(sess_data["uploaded_documents"]), None) if sess_data["uploaded_documents"] else None)
documents_list = get_documents_list(sess_data["uploaded_documents"], sess_data["shredded_documents"])
proposals_list = get_proposals_list(sess_data["proposals"])
proposal_options = [{'label': truncate_filename(fn), 'value': fn} for fn in sess_data["proposals"].keys()]
proposal_value = selected_proposal if selected_proposal in sess_data["proposals"] else (next(iter(sess_data["proposals"]), None) if sess_data["proposals"] else None)
output_data_upload = html.Div("No action taken yet.", style={"wordWrap": "break-word"})
action_btns = [
'shred-action-btn', 'proposal-action-btn', 'compliance-action-btn',
'recover-action-btn', 'board-action-btn', 'loe-action-btn'
]
if triggered_id in action_btns:
got_lock = sess_data["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 (
output_data_upload,
documents_list, doc_options, doc_value,
proposals_list, proposal_options, proposal_value,
"expanded"
)
try:
if triggered_id == "shred-action-btn":
action_name = "shred"
result, generated_filename, generated_docx_bytes, _, _ = process_document(sess_data, action_name, doc_value, chat_input, uploaded_rfp_decoded_bytes, None)
output_data_upload = dcc.Markdown(result, style={"whiteSpace": "pre-wrap", "wordWrap": "break-word"})
elif triggered_id == "compliance-action-btn":
action_name = "compliance"
result, generated_filename, generated_docx_bytes, _, _ = process_document(
sess_data, action_name, doc_value, chat_input, uploaded_rfp_decoded_bytes, proposal_value
)
output_data_upload = dcc.Markdown(result, style={"whiteSpace": "pre-wrap", "wordWrap": "break-word"})
elif triggered_id == "board-action-btn":
action_name = "virtual_board"
result, generated_filename, generated_docx_bytes, _, _ = process_document(
sess_data, action_name, doc_value, chat_input, uploaded_rfp_decoded_bytes, proposal_value
)
output_data_upload = dcc.Markdown(result, style={"whiteSpace": "pre-wrap", "wordWrap": "break-word"})
elif triggered_id == "proposal-action-btn":
action_name = "proposal"
selected_bytes = sess_data["uploaded_documents_bytes"].get(doc_value, None)
result, _, _, generated_filename, generated_docx_bytes = process_document(
sess_data, action_name, doc_value, chat_input, selected_bytes, None
)
output_data_upload = dcc.Markdown(result, style={"whiteSpace": "pre-wrap", "wordWrap": "break-word"})
elif triggered_id == "recover-action-btn":
action_name = "recover"
result, _, _, generated_filename, generated_docx_bytes = process_document(
sess_data, action_name, doc_value, chat_input, None, proposal_value
)
output_data_upload = dcc.Markdown(result, style={"whiteSpace": "pre-wrap", "wordWrap": "break-word"})
elif triggered_id == "loe-action-btn":
action_name = "loe"
result, _, _, generated_filename, generated_docx_bytes = process_document(
sess_data, action_name, None, chat_input, None, proposal_value
)
output_data_upload = dcc.Markdown(result, style={"whiteSpace": "pre-wrap", "wordWrap": "break-word"})
finally:
sess_data["gemini_lock"].release()
doc_options = [{'label': truncate_filename(fn), 'value': fn} for fn in sess_data["uploaded_documents"].keys()]
doc_value = doc_value if doc_value in sess_data["uploaded_documents"] else (next(iter(sess_data["uploaded_documents"]), None) if sess_data["uploaded_documents"] else None)
proposal_options = [{'label': truncate_filename(fn), 'value': fn} for fn in sess_data["proposals"].keys()]
proposal_value = proposal_value if proposal_value in sess_data["proposals"] else (next(iter(sess_data["proposals"]), None) if sess_data["proposals"] else None)
documents_list = get_documents_list(sess_data["uploaded_documents"], sess_data["shredded_documents"])
proposals_list = get_proposals_list(sess_data["proposals"])
return (
output_data_upload,
documents_list, doc_options, doc_value,
proposals_list, proposal_options, proposal_value,
"shrunk"
)
doc_value = doc_value if doc_value in sess_data["uploaded_documents"] else (next(iter(sess_data["uploaded_documents"]), None) if sess_data["uploaded_documents"] else None)
proposal_value = proposal_value if proposal_value in sess_data["proposals"] else (next(iter(sess_data["proposals"]), None) if sess_data["proposals"] else None)
return (
output_data_upload,
documents_list, doc_options, doc_value,
proposals_list, proposal_options, proposal_value,
"expanded"
)
@app.callback(
Output('documents-list', 'children'),
Output('select-document-dropdown', 'options'),
Output('select-document-dropdown', 'value'),
Output('proposals-list', 'children'),
Output('select-proposal-dropdown', 'options'),
Output('select-proposal-dropdown', 'value'),
Input('output-data-upload', 'children')
)
def update_lists_on_output(children):
session_id = get_session_id()
sess_data = get_session_data(session_id)
documents_list = get_documents_list(sess_data["uploaded_documents"], sess_data["shredded_documents"])
doc_options = [{'label': truncate_filename(fn), 'value': fn} for fn in sess_data["uploaded_documents"].keys()]
doc_value = next(iter(sess_data["uploaded_documents"]), None) if sess_data["uploaded_documents"] else None
proposals_list = get_proposals_list(sess_data["proposals"])
proposal_options = [{'label': truncate_filename(fn), 'value': fn} for fn in sess_data["proposals"].keys()]
proposal_value = next(iter(sess_data["proposals"]), None) if sess_data["proposals"] else None
return documents_list, doc_options, doc_value, proposals_list, proposal_options, proposal_value
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.")