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
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
import dash
from dash import dcc, html, Input, Output, State, callback_context, no_update
import dash_bootstrap_components as dbc
import logging
import threading
import os
import base64
import io
import uuid
import time
from flask import Flask

# Anthropic API stub (replace with real implementation)
import requests

# For allowed file types and basic file handling
ALLOWED_EXTENSIONS = ('pdf', 'doc', 'docx', 'txt')

# Logging configuration
logging.basicConfig(
    format="%(asctime)s %(levelname)s:%(message)s",
    level=logging.INFO
)
logger = logging.getLogger(__name__)

# In-memory storage for uploaded documents and results
uploaded_documents = {}
generated_content = {}

# Anthropic API endpoint/config stub
ANTHROPIC_API_URL = "https://api.anthropic.com/v1/messages"
ANTHROPIC_API_KEY = os.getenv("ANTHROPIC_API_KEY", "YOUR_ANTHROPIC_API_KEY")  # Replace as appropriate

# Flask server for Dash
server = Flask(__name__)

# CUDA GPU configuration (for completeness, if used by downstream libraries)
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

# External stylesheets
external_stylesheets = [dbc.themes.BOOTSTRAP]

# Dash app initialization
app = dash.Dash(
    __name__,
    server=server,
    external_stylesheets=external_stylesheets,
    suppress_callback_exceptions=True,
    title="Proposal Writing Assistant"
)

# Helper functions
def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

def save_uploaded_file(file_content, filename):
    doc_id = str(uuid.uuid4())
    uploaded_documents[doc_id] = {
        "filename": filename,
        "content": file_content
    }
    logger.info(f"Uploaded document saved: {filename} with id {doc_id}")
    return doc_id

def anthropic_api_call(prompt, files=None, task_type=None, extra_instructions=""):
    logger.info(f"Calling Anthropic API for task: {task_type}")
    # Stubbed implementation; replace with actual Anthropic API call
    headers = {
        "x-api-key": ANTHROPIC_API_KEY,
        "content-type": "application/json"
    }
    data = {
        "model": "claude-3-opus-20240229",
        "messages": [
            {"role": "user", "content": prompt + "\n" + extra_instructions}
        ],
        "max_tokens": 4096,
        "temperature": 0.2
    }
    try:
        # response = requests.post(ANTHROPIC_API_URL, headers=headers, json=data, timeout=120)
        # result = response.json().get('content', ['[Anthropic response placeholder]'])[0]
        # Simulated response:
        time.sleep(2)
        result = f"[Simulated response for {task_type}]"
        logger.info(f"Anthropic API success for task: {task_type}")
        return result
    except Exception as e:
        logger.error(f"Anthropic API error: {str(e)}")
        return f"Error: {str(e)}"

def parse_contents(contents, filename):
    content_type, content_string = contents.split(',')
    decoded = base64.b64decode(content_string)
    # Only display first few characters for preview
    try:
        if filename.lower().endswith('.txt'):
            preview = decoded.decode('utf-8')[:2048]
        else:
            preview = f"Preview not available for {filename}"
        return preview
    except Exception as e:
        logger.error(f"Could not decode file {filename}: {e}")
        return f"Error decoding {filename}"

# UI Elements
def navbar():
    return dbc.Card(
        [
            dbc.Nav(
                [
                    dbc.Button("Shred RFP/PWS/SOW/RFI", id="btn-shred", className="mb-2 btn-primary", style={"width": "100%"}),
                    dbc.Button("Generate Proposal Response", id="btn-generate", className="mb-2 btn-secondary", style={"width": "100%"}),
                    dbc.Button("Check Compliance", id="btn-compliance", className="mb-2 btn-tertiary", style={"width": "100%"}),
                    dbc.Button("Recover Document", id="btn-recover", className="mb-2 btn-primary", style={"width": "100%"}),
                    dbc.Button("Virtual Board", id="btn-virtual-board", className="mb-2 btn-secondary", style={"width": "100%"}),
                    dbc.Button("Estimate LOE", id="btn-loe", className="mb-2 btn-tertiary", style={"width": "100%"}),
                ],
                vertical=True,
                pills=False
            ),
            html.Hr(),
            html.Div(
                [
                    html.H6("Uploaded Documents"),
                    html.Ul(
                        id="uploaded-doc-list",
                        style={"listStyleType": "none", "paddingLeft": "0"}
                    ),
                ]
            ),
        ],
        body=True
    )

