Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import faiss
|
| 3 |
+
import pickle
|
| 4 |
+
import numpy as np
|
| 5 |
+
import streamlit as st
|
| 6 |
+
from sentence_transformers import SentenceTransformer
|
| 7 |
+
from groq import Groq
|
| 8 |
+
|
| 9 |
+
# ==== Setup ====
|
| 10 |
+
st.set_page_config(page_title="πͺ US Army RAG Assistant", layout="wide")
|
| 11 |
+
|
| 12 |
+
# ==== Connect to Groq ====
|
| 13 |
+
groq_api_key = os.environ.get("api_key5")
|
| 14 |
+
client = Groq(api_key=groq_api_key)
|
| 15 |
+
|
| 16 |
+
# ==== Load FAISS index and metadata ====
|
| 17 |
+
index = faiss.read_index("army_qa_faiss.index")
|
| 18 |
+
with open("army_qa_metadata.pkl", "rb") as f:
|
| 19 |
+
metadata = pickle.load(f)
|
| 20 |
+
|
| 21 |
+
encoder = SentenceTransformer("all-MiniLM-L6-v2")
|
| 22 |
+
|
| 23 |
+
# ==== RAG Function ====
|
| 24 |
+
def rag_query(user_query, top_k=5):
|
| 25 |
+
query_embedding = encoder.encode([user_query])
|
| 26 |
+
query_embedding = np.array(query_embedding).astype("float32")
|
| 27 |
+
|
| 28 |
+
distances, indices = index.search(query_embedding, top_k)
|
| 29 |
+
|
| 30 |
+
context_chunks = []
|
| 31 |
+
for i in indices[0]:
|
| 32 |
+
if 0 <= i < len(metadata):
|
| 33 |
+
row = metadata[i]
|
| 34 |
+
question = row["question"]
|
| 35 |
+
context_chunks.append(f"Q: {question}\nA: [answer from database]")
|
| 36 |
+
|
| 37 |
+
context = "\n---\n".join(context_chunks)
|
| 38 |
+
|
| 39 |
+
prompt = f"""You are a highly trained assistant with access to US Army Field Manual knowledge.
|
| 40 |
+
Use the context below to answer the user's question.
|
| 41 |
+
|
| 42 |
+
Context:
|
| 43 |
+
{context}
|
| 44 |
+
|
| 45 |
+
User Question:
|
| 46 |
+
{user_query}
|
| 47 |
+
|
| 48 |
+
Answer:"""
|
| 49 |
+
|
| 50 |
+
response = client.chat.completions.create(
|
| 51 |
+
model="deepseek-r1-distill-llama-70b",
|
| 52 |
+
messages=[{"role": "user", "content": prompt}]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
return response.choices[0].message.content.strip()
|
| 56 |
+
|
| 57 |
+
# ==== Streamlit UI ====
|
| 58 |
+
st.title("πͺ US Army RAG Assistant")
|
| 59 |
+
st.markdown("Ask tactical questions based on **US Army Field Manual** knowledge.")
|
| 60 |
+
st.divider()
|
| 61 |
+
|
| 62 |
+
recommended_questions = [
|
| 63 |
+
"What are the fundamentals of offensive operations?",
|
| 64 |
+
"How does the Army define mission command?",
|
| 65 |
+
"What is the purpose of a support zone?",
|
| 66 |
+
"How do units prepare for degraded and denied environments (D3SOE)?",
|
| 67 |
+
"Explain the role of reconnaissance in the offense."
|
| 68 |
+
]
|
| 69 |
+
|
| 70 |
+
st.markdown("π‘ **Recommended Questions:**")
|
| 71 |
+
for q in recommended_questions:
|
| 72 |
+
st.markdown(f"- {q}")
|
| 73 |
+
|
| 74 |
+
# Chat state
|
| 75 |
+
if "chat_history" not in st.session_state:
|
| 76 |
+
st.session_state.chat_history = []
|
| 77 |
+
|
| 78 |
+
# ==== Input ====
|
| 79 |
+
user_input = st.text_input("Your question", placeholder="Type your question here...")
|
| 80 |
+
|
| 81 |
+
if st.button("Submit") and user_input:
|
| 82 |
+
if user_input.strip().lower() == "terminate":
|
| 83 |
+
st.session_state.chat_history.append((user_input, "π Goodbye, soldier. Stay sharp!"))
|
| 84 |
+
else:
|
| 85 |
+
answer = rag_query(user_input)
|
| 86 |
+
st.session_state.chat_history.append((user_input, answer))
|
| 87 |
+
|
| 88 |
+
if st.button("Clear Chat"):
|
| 89 |
+
st.session_state.chat_history = []
|
| 90 |
+
|
| 91 |
+
# ==== Display Chat ====
|
| 92 |
+
for i, (user, bot) in enumerate(st.session_state.chat_history):
|
| 93 |
+
st.markdown(f"**You:** {user}")
|
| 94 |
+
st.markdown(f"**Assistant:** {bot}")
|
| 95 |
+
st.divider()
|