borutokarma123 commited on
Commit
438ca4b
Β·
verified Β·
1 Parent(s): 5802ce7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -29
app.py CHANGED
@@ -1,18 +1,17 @@
1
  import os
2
- import gradio as gr
3
  import faiss
4
  import pickle
5
  import numpy as np
 
6
  from sentence_transformers import SentenceTransformer
7
  from groq import Groq
8
 
9
- # ==== Connect to Groq using environment variable ====
10
- #client = Groq(api_key=os.environ.get("api_key5"))
 
 
11
  groq_api_key = os.environ.get("api_key5")
12
  client = Groq(api_key=groq_api_key)
13
- # Store API key in a constant
14
- # Set up the Groq API key (replace with your actual key or use an environment variable)
15
- #client = Groq(api_key="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")
16
 
17
  # ==== Load FAISS index and metadata ====
18
  index = faiss.read_index("army_qa_faiss.index")
@@ -55,7 +54,10 @@ Answer:"""
55
 
56
  return response.choices[0].message.content.strip()
57
 
58
- # ==== Chat Interface ====
 
 
 
59
 
60
  recommended_questions = [
61
  "What are the fundamentals of offensive operations?",
@@ -65,28 +67,29 @@ recommended_questions = [
65
  "Explain the role of reconnaissance in the offense."
66
  ]
67
 
68
- def chat(user_input, history):
69
- if user_input.strip().lower() == "terminate":
70
- return history + [[user_input, "πŸ‘‹ Goodbye, soldier. Stay sharp!"]], gr.update(interactive=False)
71
- answer = rag_query(user_input)
72
- return history + [[user_input, answer]], history
73
-
74
- with gr.Blocks() as demo:
75
- gr.Markdown("### πŸͺ– US Army RAG Assistant\nAsk tactical questions based on FM manual knowledge.")
76
- chatbot = gr.Chatbot()
77
- textbox = gr.Textbox(label="Your question", placeholder="Type here (or type 'terminate' to exit)")
78
- state = gr.State([])
79
- submit = gr.Button("Submit")
80
- clear = gr.Button("Clear")
81
 
82
- with gr.Row():
83
- gr.Markdown("##### πŸ’‘ Recommended Questions:")
84
- for q in recommended_questions:
85
- gr.Markdown(f"- {q}")
86
 
87
- submit.click(chat, [textbox, state], [chatbot, state])
88
- clear.click(lambda: ([], []), None, [chatbot, state])
89
-
90
- if __name__ == "__main__":
91
- demo.launch()
92
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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")
 
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?",
 
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()