Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,65 @@
|
|
| 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
|
| 8 |
from langchain.prompts import PromptTemplate
|
| 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 |
-
|
|
|
|
| 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 |
-
#
|
| 29 |
-
|
| 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")
|
| 39 |
data = loader.load()
|
|
|
|
|
|
|
| 40 |
vectordb = FAISS.from_documents(documents=data, embedding=embeddings_model)
|
| 41 |
vectordb.save_local(config["vector_db_path"])
|
|
|
|
| 42 |
logging.info("Vector database successfully created and saved.")
|
| 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"]):
|
| 54 |
logging.error(f"FAISS index path does not exist: {config['vector_db_path']}")
|
| 55 |
return "Error: No data found."
|
| 56 |
|
|
|
|
| 57 |
vectordb = FAISS.load_local(
|
| 58 |
config["vector_db_path"], embeddings_model, allow_dangerous_deserialization=True
|
| 59 |
)
|
|
@@ -90,18 +96,24 @@ def get_qa_chain(query):
|
|
| 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
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
if not query:
|
| 100 |
-
return jsonify({"error": "Query parameter is required."}), 400
|
| 101 |
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
| 103 |
return jsonify({"response": response})
|
| 104 |
|
| 105 |
-
# Run Flask app
|
| 106 |
if __name__ == "__main__":
|
| 107 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import os
|
| 2 |
+
import logging
|
| 3 |
+
import dotenv
|
| 4 |
+
import yaml
|
| 5 |
import zipfile
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from flask import Flask, request, jsonify
|
| 8 |
from langchain_community.vectorstores import FAISS
|
| 9 |
from langchain_community.document_loaders import CSVLoader
|
| 10 |
from langchain_community.embeddings import HuggingFaceEmbeddings
|
| 11 |
from langchain.prompts import PromptTemplate
|
| 12 |
from langchain.llms import HuggingFaceHub
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# Load environment variables
|
| 15 |
dotenv.load_dotenv()
|
| 16 |
|
| 17 |
+
# Load Config
|
| 18 |
def load_config():
|
| 19 |
with open("yaml-editor-online.yaml", "r") as f:
|
| 20 |
+
config = yaml.safe_load(f)
|
| 21 |
+
return config
|
| 22 |
|
|
|
|
| 23 |
config = load_config()
|
| 24 |
+
hf_token = os.getenv("HUGGING")
|
| 25 |
+
|
| 26 |
+
# Logging setup
|
| 27 |
logging.basicConfig(level=logging.INFO)
|
|
|
|
| 28 |
|
| 29 |
+
# Flask App
|
| 30 |
+
app = Flask(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
|
|
|
| 32 |
def create_vector_db():
|
| 33 |
+
"""Creates FAISS vector database from disease.csv."""
|
| 34 |
try:
|
| 35 |
+
if not os.path.exists("disease.csv"):
|
| 36 |
+
logging.error("CSV file not found!")
|
| 37 |
+
return
|
| 38 |
+
|
| 39 |
+
df = pd.read_csv("disease.csv")
|
| 40 |
+
if "Disease Information" not in df.columns:
|
| 41 |
+
logging.error("Column 'Disease Information' not found in CSV!")
|
| 42 |
+
return
|
| 43 |
+
|
| 44 |
loader = CSVLoader(file_path="disease.csv", source_column="Disease Information")
|
| 45 |
data = loader.load()
|
| 46 |
+
|
| 47 |
+
embeddings_model = HuggingFaceEmbeddings(model_name=config["embedding_model"])
|
| 48 |
vectordb = FAISS.from_documents(documents=data, embedding=embeddings_model)
|
| 49 |
vectordb.save_local(config["vector_db_path"])
|
| 50 |
+
|
| 51 |
logging.info("Vector database successfully created and saved.")
|
| 52 |
except Exception as e:
|
| 53 |
logging.error("Error creating vector database:", exc_info=e)
|
| 54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
def get_qa_chain(query):
|
| 56 |
+
"""Retrieves relevant documents and generates an answer using LLM."""
|
| 57 |
try:
|
| 58 |
if not os.path.exists(config["vector_db_path"]):
|
| 59 |
logging.error(f"FAISS index path does not exist: {config['vector_db_path']}")
|
| 60 |
return "Error: No data found."
|
| 61 |
|
| 62 |
+
embeddings_model = HuggingFaceEmbeddings(model_name=config["embedding_model"])
|
| 63 |
vectordb = FAISS.load_local(
|
| 64 |
config["vector_db_path"], embeddings_model, allow_dangerous_deserialization=True
|
| 65 |
)
|
|
|
|
| 96 |
logging.error("Error getting response:", exc_info=e)
|
| 97 |
return "Sorry, there was an error processing your request."
|
| 98 |
|
|
|
|
| 99 |
@app.route("/query", methods=["POST"])
|
| 100 |
+
def query():
|
| 101 |
+
"""API endpoint for answering disease-related queries."""
|
| 102 |
+
data = request.json
|
| 103 |
+
user_query = data.get("query", "")
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
if not user_query:
|
| 106 |
+
return jsonify({"error": "No query provided"}), 400
|
| 107 |
+
|
| 108 |
+
response = get_qa_chain(user_query)
|
| 109 |
return jsonify({"response": response})
|
| 110 |
|
|
|
|
| 111 |
if __name__ == "__main__":
|
| 112 |
+
# If FAISS index is missing, create it
|
| 113 |
+
if not os.path.exists(config["vector_db_path"]):
|
| 114 |
+
logging.info(f"Vector database not found at {config['vector_db_path']}, creating it now.")
|
| 115 |
+
create_vector_db()
|
| 116 |
+
|
| 117 |
+
# Run Flask server on Hugging Face-compatible settings
|
| 118 |
+
from waitress import serve
|
| 119 |
+
serve(app, host="0.0.0.0", port=7860)
|