rohitashva commited on
Commit
1a83863
·
verified ·
1 Parent(s): cb9e9f6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -49
app.py CHANGED
@@ -1,58 +1,54 @@
1
  import streamlit as st
2
  import logging
3
  import os
4
- import pandas as pd
5
  from langchain_community.vectorstores import FAISS
 
6
  from langchain_community.embeddings import HuggingFaceEmbeddings
7
  from langchain.prompts import PromptTemplate
8
  from langchain.llms import HuggingFaceHub
9
  import dotenv
10
  import yaml
 
 
 
 
11
 
12
- # Load environment variables
 
 
 
13
  dotenv.load_dotenv()
14
 
15
- # Load YAML config
16
  def load_config():
17
  with open("yaml-editor-online.yaml", "r") as f:
18
- return yaml.safe_load(f)
 
19
 
20
- config = load_config()
21
  hf_token = os.getenv("HUGGING")
 
22
  logging.basicConfig(level=logging.INFO)
23
 
24
- # Load embedding model
25
  embeddings_model = HuggingFaceEmbeddings(model_name=config["embedding_model"])
26
 
27
- # Load disease data manually using pandas
28
  def create_vector_db():
29
  try:
30
- if not os.path.exists("disease.csv"):
31
- logging.error("Error: disease.csv file not found.")
32
- return None
33
-
34
- df = pd.read_csv("disease.csv")
35
-
36
- # Check if the expected column exists
37
- if "Disease Information" not in df.columns:
38
- logging.error("Error: 'Disease Information' column not found in CSV.")
39
- return None
40
-
41
- data = [{"page_content": row["Disease Information"]} for _, row in df.iterrows()]
42
  vectordb = FAISS.from_documents(documents=data, embedding=embeddings_model)
43
- return vectordb
 
44
  except Exception as e:
45
  logging.error("Error creating vector database:", exc_info=e)
46
- return None
47
 
48
- vectordb = create_vector_db()
49
-
50
- # Function to get responses
51
  def get_qa_chain(query):
52
  try:
53
- if not vectordb:
 
54
  return "Error: No data found."
55
 
 
 
 
56
  retriever = vectordb.as_retriever(score_threshold=config["score_threshold"])
57
  relevant_docs = retriever.get_relevant_documents(query)[:3]
58
 
@@ -62,9 +58,9 @@ def get_qa_chain(query):
62
  summarized_context = " ".join(doc.page_content for doc in relevant_docs)
63
  prompt_template = """
64
  Given the following health-related context and a question, generate a structured answer:
65
-
66
  QUESTION: {query}
67
-
68
  Ensure the response is easy to understand and medically accurate.
69
  """
70
  prompt = PromptTemplate(input_variables=["query"], template=prompt_template).format(query=query)
@@ -86,38 +82,50 @@ def get_qa_chain(query):
86
  logging.error("Error getting response:", exc_info=e)
87
  return "Sorry, there was an error processing your request."
88
 
89
- # Streamlit UI
90
  def main():
91
  st.set_page_config(page_title="Health Disease Chatbot", page_icon="🩺", layout="centered")
92
-
93
- st.markdown("""
94
- <h1 style='text-align: center; color: #2E7D32;'>🩺 Health Disease Chatbot</h1>
95
- <p style='text-align: center; font-size: 18px;'>Enter a question related to health conditions, symptoms, or treatments.</p>
96
  <style>
97
- .stTextInput > div > div > input {
98
- border-radius: 10px;
99
- padding: 10px;
100
- border: 1px solid #2E7D32;
101
- }
102
- .response-box {
103
- background-color: #ffffff;
104
- padding: 15px;
105
- border-radius: 8px;
106
- box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
107
- margin-top: 15px;
108
- font-size: 16px;
109
- }
 
 
 
 
 
 
110
  </style>
111
- """, unsafe_allow_html=True)
112
-
 
 
 
 
 
113
  query = st.text_input("Your health-related question:", key="query", help="Ask about diseases, symptoms, or treatments.")
114
-
115
  if st.button("Get Information"):
116
  if query:
117
  response = get_qa_chain(query)
118
- st.markdown(f"""<div class='response-box'><b>Response:</b><br>{response}</div>""", unsafe_allow_html=True)
119
  else:
120
  st.warning("Please enter a query to get a response.")
121
 
122
  if __name__ == "__main__":
 
 
 
123
  main()
 
1
  import streamlit as st
2
  import logging
3
  import os
 
4
  from langchain_community.vectorstores import FAISS
