rohitashva commited on
Commit
fce6881
·
verified ·
1 Parent(s): 6c91214

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -15
app.py CHANGED
@@ -8,30 +8,41 @@ from langchain.prompts import PromptTemplate
8
  from langchain.llms import HuggingFaceHub
9
  import dotenv
10
  import yaml
11
- import zipfile
12
 
 
13
  dotenv.load_dotenv()
14
 
 
15
  def load_config():
16
  with open("yaml-editor-online.yaml", "r") as f:
17
- config = yaml.safe_load(f)
18
- return config
19
 
20
- hf_token = os.getenv("HUGGING")
21
  config = load_config()
 
22
  logging.basicConfig(level=logging.INFO)
23
 
 
24
  embeddings_model = HuggingFaceEmbeddings(model_name=config["embedding_model"])
25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  def get_qa_chain(query):
27
  try:
28
- if not os.path.exists(config["vector_db_path"]):
29
- logging.error(f"FAISS index path does not exist: {config['vector_db_path']}")
30
  return "Error: No data found."
31
 
32
- vectordb = FAISS.load_local(
33
- config["vector_db_path"], embeddings_model, allow_dangerous_deserialization=True
34
- )
35
  retriever = vectordb.as_retriever(score_threshold=config["score_threshold"])
36
  relevant_docs = retriever.get_relevant_documents(query)[:3]
37
 
@@ -41,9 +52,9 @@ def get_qa_chain(query):
41
  summarized_context = " ".join(doc.page_content for doc in relevant_docs)
42
  prompt_template = """
43
  Given the following health-related context and a question, generate a structured answer:
44
-
45
  QUESTION: {query}
46
-
47
  Ensure the response is easy to understand and medically accurate.
48
  """
49
  prompt = PromptTemplate(input_variables=["query"], template=prompt_template).format(query=query)
@@ -65,9 +76,10 @@ def get_qa_chain(query):
65
  logging.error("Error getting response:", exc_info=e)
66
  return "Sorry, there was an error processing your request."
67
 
 
68
  def main():
69
  st.set_page_config(page_title="Health Disease Chatbot", page_icon="🩺", layout="centered")
70
-
71
  st.markdown("""
72
  <h1 style='text-align: center; color: #2E7D32;'>🩺 Health Disease Chatbot</h1>
73
  <p style='text-align: center; font-size: 18px;'>Enter a question related to health conditions, symptoms, or treatments.</p>
@@ -87,9 +99,9 @@ def main():
87
  }
88
  </style>
89
  """, unsafe_allow_html=True)
90
-
91
  query = st.text_input("Your health-related question:", key="query", help="Ask about diseases, symptoms, or treatments.")
92
-
93
  if st.button("Get Information"):
94
  if query:
95
  response = get_qa_chain(query)
@@ -98,4 +110,4 @@ def main():
98
  st.warning("Please enter a query to get a response.")
99
 
100
  if __name__ == "__main__":
101
- main()
 
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 into FAISS in-memory
28
+ def create_vector_db():
29
+ try:
30
+ loader = CSVLoader(file_path="disease.csv", source_column="Disease Information")
31
+ data = loader.load()
32
+ vectordb = FAISS.from_documents(documents=data, embedding=embeddings_model)
33
+ return vectordb
34
+ except Exception as e:
35
+ logging.error("Error creating vector database:", exc_info=e)
36
+ return None
37
+
38
+ vectordb = create_vector_db()
39
+
40
+ # Function to get responses
41
  def get_qa_chain(query):
42
  try:
43
+ if not vectordb:
 
44
  return "Error: No data found."
45
 
 
 
 
46
  retriever = vectordb.as_retriever(score_threshold=config["score_threshold"])
47
  relevant_docs = retriever.get_relevant_documents(query)[:3]
48
 
 
52
  summarized_context = " ".join(doc.page_content for doc in relevant_docs)
53
  prompt_template = """
54
  Given the following health-related context and a question, generate a structured answer:
55
+
56
  QUESTION: {query}
57
+
58
  Ensure the response is easy to understand and medically accurate.
59
  """
60
  prompt = PromptTemplate(input_variables=["query"], template=prompt_template).format(query=query)
 
76
  logging.error("Error getting response:", exc_info=e)
77
  return "Sorry, there was an error processing your request."
78
 
79
+ # Streamlit UI
80
  def main():
81
  st.set_page_config(page_title="Health Disease Chatbot", page_icon="🩺", layout="centered")
82
+
83
  st.markdown("""
84
  <h1 style='text-align: center; color: #2E7D32;'>🩺 Health Disease Chatbot</h1>
85
  <p style='text-align: center; font-size: 18px;'>Enter a question related to health conditions, symptoms, or treatments.</p>
 
99
  }
100
  </style>
101
  """, unsafe_allow_html=True)
102
+
103
  query = st.text_input("Your health-related question:", key="query", help="Ask about diseases, symptoms, or treatments.")
104
+
105
  if st.button("Get Information"):
106
  if query:
107
  response = get_qa_chain(query)
 
110
  st.warning("Please enter a query to get a response.")
111
 
112
  if __name__ == "__main__":
113
+ main()