Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import os | |
| from langchain_google_genai import GoogleGenerativeAIEmbeddings | |
| import google.generativeai as genai | |
| from langchain.vectorstores import FAISS | |
| from langchain_google_genai import ChatGoogleGenerativeAI | |
| from langchain.chains.question_answering import load_qa_chain | |
| from langchain.prompts import PromptTemplate | |
| from dotenv import load_dotenv | |
| # Load environment variables | |
| load_dotenv() | |
| genai.configure(api_key=os.getenv("GOOGLE_API_KEY")) | |
| def load_models_and_embeddings(): | |
| embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001") | |
| conversational_model = ChatGoogleGenerativeAI(model="gemini-2.5-pro", temperature=0.9) | |
| return embeddings, conversational_model | |
| def get_conversational_chain(conversational_model): | |
| prompt_template = """ | |
| Answer the question as detailed as possible from the provided context. If the answer is not in the context, say "answer is not available in the context" and do not provide an incorrect answer.\n\n | |
| Context:\n{context}\n | |
| Question:\n{question}\n | |
| Answer: | |
| """ | |
| prompt = PromptTemplate(template=prompt_template, input_variables=["context", "question"]) | |
| chain = load_qa_chain(conversational_model, chain_type="stuff", prompt=prompt) | |
| return chain | |
| def user_input(user_question, embeddings, conversational_model): | |
| try: | |
| new_db = FAISS.load_local("faiss_index", embeddings, allow_dangerous_deserialization=True) | |
| docs = new_db.similarity_search(user_question, k=20) | |
| chain = get_conversational_chain(conversational_model) | |
| response = chain({"input_documents": docs, "question": user_question}, return_only_outputs=True) | |
| # # Display context and response | |
| # st.write("### Context Used:") | |
| # for doc in docs: | |
| # st.write(doc.page_content) | |
| st.write("### Answer:") | |
| st.write(response["output_text"]) | |
| except Exception as e: | |
| st.error(f"An error occurred: {e}") | |
| def main(): | |
| st.set_page_config(page_title="HawkEyes") | |
| st.header("HawkEyes BOT") | |
| embeddings, conversational_model = load_models_and_embeddings() | |
| user_question = st.text_input("Ask Questions About HawkEyes") | |
| if user_question: | |
| user_input(user_question, embeddings, conversational_model) | |
| if __name__ == "__main__": | |
| main() | |