5
+ from langchain_community.document_loaders import CSVLoader
6
  from langchain_community.embeddings import HuggingFaceEmbeddings
7
  from langchain.prompts import PromptTemplate
8
  from langchain.llms import HuggingFaceHub
9
  import dotenv
10
  import yaml
11
+ import os
12
+ import zipfile
13
+
14
+ zip_file = "faiss_index.zip"
15
 
16
+ with zipfile.ZipFile(zip_file, 'r') as zip_ref:
17
+ zip_ref.extractall(".") # Extract to the current directory
18
+
19
+ print("Unzipping completed successfully.")
20
  dotenv.load_dotenv()
21
 
 
22
  def load_config():
23
  with open("yaml-editor-online.yaml", "r") as f:
24
+ config = yaml.safe_load(f)
25
+ return config
26
 
 
27
  hf_token = os.getenv("HUGGING")
28
+ config = load_config()
29
  logging.basicConfig(level=logging.INFO)
30
 
 
31
  embeddings_model = HuggingFaceEmbeddings(model_name=config["embedding_model"])
32
 
 
33
  def create_vector_db():
34
  try:
35
+ loader = CSVLoader(file_path="disease.csv", source_column="Disease Information")
36
+ data = loader.load()
 
 
 
 
 
 
 
 
 
 
37
  vectordb = FAISS.from_documents(documents=data, embedding=embeddings_model)
38
+ vectordb.save_local(config["vector_db_path"])
39
+ logging.info("Vector database successfully created and saved.")
40
  except Exception as e:
41
  logging.error("Error creating vector database:", exc_info=e)
 
42
 
 
 
 
43
  def get_qa_chain(query):
44
  try:
45
+ if not os.path.exists(config["vector_db_path"]):
46
+ logging.error(f"FAISS index path does not exist: {config['vector_db_path']}")
47
  return "Error: No data found."
48
 
49
+ vectordb = FAISS.load_local(
50
+ config["vector_db_path"], embeddings_model, allow_dangerous_deserialization=True
51
+ )
52
  retriever = vectordb.as_retriever(score_threshold=config["score_threshold"])
53
  relevant_docs = retriever.get_relevant_documents(query)[:3]
54
 
 
58
  summarized_context = " ".join(doc.page_content for doc in relevant_docs)
59
  prompt_template = """
60
  Given the following health-related context and a question, generate a structured answer:
61
+
62
  QUESTION: {query}
63
+
64
  Ensure the response is easy to understand and medically accurate.
65
  """
66
  prompt = PromptTemplate(input_variables=["query"], template=prompt_template).format(query=query)
 
82
  logging.error("Error getting response:", exc_info=e)
83
  return "Sorry, there was an error processing your request."
84
 
 
85
  def main():
86
  st.set_page_config(page_title="Health Disease Chatbot", page_icon="🩺", layout="centered")
87
+
88
+ st.markdown(
89
+ """
 
90
  <style>
91
+ .stApp {
92
+ background-color: #f0f2f6;
93
+ color: #333;
94
+ font-family: 'Arial', sans-serif;
95
+ }
96
+ .title {
97
+ color: #2E7D32;
98
+ text-align: center;
99
+ }
100
+ .query-input {
101
+ border-radius: 10px;
102
+ padding: 10px;
103
+ }
104
+ .response-box {
105
+ background-color: #ffffff;
106
+ padding: 15px;
107
+ border-radius: 8px;
108
+ box-shadow: 2px 2px 10px rgba(0,0,0,0.1);
109
+ }
110
  </style>
111
+ """,
112
+ unsafe_allow_html=True
113
+ )
114
+
115
+ st.markdown("<h1 class='title'>🩺 Health Disease Chatbot</h1>", unsafe_allow_html=True)
116
+ st.write("Enter a question related to health conditions, symptoms, or treatments.")
117
+
118
  query = st.text_input("Your health-related question:", key="query", help="Ask about diseases, symptoms, or treatments.")
119
+
120
  if st.button("Get Information"):
121
  if query:
122
  response = get_qa_chain(query)
123
+ st.markdown(f"<div class='response-box'><b>Response:</b><br>{response}</div>", unsafe_allow_html=True)
124
  else:
125
  st.warning("Please enter a query to get a response.")
126
 
127
  if __name__ == "__main__":
128
+ if not os.path.exists(config["vector_db_path"]):
129
+ logging.info(f"Vector database not found at {config['vector_db_path']}, creating it now.")
130
+ create_vector_db()
131
  main()