Spaces:
Sleeping
Sleeping
Upload 14 files
Browse files- .env +3 -0
- Dockerfile +20 -0
- app.py +71 -0
- requirements.txt +9 -0
- setup.py +10 -0
- src/__pycache__/helper.cpython-310.pyc +0 -0
- src/__pycache__/prompt.cpython-310.pyc +0 -0
- src/_init_.py +0 -0
- src/helper.py +49 -0
- src/prompt.py +9 -0
- static/style.css +0 -0
- store_index.py +45 -0
- template.sh +15 -0
- templates/chat.html +81 -0
.env
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
COHERE_API_KEY="cohere_WLMxZKMic5WZ3BI14kGAhRcnc1lfOeyxUpoEalsQ0GXKb0"
|
| 2 |
+
|
| 3 |
+
PINECONE_API_KEY="pcsk_3gubdo_LjdCW5RRJrPkUHab5BUtkEk414Mt3jMTQmfLrnFjsYbkBZucBmQArEXkU1XGxRZ"
|
Dockerfile
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use an official Python runtime as a parent image
|
| 2 |
+
FROM python:3.10-slim
|
| 3 |
+
|
| 4 |
+
# Set the working directory in the container
|
| 5 |
+
WORKDIR /code
|
| 6 |
+
|
| 7 |
+
# Copy the requirements file into the container
|
| 8 |
+
COPY ./requirements.txt /code/requirements.txt
|
| 9 |
+
|
| 10 |
+
# Install any needed packages specified in requirements.txt
|
| 11 |
+
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 12 |
+
|
| 13 |
+
# Copy the rest of your application code
|
| 14 |
+
COPY . .
|
| 15 |
+
|
| 16 |
+
# Expose port 7860 (Hugging Face Spaces default port)
|
| 17 |
+
EXPOSE 7860
|
| 18 |
+
|
| 19 |
+
# Run app.py when the container launches
|
| 20 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, jsonify, request
|
| 2 |
+
from src.helper import download_hugging_face_embeddings
|
| 3 |
+
from langchain_pinecone import PineconeVectorStore
|
| 4 |
+
from langchain_openai import ChatOpenAI
|
| 5 |
+
from langchain.chains import create_retrieval_chain
|
| 6 |
+
from langchain.chains.combine_documents import create_stuff_documents_chain
|
| 7 |
+
from langchain_core.prompts import ChatPromptTemplate
|
| 8 |
+
from dotenv import load_dotenv
|
| 9 |
+
from langchain_cohere import ChatCohere
|
| 10 |
+
from src.prompt import *
|
| 11 |
+
import os
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
app = Flask(__name__)
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
load_dotenv()
|
| 18 |
+
|
| 19 |
+
PINECONE_API_KEY=os.environ.get('PINECONE_API_KEY')
|
| 20 |
+
COHERE_API_KEY=os.environ.get('COHERE_API_KEY')
|
| 21 |
+
|
| 22 |
+
os.environ["PINECONE_API_KEY"] = PINECONE_API_KEY
|
| 23 |
+
os.environ["COHERE_API_KEY"] = COHERE_API_KEY
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
embeddings = download_hugging_face_embeddings()
|
| 27 |
+
|
| 28 |
+
index_name = "medical-chatbot"
|
| 29 |
+
# Embed each chunk and upsert the embeddings into your Pinecone index.
|
| 30 |
+
docsearch = PineconeVectorStore.from_existing_index(
|
| 31 |
+
index_name=index_name,
|
| 32 |
+
embedding=embeddings
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
retriever = docsearch.as_retriever(search_type="similarity", search_kwargs={"k":3})
|
| 39 |
+
|
| 40 |
+
chatModel = ChatCohere(model="command-r-plus-08-2024", temperature=0.4)
|
| 41 |
+
prompt = ChatPromptTemplate.from_messages(
|
| 42 |
+
[
|
| 43 |
+
("system", system_prompt),
|
| 44 |
+
("human", "{input}"),
|
| 45 |
+
]
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
+
question_answer_chain = create_stuff_documents_chain(chatModel, prompt)
|
| 49 |
+
rag_chain = create_retrieval_chain(retriever, question_answer_chain)
|
| 50 |
+
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@app.route("/")
|
| 54 |
+
def index():
|
| 55 |
+
return render_template('chat.html')
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|
| 59 |
+
@app.route("/get", methods=["GET", "POST"])
|
| 60 |
+
def chat():
|
| 61 |
+
msg = request.form["msg"]
|
| 62 |
+
input = msg
|
| 63 |
+
print(input)
|
| 64 |
+
response = rag_chain.invoke({"input": msg})
|
| 65 |
+
print("Response : ", response["answer"])
|
| 66 |
+
return str(response["answer"])
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
if __name__ == '__main__':
|
| 71 |
+
app.run(host="0.0.0.0", port= 8080, debug= True)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
langchain==0.3.26
|
| 2 |
+
flask==3.1.1
|
| 3 |
+
sentence-transformers==4.1.0
|
| 4 |
+
pypdf==5.6.1
|
| 5 |
+
python-dotenv==1.1.0
|
| 6 |
+
langchain-pinecone==0.2.8
|
| 7 |
+
cohere>=4.0.0
|
| 8 |
+
langchain-community==0.3.26
|
| 9 |
+
-e .
|
setup.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from setuptools import find_packages, setup
|
| 2 |
+
|
| 3 |
+
setup(
|
| 4 |
+
name="medical_chatbot",
|
| 5 |
+
version="0.1.0",
|
| 6 |
+
author="Arjun S",
|
| 7 |
+
author_email="arjunselvaraj0107@gmail.com",
|
| 8 |
+
packages=find_packages(),
|
| 9 |
+
install_requires=[]
|
| 10 |
+
)
|
src/__pycache__/helper.cpython-310.pyc
ADDED
|
Binary file (1.57 kB). View file
|
|
|
src/__pycache__/prompt.cpython-310.pyc
ADDED
|
Binary file (433 Bytes). View file
|
|
|
src/_init_.py
ADDED
|
File without changes
|
src/helper.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.document_loaders import PyPDFLoader, DirectoryLoader
|
| 2 |
+
from langchain.text_splitter import RecursiveCharacterTextSplitter
|
| 3 |
+
from langchain.embeddings import HuggingFaceEmbeddings
|
| 4 |
+
from typing import List
|
| 5 |
+
from langchain.schema import Document
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
#Extract Data From the PDF File
|
| 9 |
+
def load_pdf_file(data):
|
| 10 |
+
loader= DirectoryLoader(data,
|
| 11 |
+
glob="*.pdf",
|
| 12 |
+
loader_cls=PyPDFLoader)
|
| 13 |
+
|
| 14 |
+
documents=loader.load()
|
| 15 |
+
|
| 16 |
+
return documents
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def filter_to_minimal_docs(docs: List[Document]) -> List[Document]:
|
| 21 |
+
"""
|
| 22 |
+
Given a list of Document objects, return a new list of Document objects
|
| 23 |
+
containing only 'source' in metadata and the original page_content.
|
| 24 |
+
"""
|
| 25 |
+
minimal_docs: List[Document] = []
|
| 26 |
+
for doc in docs:
|
| 27 |
+
src = doc.metadata.get("source")
|
| 28 |
+
minimal_docs.append(
|
| 29 |
+
Document(
|
| 30 |
+
page_content=doc.page_content,
|
| 31 |
+
metadata={"source": src}
|
| 32 |
+
)
|
| 33 |
+
)
|
| 34 |
+
return minimal_docs
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
#Split the Data into Text Chunks
|
| 39 |
+
def text_split(extracted_data):
|
| 40 |
+
text_splitter=RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=20)
|
| 41 |
+
text_chunks=text_splitter.split_documents(extracted_data)
|
| 42 |
+
return text_chunks
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
#Download the Embeddings from HuggingFace
|
| 47 |
+
def download_hugging_face_embeddings():
|
| 48 |
+
embeddings=HuggingFaceEmbeddings(model_name='sentence-transformers/all-MiniLM-L6-v2') #this model return 384 dimensions
|
| 49 |
+
return embeddings
|
src/prompt.py
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
system_prompt = (
|
| 2 |
+
"You are an Medical assistant for question-answering tasks. "
|
| 3 |
+
"Use the following pieces of retrieved context to answer "
|
| 4 |
+
"the question. If you don't know the answer, say that you "
|
| 5 |
+
"don't know. Use three sentences maximum and keep the "
|
| 6 |
+
"answer concise."
|
| 7 |
+
"\n\n"
|
| 8 |
+
"{context}"
|
| 9 |
+
)
|
static/style.css
ADDED
|
File without changes
|
store_index.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dotenv import load_dotenv
|
| 2 |
+
import os
|
| 3 |
+
from src.helper import load_pdf_file, filter_to_minimal_docs, text_split, download_hugging_face_embeddings
|
| 4 |
+
from pinecone import Pinecone
|
| 5 |
+
from pinecone import ServerlessSpec
|
| 6 |
+
from langchain_pinecone import PineconeVectorStore
|
| 7 |
+
load_dotenv()
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
PINECONE_API_KEY=os.environ.get('PINECONE_API_KEY')
|
| 11 |
+
COHERE_API_KEY=os.environ.get('COHERE_API_KEY')
|
| 12 |
+
|
| 13 |
+
os.environ["PINECONE_API_KEY"] = PINECONE_API_KEY
|
| 14 |
+
os.environ["COHERE_API_KEY"] = COHERE_API_KEY
|
| 15 |
+
|
| 16 |
+
|
| 17 |
+
extracted_data=load_pdf_file(data='data/')
|
| 18 |
+
filter_data = filter_to_minimal_docs(extracted_data)
|
| 19 |
+
text_chunks=text_split(filter_data)
|
| 20 |
+
|
| 21 |
+
embeddings = download_hugging_face_embeddings()
|
| 22 |
+
|
| 23 |
+
pinecone_api_key = PINECONE_API_KEY
|
| 24 |
+
pc = Pinecone(api_key=pinecone_api_key)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
index_name = "medical-chatbot" # change if desired
|
| 29 |
+
|
| 30 |
+
if not pc.has_index(index_name):
|
| 31 |
+
pc.create_index(
|
| 32 |
+
name=index_name,
|
| 33 |
+
dimension=384,
|
| 34 |
+
metric="cosine",
|
| 35 |
+
spec=ServerlessSpec(cloud="aws", region="us-east-1"),
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
index = pc.Index(index_name)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
docsearch = PineconeVectorStore.from_documents(
|
| 42 |
+
documents=text_chunks,
|
| 43 |
+
index_name=index_name,
|
| 44 |
+
embedding=embeddings,
|
| 45 |
+
)
|
template.sh
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Creating directory
|
| 2 |
+
mkdir -p src
|
| 3 |
+
mkdir -p research
|
| 4 |
+
|
| 5 |
+
# Creating files
|
| 6 |
+
touch src/_init_.py
|
| 7 |
+
touch src/helper.py
|
| 8 |
+
touch src/prompt.py
|
| 9 |
+
touch .env
|
| 10 |
+
touch setup.py
|
| 11 |
+
touch app.py
|
| 12 |
+
touch research/trials.ipynb
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
echo "Directory and files created successfully!."
|
templates/chat.html
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<link href="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" rel="stylesheet" id="bootstrap-css">
|
| 2 |
+
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.1.1/js/bootstrap.min.js"></script>
|
| 3 |
+
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
|
| 4 |
+
|
| 5 |
+
<!DOCTYPE html>
|
| 6 |
+
<html>
|
| 7 |
+
<head>
|
| 8 |
+
<title>Chatbot</title>
|
| 9 |
+
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
|
| 10 |
+
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.5.0/css/all.css" integrity="sha384-B4dIYHKNBt8Bc12p+WXckhzcICo0wtJAoU8YZTY5qE0Id1GSseTk6S+L3BlXeVIU" crossorigin="anonymous">
|
| 11 |
+
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
|
| 12 |
+
<link rel="stylesheet" type="text/css" href="{{ url_for('static', filename='style.css')}}"/>
|
| 13 |
+
</head>
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
<body>
|
| 17 |
+
<div class="container-fluid h-100">
|
| 18 |
+
<div class="row justify-content-center h-100">
|
| 19 |
+
<div class="col-md-8 col-xl-6 chat">
|
| 20 |
+
<div class="card">
|
| 21 |
+
<div class="card-header msg_head">
|
| 22 |
+
<div class="d-flex bd-highlight">
|
| 23 |
+
<div class="img_cont">
|
| 24 |
+
<img src="https://cdn-icons-png.flaticon.com/512/387/387569.png" class="rounded-circle user_img">
|
| 25 |
+
<!-- <img src="https://www.prdistribution.com/spirit/uploads/pressreleases/2019/newsreleases/d83341deb75c4c4f6b113f27b1e42cd8-chatbot-florence-already-helps-thousands-of-patients-to-remember-their-medication.png" class="rounded-circle user_img"> -->
|
| 26 |
+
<span class="online_icon"></span>
|
| 27 |
+
</div>
|
| 28 |
+
<div class="user_info">
|
| 29 |
+
<span>Medical Chatbot</span>
|
| 30 |
+
<p>Ask me anything!</p>
|
| 31 |
+
</div>
|
| 32 |
+
</div>
|
| 33 |
+
</div>
|
| 34 |
+
<div id="messageFormeight" class="card-body msg_card_body">
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
</div>
|
| 38 |
+
<div class="card-footer">
|
| 39 |
+
<form id="messageArea" class="input-group">
|
| 40 |
+
<input type="text" id="text" name="msg" placeholder="Type your message..." autocomplete="off" class="form-control type_msg" required/>
|
| 41 |
+
<div class="input-group-append">
|
| 42 |
+
<button type="submit" id="send" class="input-group-text send_btn"><i class="fas fa-location-arrow"></i></button>
|
| 43 |
+
</div>
|
| 44 |
+
</form>
|
| 45 |
+
</div>
|
| 46 |
+
</div>
|
| 47 |
+
</div>
|
| 48 |
+
</div>
|
| 49 |
+
</div>
|
| 50 |
+
|
| 51 |
+
<script>
|
| 52 |
+
$(document).ready(function() {
|
| 53 |
+
$("#messageArea").on("submit", function(event) {
|
| 54 |
+
const date = new Date();
|
| 55 |
+
const hour = date.getHours();
|
| 56 |
+
const minute = date.getMinutes();
|
| 57 |
+
const str_time = hour+":"+minute;
|
| 58 |
+
var rawText = $("#text").val();
|
| 59 |
+
|
| 60 |
+
var userHtml = '<div class="d-flex justify-content-end mb-4"><div class="msg_cotainer_send">' + rawText + '<span class="msg_time_send">'+ str_time + '</span></div><div class="img_cont_msg"><img src="https://i.ibb.co/d5b84Xw/Untitled-design.png" class="rounded-circle user_img_msg"></div></div>';
|
| 61 |
+
|
| 62 |
+
$("#text").val("");
|
| 63 |
+
$("#messageFormeight").append(userHtml);
|
| 64 |
+
|
| 65 |
+
$.ajax({
|
| 66 |
+
data: {
|
| 67 |
+
msg: rawText,
|
| 68 |
+
},
|
| 69 |
+
type: "POST",
|
| 70 |
+
url: "/get",
|
| 71 |
+
}).done(function(data) {
|
| 72 |
+
var botHtml = '<div class="d-flex justify-content-start mb-4"><div class="img_cont_msg"><img src="https://cdn-icons-png.flaticon.com/512/387/387569.png" class="rounded-circle user_img_msg"></div><div class="msg_cotainer">' + data + '<span class="msg_time">' + str_time + '</span></div></div>';
|
| 73 |
+
$("#messageFormeight").append($.parseHTML(botHtml));
|
| 74 |
+
});
|
| 75 |
+
event.preventDefault();
|
| 76 |
+
});
|
| 77 |
+
});
|
| 78 |
+
</script>
|
| 79 |
+
|
| 80 |
+
</body>
|
| 81 |
+
</html>
|