Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
|
2 |
import logging
|
3 |
import os
|
4 |
from langchain_community.vectorstores import FAISS
|
@@ -8,48 +8,38 @@ from langchain.prompts import PromptTemplate
|
|
8 |
from langchain.llms import HuggingFaceHub
|
9 |
import dotenv
|
10 |
import yaml
|
|
|
11 |
import zipfile
|
12 |
-
|
13 |
zip_file = "faiss_index.zip"
|
14 |
|
15 |
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
16 |
zip_ref.extractall(".") # Extract to the current directory
|
17 |
|
18 |
print("Unzipping completed successfully.")
|
19 |
-
|
20 |
-
# Load environment variables
|
21 |
dotenv.load_dotenv()
|
22 |
|
23 |
-
# Initialize Flask app
|
24 |
-
app = Flask(__name__)
|
25 |
-
CORS(app)
|
26 |
-
|
27 |
-
# Load configuration
|
28 |
def load_config():
|
29 |
with open("yaml-editor-online.yaml", "r") as f:
|
30 |
config = yaml.safe_load(f)
|
31 |
return config
|
32 |
|
|
|
33 |
config = load_config()
|
34 |
-
hf_token = os.getenv("HUGGINGFACE_API_TOKEN")
|
35 |
-
|
36 |
logging.basicConfig(level=logging.INFO)
|
37 |
|
38 |
-
# Load embedding model
|
39 |
embeddings_model = HuggingFaceEmbeddings(model_name=config["embedding_model"])
|
40 |
|
41 |
-
# Create FAISS vector database from CSV
|
42 |
def create_vector_db():
|
43 |
try:
|
44 |
-
loader = CSVLoader(file_path="disease.csv", source_column="Disease")
|
45 |
data = loader.load()
|
46 |
vectordb = FAISS.from_documents(documents=data, embedding=embeddings_model)
|
47 |
vectordb.save_local(config["vector_db_path"])
|
48 |
logging.info("Vector database successfully created and saved.")
|
49 |
except Exception as e:
|
50 |
-
logging.error(
|
51 |
|
52 |
-
# Load vector database and get response
|
53 |
def get_qa_chain(query):
|
54 |
try:
|
55 |
if not os.path.exists(config["vector_db_path"]):
|
@@ -69,14 +59,11 @@ def get_qa_chain(query):
|
|
69 |
prompt_template = """
|
70 |
Given the following health-related context and a question, generate a structured answer:
|
71 |
|
72 |
-
CONTEXT: {context}
|
73 |
QUESTION: {query}
|
74 |
|
75 |
Ensure the response is easy to understand and medically accurate.
|
76 |
"""
|
77 |
-
prompt = PromptTemplate(input_variables=["query"
|
78 |
-
query=query, context=summarized_context
|
79 |
-
)
|
80 |
|
81 |
llm = HuggingFaceHub(
|
82 |
repo_id=config["model_name"],
|
@@ -92,24 +79,53 @@ def get_qa_chain(query):
|
|
92 |
response = llm(prompt)
|
93 |
return response.strip()
|
94 |
except Exception as e:
|
95 |
-
logging.error(
|
96 |
return "Sorry, there was an error processing your request."
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
|
110 |
-
# Run Flask app
|
111 |
if __name__ == "__main__":
|
112 |
if not os.path.exists(config["vector_db_path"]):
|
113 |
logging.info(f"Vector database not found at {config['vector_db_path']}, creating it now.")
|
114 |
create_vector_db()
|
115 |
-
|
|
|
1 |
+
import streamlit as st
|
2 |
import logging
|
3 |
import os
|
4 |
from langchain_community.vectorstores import FAISS
|
|
|
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"]):
|
|
|
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)
|
|
|
|
|
67 |
|
68 |
llm = HuggingFaceHub(
|
69 |
repo_id=config["model_name"],
|
|
|
79 |
response = llm(prompt)
|
80 |
return response.strip()
|
81 |
except Exception as e:
|
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()
|