def chat_window():
    return dbc.Card(
        [
            html.Div(
                [
                    html.Div(id="chat-history", style={"height": "160px", "overflowY": "auto", "padding": "0.5rem"}),
                    dbc.InputGroup(
                        [
                            dbc.Textarea(id="chat-input", placeholder="Send additional instructions...", style={"resize":"vertical", "wordWrap":"break-word", "width": "100%", "height": "60px"}),
                            dbc.Button("Send", id="btn-send-chat", className="btn-secondary", n_clicks=0),
                        ],
                        className="mt-2"
                    ),
                ]
            ),
        ],
        body=True,
        style={"marginBottom": "10px"}
    )

def top_action_buttons():
    return html.Div(
        [
            dbc.Button("Shred", id="action-shred", className="me-2 btn-primary", n_clicks=0, style={"minWidth": "120px"}),
            dbc.Button("Generate Response", id="action-generate", className="me-2 btn-secondary", n_clicks=0, style={"minWidth": "180px"}),
            dbc.Button("Check Compliance", id="action-compliance", className="me-2 btn-tertiary", n_clicks=0, style={"minWidth": "160px"}),
            dbc.Button("Recover", id="action-recover", className="me-2 btn-primary", n_clicks=0, style={"minWidth": "120px"}),
            dbc.Button("Virtual Board", id="action-virtual-board", className="me-2 btn-secondary", n_clicks=0, style={"minWidth": "160px"}),
            dbc.Button("Estimate LOE", id="action-loe", className="btn-tertiary", n_clicks=0, style={"minWidth": "140px"}),
        ],
        className="mb-3",
        style={"display": "flex", "flexWrap": "wrap"}
    )

def upload_area():
    return html.Div(
        [
            dcc.Upload(
                id="upload-document",
                children=html.Div(["Drag & drop or click to select a file."]),
                multiple=False,
                style={
                    "width": "100%",
                    "height": "70px",
                    "lineHeight": "70px",
                    "borderWidth": "1px",
                    "borderStyle": "dashed",
                    "borderRadius": "4px",
                    "textAlign": "center",
                    "marginBottom": "8px"
                }
            ),
            html.Div(id="upload-feedback")
        ]
    )

def preview_area():
    return dbc.Card(
        [
            html.H6("Document Preview / Output"),
            html.Pre(id="preview-content", style={"whiteSpace": "pre-wrap", "wordWrap": "break-word", "maxHeight": "340px", "overflowY": "auto"})
        ],
        body=True
    )

def main_layout():
    return dbc.Container(
        [
            dbc.Row(
                [
                    dbc.Col(
                        html.H2("Proposal Writing Assistant", style={"margin": "12px 0"}),
                        width=12
                    ),
                ],
                align="center",
                style={"marginBottom": "8px"}
            ),
            dbc.Row(
                [
                    dbc.Col(
                        navbar(),
                        width=3,
                        style={"minWidth": "220px", "maxWidth": "400px"}
                    ),
                    dbc.Col(
                        dbc.Card(
                            [
                                chat_window(),
                                top_action_buttons(),
                                upload_area(),
                                preview_area(),
                                dcc.Loading(
                                    id="loading",
                                    type="default",
                                    children=html.Div(id="loading-output"),
                                    style={"position": "absolute", "top": "6px", "left": "50%"}
                                ),
                            ],
                            body=True
                        ),
                        width=9
                    ),
                ],
                style={"minHeight": "90vh"}
            ),
        ],
        fluid=True
    )

