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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -55
app.py CHANGED
@@ -1,6 +1,7 @@
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
@@ -8,28 +9,30 @@ 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")
@@ -40,6 +43,11 @@ def create_vector_db():
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"]):
@@ -82,50 +90,18 @@ def get_qa_chain(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()
 
1
+ from flask import Flask, request, jsonify
2
  import logging
3
  import os
4
+ import zipfile
5
  from langchain_community.vectorstores import FAISS
6
  from langchain_community.document_loaders import CSVLoader
7
  from langchain_community.embeddings import HuggingFaceEmbeddings
 
9
  from langchain.llms import HuggingFaceHub
10
  import dotenv
11
  import yaml
 
 
12
 
13
+ # Initialize Flask app
14
+ app = Flask(__name__)
15
 
16
+ # Load environment variables
 
 
 
17
  dotenv.load_dotenv()
18
 
19
  def load_config():
20
  with open("yaml-editor-online.yaml", "r") as f:
21
+ return yaml.safe_load(f)
 
22
 
23
  hf_token = os.getenv("HUGGING")
24
  config = load_config()
25
  logging.basicConfig(level=logging.INFO)
 
26
  embeddings_model = HuggingFaceEmbeddings(model_name=config["embedding_model"])
27
 
28
+ # Unzip FAISS index
29
+ zip_file = "faiss_index.zip"
30
+ if os.path.exists(zip_file):
31
+ with zipfile.ZipFile(zip_file, 'r') as zip_ref:
32
+ zip_ref.extractall(".")
33
+ print("Unzipping completed successfully.")
34
+
35
+ # Create vector database if not found
36
  def create_vector_db():
37
  try:
38
  loader = CSVLoader(file_path="disease.csv", source_column="Disease Information")
 
43
  except Exception as e:
44
  logging.error("Error creating vector database:", exc_info=e)
45
 
46
+ if not os.path.exists(config["vector_db_path"]):
47
+ logging.info(f"Vector database not found at {config['vector_db_path']}, creating it now.")
48
+ create_vector_db()
49
+
50
+ # Function to handle query requests
51
  def get_qa_chain(query):
52
  try:
53
  if not os.path.exists(config["vector_db_path"]):
 
90
  logging.error("Error getting response:", exc_info=e)
91
  return "Sorry, there was an error processing your request."
92
 
93
+ # Define API route
94
+ @app.route("/query", methods=["POST"])
95
+ def query_api():
96
+ data = request.get_json()
97
+ query = data.get("query", "").strip()
98
+
99
+ if not query:
100
+ return jsonify({"error": "Query parameter is required."}), 400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
+ response = get_qa_chain(query)
103
+ return jsonify({"response": response})
 
 
 
 
104
 
105
+ # Run Flask app
106
  if __name__ == "__main__":
107
+ app.run(host="0.0.0.0", port=5000, debug=True)