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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# app.py — Kashmir AO Action Plan for Inf (Full Version)
|
| 2 |
+
import os
|
| 3 |
+
import gradio as gr
|
| 4 |
+
from openai import OpenAI
|
| 5 |
+
|
| 6 |
+
# ---------------------------
|
| 7 |
+
# Setup OpenAI
|
| 8 |
+
# ---------------------------
|
| 9 |
+
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
|
| 10 |
+
|
| 11 |
+
# ---------------------------
|
| 12 |
+
# Knowledge Base placeholder
|
| 13 |
+
# ---------------------------
|
| 14 |
+
KNOWLEDGE_BASE = """
|
| 15 |
+
Embedded doctrinal and operational notes for Kashmir AO.
|
| 16 |
+
"""
|
| 17 |
+
|
| 18 |
+
# ---------------------------
|
| 19 |
+
# Helper functions
|
| 20 |
+
# ---------------------------
|
| 21 |
+
def generate_standard_report(answers):
|
| 22 |
+
"""Generate standard analyst report from answers + KB context."""
|
| 23 |
+
text_blob = "\n".join(f"{q}: {a}" for q, a in answers.items())
|
| 24 |
+
prompt = f"""You are a military analyst. Based on the following answers and knowledge base, write a structured, advisory style report for the commander.
|
| 25 |
+
Knowledge Base:
|
| 26 |
+
{KNOWLEDGE_BASE}
|
| 27 |
+
|
| 28 |
+
Answers:
|
| 29 |
+
{text_blob}
|
| 30 |
+
|
| 31 |
+
Report:"""
|
| 32 |
+
try:
|
| 33 |
+
resp = client.chat.completions.create(
|
| 34 |
+
model="gpt-4o-mini",
|
| 35 |
+
messages=[{"role": "user", "content": prompt}],
|
| 36 |
+
max_tokens=500
|
| 37 |
+
)
|
| 38 |
+
return resp.choices[0].message.content.strip()
|
| 39 |
+
except Exception as e:
|
| 40 |
+
return f"[Error generating report: {e}]"
|
| 41 |
+
|
| 42 |
+
def generate_enhanced_report(answers_a, answers_b, answers_c, gate):
|
| 43 |
+
"""Generate enhanced analyst report (sectioned)."""
|
| 44 |
+
text_blob = "\n".join(
|
| 45 |
+
f"Section A: {answers_a}\nSection B: {answers_b}\nSection C: {answers_c}\nGate: {gate}"
|
| 46 |
+
)
|
| 47 |
+
prompt = f"""You are a senior staff officer. Based on the following sectioned answers and doctrinal context, prepare an advisory intelligence report for the commander.
|
| 48 |
+
|
| 49 |
+
Knowledge Base:
|
| 50 |
+
{KNOWLEDGE_BASE}
|
| 51 |
+
|
| 52 |
+
Answers:
|
| 53 |
+
{text_blob}
|
| 54 |
+
|
| 55 |
+
Report:"""
|
| 56 |
+
try:
|
| 57 |
+
resp = client.chat.completions.create(
|
| 58 |
+
model="gpt-4o-mini",
|
| 59 |
+
messages=[{"role": "user", "content": prompt}],
|
| 60 |
+
max_tokens=600
|
| 61 |
+
)
|
| 62 |
+
return resp.choices[0].message.content.strip()
|
| 63 |
+
except Exception as e:
|
| 64 |
+
return f"[Error generating enhanced report: {e}]"
|
| 65 |
+
|
| 66 |
+
# ---------------------------
|
| 67 |
+
# Threat Status Evaluator
|
| 68 |
+
# ---------------------------
|
| 69 |
+
def derive_threat_status(answers):
|
| 70 |
+
"""Simple heuristic threat evaluation."""
|
| 71 |
+
status = "Green"
|
| 72 |
+
desc = "Prepared for peacetime security; no immediate or future attack indicators."
|
| 73 |
+
text_blob = " ".join(str(v).lower() for v in answers.values())
|
| 74 |
+
if any(word in text_blob for word in ["engaged", "shooting", "contact", "attack now", "ambush"]):
|
| 75 |
+
status = "Red"
|
| 76 |
+
desc = "Ready for ongoing attack — hostile activity confirmed."
|
| 77 |
+
elif any(word in text_blob for word in ["imminent", "soon", "moving towards", "likely attack", "advance"]):
|
| 78 |
+
status = "Orange"
|
| 79 |
+
desc = "Prepared for imminent attack — strong indicators from inputs."
|
| 80 |
+
elif any(word in text_blob for word in ["possible", "expected", "threat", "risk", "movement detected"]):
|
| 81 |
+
status = "Blue"
|
| 82 |
+
desc = "Ready for anticipated short-term attack probability."
|
| 83 |
+
return status, desc
|
| 84 |
+
|
| 85 |
+
# ---------------------------
|
| 86 |
+
# Doctrinal summary constant
|
| 87 |
+
# ---------------------------
|
| 88 |
+
DOCTRINAL_SUMMARY_DETAILED = """
|
| 89 |
+
# 📘 Doctrinal Summary (Sanitized, Detailed)
|
| 90 |
+
|
| 91 |
+
Purpose and Approach
|
| 92 |
+
- Intelligence is treated as a combat function, not just support. Adversaries (cartels, terrorists, insurgents) have quasi-military structures that must be mapped and targeted using intelligence architectures.
|
| 93 |
+
- Objective: Give the commander a persistent, adaptive situational picture by embedding Command, Control, Communications, Intelligence, Surveillance, Reconnaissance (C3ISR) across the AO.
|
| 94 |
+
- Scope: Covers architecture, zoning, collection posture, force protection, staff planning, and doctrinal lenses. No tactical timings or unit counts are provided here.
|
| 95 |
+
|
| 96 |
+
Doctrinal Lenses
|
| 97 |
+
- Threat vs. Situational Awareness: Distinguish enemy threat indicators from the commander’s broader situational awareness.
|
| 98 |
+
- The 5Ds (Detect, Deter, Deny, Deliver, Destroy): Each lens shifts how intelligence and reconnaissance assets should be tasked.
|
| 99 |
+
- Red Teaming & Vulnerability Analysis: Commanders must ask not only what is seen but why something was *not* seen.
|
| 100 |
+
|
| 101 |
+
Command & Control Embedding
|
| 102 |
+
- Rear Area: Secure HQ and sustainment.
|
| 103 |
+
- Forward Areas: Distributed tactical nodes with reconnaissance, HUMINT, and CI integration.
|
| 104 |
+
- Deep Forward: Covert placements close to adversary leadership or logistics.
|
| 105 |
+
- Tactical Ops Centers (TOCs): Covert urban/rural apartments acting as intelligence fusion and relay nodes.
|
| 106 |
+
|
| 107 |
+
Force Protection
|
| 108 |
+
- Randomize movement, guard routines, and convoy timings.
|
| 109 |
+
- Use counterintelligence not only to detect spies but also to map and attack enemy intent and surveillance.
|
| 110 |
+
|
| 111 |
+
Collection Integration
|
| 112 |
+
- HUMINT, TACHUMINT, Reconnaissance, Surveillance, and CI nodes all feed Tactical Ops Centers.
|
| 113 |
+
- SIIE (Structured Integrated Intelligence Environment) ensures commanders see a fused picture, not fragments.
|
| 114 |
+
|
| 115 |
+
Staff Guidance
|
| 116 |
+
- Align staff planning with commander’s intent *and* with adversary threat reality.
|
| 117 |
+
- Avoid overreliance on external MI units; embed unit-level intelligence.
|
| 118 |
+
- Differentiate between Advance Warning, Surprise, and Situational Awareness — they are not the same.
|
| 119 |
+
|
| 120 |
+
Outcome
|
| 121 |
+
- Commanders who apply these doctrines convert intelligence from passive reporting into an active weapon system.
|
| 122 |
+
- Security forces must think of intelligence as combat, structuring AO into zones, embedding covert/ overt nodes, and preempting threats before they manifest.
|
| 123 |
+
"""
|
| 124 |
+
|
| 125 |
+
# ---------------------------
|
| 126 |
+
# Build Gradio UI
|
| 127 |
+
# ---------------------------
|
| 128 |
+
with gr.Blocks() as demo:
|
| 129 |
+
gr.HTML('<img src="https://huggingface.co/spaces/Militaryint/ops/resolve/main/banner.png" width="100%">')
|
| 130 |
+
gr.Markdown("# Kashmir AO Action Plan for Inf — ARMY Analyst Tool & Battle Planner")
|
| 131 |
+
|
| 132 |
+
# ----------- Standard Analyst Tab -----------
|
| 133 |
+
with gr.Tab("Standard Analyst"):
|
| 134 |
+
std_questions = [
|
| 135 |
+
"When/where was the enemy sighted?",
|
| 136 |
+
"Direction of movement?",
|
| 137 |
+
"Enemy size and strength?",
|
| 138 |
+
"Weapons and equipment observed?",
|
| 139 |
+
"Vehicles or on foot?",
|
| 140 |
+
"Proximity to roads?",
|
| 141 |
+
"Proximity to camps?",
|
| 142 |
+
"Support of locals?",
|
| 143 |
+
"Enemy formation/disposition?",
|
| 144 |
+
"Terrain type?"
|
| 145 |
+
]
|
| 146 |
+
std_inputs = [gr.Textbox(label=q) for q in std_questions]
|
| 147 |
+
std_btn = gr.Button("Generate Standard Report")
|
| 148 |
+
std_output = gr.Markdown()
|
| 149 |
+
def on_std_click(*answers):
|
| 150 |
+
return generate_standard_report(dict(zip(std_questions, answers)))
|
| 151 |
+
std_btn.click(on_std_click, inputs=std_inputs, outputs=std_output)
|
| 152 |
+
|
| 153 |
+
# ----------- Enhanced Analyst Tab -----------
|
| 154 |
+
with gr.Tab("Enhanced Analyst (Sectioned)"):
|
| 155 |
+
ENH_SECTION_A = [
|
| 156 |
+
"Does the unit have Ops + Int integration?",
|
| 157 |
+
"Does the unit have intelligence SOPs?",
|
| 158 |
+
"Reconnaissance & Surveillance plan integrated?",
|
| 159 |
+
"Force Protection SOP & threat conditions?",
|
| 160 |
+
"Forward intelligence projection capability?"
|
| 161 |
+
]
|
| 162 |
+
ENH_SECTION_B = [
|
| 163 |
+
"Vulnerability analysis tools in place?",
|
| 164 |
+
"Randomness in troop/officer movement?",
|
| 165 |
+
"Source vetting & counterintelligence registry?",
|
| 166 |
+
"Intelligence treated as doctrine repository?",
|
| 167 |
+
"CI used in base/ops vulnerability analysis?"
|
| 168 |
+
]
|
| 169 |
+
ENH_SECTION_C = [
|
| 170 |
+
"Embedded intelligence in routine ops?",
|
| 171 |
+
"Advance warning system present?",
|
| 172 |
+
"Capability for deliberate vs quick ops?",
|
| 173 |
+
"Projected command/control nodes?",
|
| 174 |
+
"Distinction between SA, Advance Warn, Surprise?"
|
| 175 |
+
]
|
| 176 |
+
a_inputs = [gr.Textbox(label=q) for q in ENH_SECTION_A]
|
| 177 |
+
b_inputs = [gr.Textbox(label=q) for q in ENH_SECTION_B]
|
| 178 |
+
c_inputs = [gr.Textbox(label=q) for q in ENH_SECTION_C]
|
| 179 |
+
gate_q = gr.Textbox(label="Does the commander want AI to supply the report?")
|
| 180 |
+
enh_btn = gr.Button("Generate Enhanced Report")
|
| 181 |
+
enh_output = gr.Markdown()
|
| 182 |
+
def on_enh_click(*args):
|
| 183 |
+
answers_a = dict(zip(ENH_SECTION_A, args[:len(ENH_SECTION_A)]))
|
| 184 |
+
answers_b = dict(zip(ENH_SECTION_B, args[len(ENH_SECTION_A):len(ENH_SECTION_A)+len(ENH_SECTION_B)]))
|
| 185 |
+
answers_c = dict(zip(ENH_SECTION_C, args[len(ENH_SECTION_A)+len(ENH_SECTION_B):-1]))
|
| 186 |
+
gate = args[-1]
|
| 187 |
+
return generate_enhanced_report(answers_a, answers_b, answers_c, gate)
|
| 188 |
+
enh_btn.click(on_enh_click, inputs=a_inputs+b_inputs+c_inputs+[gate_q], outputs=enh_output)
|
| 189 |
+
|
| 190 |
+
# ----------- Doctrinal Summary Tab -----------
|
| 191 |
+
with gr.Tab("Doctrinal Summary (Sanitized, Detailed)"):
|
| 192 |
+
gr.Markdown(DOCTRINAL_SUMMARY_DETAILED)
|
| 193 |
+
|
| 194 |
+
# ----------- Threat Status Tab (NEW) -----------
|
| 195 |
+
with gr.Tab("Threat Status"):
|
| 196 |
+
gr.Markdown("### 🛡️ Threat Status Indicator\nColors indicate readiness levels derived from your inputs in Standard/Enhanced tabs.")
|
| 197 |
+
status_out = gr.HTML("<div style='font-size:20px;'>Awaiting evaluation...</div>")
|
| 198 |
+
eval_btn = gr.Button("Evaluate Threat Status")
|
| 199 |
+
def on_eval():
|
| 200 |
+
# Placeholder, since live state wiring not connected
|
| 201 |
+
dummy_answers = {"sample": "no threat"}
|
| 202 |
+
status, desc = derive_threat_status(dummy_answers)
|
| 203 |
+
color_map = {"Red":"#FF4C4C","Orange":"#FFA500","Blue":"#1E90FF","Green":"#32CD32"}
|
| 204 |
+
html = f"<div style='padding:1em;border-radius:8px;background:{color_map[status]};color:white;font-size:20px;'><b>{status}</b>: {desc}</div>"
|
| 205 |
+
return html
|
| 206 |
+
eval_btn.click(on_eval, inputs=None, outputs=status_out)
|
| 207 |
+
|
| 208 |
+
# ---------------------------
|
| 209 |
+
# Launch app
|
| 210 |
+
# ---------------------------
|
| 211 |
+
if __name__ == "__main__":
|
| 212 |
+
demo.launch()
|