app.layout = main_layout

# Callbacks
@app.callback(
    Output("uploaded-doc-list", "children"),
    Output("preview-content", "children"),
    Output("upload-feedback", "children"),
    Output("loading-output", "children"),
    Output("chat-history", "children"),
    [
        Input("upload-document", "contents"),
        Input("action-shred", "n_clicks"),
        Input("action-generate", "n_clicks"),
        Input("action-compliance", "n_clicks"),
        Input("action-recover", "n_clicks"),
        Input("action-virtual-board", "n_clicks"),
        Input("action-loe", "n_clicks"),
        Input("btn-send-chat", "n_clicks"),
        Input({"type": "delete-doc-btn", "index": dash.ALL}, "n_clicks"),
    ],
    [
        State("upload-document", "filename"),
        State("chat-input", "value"),
        State("chat-history", "children"),
        State("preview-content", "children"),
        State("uploaded-doc-list", "children"),
    ],
    prevent_initial_call=True
)
def main_callback(
    upload_contents, shred, generate, compliance, recover, virtual_board, loe, send_chat, delete_doc_clicks,
    upload_filename, chat_input, chat_history, preview_content, uploaded_doc_list
):
    triggered_id = callback_context.triggered[0]["prop_id"].split(".")[0] if callback_context.triggered else None
    logger.info(f"Triggered callback: {triggered_id}")

    feedback = no_update
    loading_message = ""
    new_preview_content = no_update
    new_chat_history = chat_history if chat_history else []
    doc_list_items = []

    # Handle file upload
    if triggered_id == "upload-document" and upload_contents and upload_filename:
        if not allowed_file(upload_filename):
            feedback = dbc.Alert("Unsupported file type. Please upload PDF, Word, or TXT.", color="danger", dismissable=True)
        else:
            doc_id = save_uploaded_file(upload_contents, upload_filename)
            preview = parse_contents(upload_contents, upload_filename)
            new_preview_content = f"{upload_filename}:\n\n{preview}"
            feedback = dbc.Alert(f"Uploaded {upload_filename}", color="success", dismissable=True)
            logger.info(f"File uploaded: {upload_filename}")

    # Update doc list
    for doc_id, doc in uploaded_documents.items():
        doc_list_items.append(
            html.Li(
                [
                    html.Span(doc['filename'], style={"marginRight": "8px"}),
                    dbc.Button("Delete", id={"type": "delete-doc-btn", "index": doc_id}, color="danger", size="sm", n_clicks=0)
                ],
                style={"display": "flex", "justifyContent": "space-between", "alignItems": "center", "marginBottom": "5px"}
            )
        )

    # Handle document deletion
    if isinstance(delete_doc_clicks, list) and any(delete_doc_clicks):
        idx = delete_doc_clicks.index(max(delete_doc_clicks))
        doc_ids = list(uploaded_documents.keys())
        if idx < len(doc_ids):
            deleted_doc = uploaded_documents.pop(doc_ids[idx])
            feedback = dbc.Alert(f"Deleted {deleted_doc['filename']}", color="info", dismissable=True)
            logger.info(f"Document deleted: {deleted_doc['filename']}")
            doc_list_items = [
                html.Li(
                    [
                        html.Span(doc['filename'], style={"marginRight": "8px"}),
                        dbc.Button("Delete", id={"type": "delete-doc-btn", "index": doc_id}, color="danger", size="sm", n_clicks=0)
                    ],
                    style={"display": "flex", "justifyContent": "space-between", "alignItems": "center", "marginBottom": "5px"}
                )
                for doc_id, doc in uploaded_documents.items()
            ]
            new_preview_content = "" if not uploaded_documents else no_update

    # If no uploaded documents, block actions
    if len(uploaded_documents) == 0 and triggered_id not in ["upload-document", "btn-send-chat"]:
        feedback = dbc.Alert("Please upload a document before performing actions.", color="warning", dismissable=True)
        logger.warning("Attempted action without documents.")
        return doc_list_items, new_preview_content, feedback, loading_message, new_chat_history

    # Handle chat
    if triggered_id == "btn-send-chat" and chat_input and chat_input.strip():
        if not isinstance(new_chat_history, list):
            new_chat_history = []
        new_chat_history.append(html.Div([
            html.Strong("You: "), html.Span(chat_input)
        ], style={"marginBottom": "0.25rem"}))
        feedback = dbc.Alert("Chat message sent. Instructions will be used in next action.", color="info", dismissable=True)
        logger.info(f"Chat message sent: {chat_input}")
        # Clear chat input (handled client-side)

    # Identify last chat instructions to use
    last_chat = ""
    if isinstance(new_chat_history, list) and new_chat_history:
        for item in reversed(new_chat_history):
            if isinstance(item, html.Div):
                children = item.children
                if len(children) > 1 and isinstance(children[1], html.Span):
                    last_chat = children[1].children
                    break
    elif isinstance(chat_input, str):
        last_chat = chat_input

    # Action buttons: always require at least one document
    if triggered_id in ["action-shred", "action-generate", "action-compliance", "action-recover", "action-virtual-board", "action-loe"]:
        loading_message = dbc.Alert("Processing request, please wait...", color="primary", dismissable=False, style={"textAlign": "center"})
        doc_id, doc = next(iter(uploaded_documents.items()))
        file_name = doc['filename']
        file_content = doc['content']
        action_type = triggered_id.replace("action-", "").replace("-", " ").title()

        # Anthropic API call in a thread
        result_holder = {}

        def threaded_api_call():
            if triggered_id == "action-shred":
                prompt = (
                    "Shred this document into requirements, organized by section. "
                    "Identify requirements by action words (shall, will, perform, etc). "
                    "Output as spreadsheet: PWS Section, Requirement."
                )
                task_type = "Shred"
            elif triggered_id == "action-generate":
                prompt = (
                    "Generate a detailed proposal response, organized by section/subsection. "
                    "Focus on approach, steps, workflow, people, processes, technology. "
                    "Include research validation and citations. Address Red Review findings."
                )
                task_type = "Generate Proposal Response"
            elif triggered_id == "action-compliance":
                prompt = (
                    "Check compliance of the proposal response against the shredded requirements. "
                    "Produce a spreadsheet: PWS number, requirement, finding, recommendation."
                )
                task_type = "Check Compliance"
            elif triggered_id == "action-recover":
                prompt = (
                    "Using the compliance spreadsheet, improve the document sections. "
                    "Address recommendations without materially changing content. "
                    "Organize improvements by PWS section headers/subheaders."
                )
                task_type = "Recover Document"
            elif triggered_id == "action-virtual-board":
                prompt = (
                    "Evaluate the proposal based on requirements and evaluation criteria. "
                    "Generate a section-by-section evaluation spreadsheet using ratings: "
                    "unsatisfactory, satisfactory, good, very good, excellent. Include explanations. "
                    "Base evaluation on sections L and M."
                )
                task_type = "Virtual Board"
            elif triggered_id == "action-loe":
                prompt = (
                    "Estimate Level of Effort for the proposal. Output spreadsheet: "
                    "PWS task area, brief description, labor categories, estimated hours per category."
                )
                task_type = "Estimate LOE"
            else:
                prompt = ""
                task_type = "Unknown"
            result_holder["result"] = anthropic_api_call(prompt, files=[file_content], task_type=task_type, extra_instructions=last_chat or "")

        thread = threading.Thread(target=threaded_api_call)
        thread.start()
        thread.join()

        result = result_holder.get("result", "[No result]")
        generated_content[triggered_id] = result
        new_preview_content = f"{action_type} Output for {file_name}:\n\n{result}"
        feedback = dbc.Alert(f"{action_type} completed.", color="success", dismissable=True)
        logger.info(f"{action_type} completed for {file_name}")

    return doc_list_items, new_preview_content, feedback, loading_message, new_chat_history

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.")