import streamlit as st import logging import os from langchain_community.vectorstores import FAISS from langchain_community.document_loaders import CSVLoader from langchain_community.embeddings import HuggingFaceEmbeddings from langchain.prompts import PromptTemplate from langchain.llms import HuggingFaceHub import dotenv import yaml import os import zipfile zip_file = "faiss_index.zip" with zipfile.ZipFile(zip_file, 'r') as zip_ref: zip_ref.extractall(".") # Extract to the current directory print("Unzipping completed successfully.") dotenv.load_dotenv() def load_config(): with open("yaml-editor-online.yaml", "r") as f: config = yaml.safe_load(f) return config hf_token = os.getenv("HUGGING") config = load_config() logging.basicConfig(level=logging.INFO) embeddings_model = HuggingFaceEmbeddings(model_name=config["embedding_model"]) def create_vector_db(): try: loader = CSVLoader(file_path="disease.csv", source_column="Disease Information") data = loader.load() vectordb = FAISS.from_documents(documents=data, embedding=embeddings_model) vectordb.save_local(config["vector_db_path"]) logging.info("Vector database successfully created and saved.") except Exception as e: logging.error("Error creating vector database:", exc_info=e) def get_qa_chain(query): try: if not os.path.exists(config["vector_db_path"]): logging.error(f"FAISS index path does not exist: {config['vector_db_path']}") return "Error: No data found." vectordb = FAISS.load_local( config["vector_db_path"], embeddings_model, allow_dangerous_deserialization=True ) retriever = vectordb.as_retriever(score_threshold=config["score_threshold"]) relevant_docs = retriever.get_relevant_documents(query)[:3] if not relevant_docs: return "No relevant information found." summarized_context = " ".join(doc.page_content for doc in relevant_docs) prompt_template = """ Given the following health-related context and a question, generate a structured answer: QUESTION: {query} Ensure the response is easy to understand and medically accurate. """ prompt = PromptTemplate(input_variables=["query"], template=prompt_template).format(query=query) llm = HuggingFaceHub( repo_id=config["model_name"], model_kwargs={ "temperature": config["temperature"], "max_length": 200, "top_p": config["top_p"], "top_k": config["top_k"] }, huggingfacehub_api_token=hf_token ) response = llm(prompt) return response.strip() except Exception as e: logging.error("Error getting response:", exc_info=e) return "Sorry, there was an error processing your request." def main(): st.set_page_config(page_title="Health Disease Chatbot", page_icon="🩺", layout="centered") st.markdown( """ """, unsafe_allow_html=True ) st.markdown("