Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
443 changes: 443 additions & 0 deletions Database/SQL_medical_assistant.sql

Large diffs are not rendered by default.

Binary file added Database/med_assist.db
Binary file not shown.
142 changes: 142 additions & 0 deletions Database/report_type_update3+.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import os
import time
from langchain_community.document_loaders import PyMuPDFLoader
from langchain_community.embeddings import AnyscaleEmbeddings
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Pinecone as PineconeStore
from langchain_community.chat_models import ChatAnyscale
from langchain.chains import create_retrieval_chain
from langchain.chains.combine_documents import create_stuff_documents_chain
from langchain_core.prompts import ChatPromptTemplate
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import declarative_base, sessionmaker
import pinecone

# Set API keys and environment variables
os.environ["ANYSCALE_API_KEY"] = ""
os.environ["PINECONE_API_KEY"] = ""
os.environ["HUGGINGFACEHUB_API_TOKEN"] = ""
os.environ["TOKENIZERS_PARALLELISM"] = "false"

# Connect to the SQLite database
engine = create_engine('sqlite:///C:/Users/MI/Sqlite/med_assist.db', echo=True)
Base = declarative_base()

# Define the report_type table
class ReportType(Base):
__tablename__ = 'report_type'
report_type_id = Column(Integer, primary_key=True, autoincrement=True)
report_type_name = Column(String, nullable=False)

Session = sessionmaker(bind=engine)
session = Session()

# Load data
pdf_loader = PyMuPDFLoader('Blood report.pdf')
Blood_reports = pdf_loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=150)
pdf_doc = text_splitter.split_documents(Blood_reports)

# Initialize a LangChain embedding object
api_key = os.getenv("ANYSCALE_API_KEY")
embeddings = AnyscaleEmbeddings(
anyscale_api_key=api_key, model="thenlper/gte-large"
)

# Initialize Pinecone
pc = pinecone.Pinecone(api_key=os.getenv("PINECONE_API_KEY"))

# Create index if it doesn't exist
index_name = "pp"
if index_name not in pc.list_indexes().names():
pc.create_index(
name=index_name,
dimension=1024,
metric="cosine",
spec=pinecone.ServerlessSpec(
cloud="aws",
region="us-east-1"
)
)

# Initialize Pinecone index
index = pc.Index(index_name)

# Embed each chunk and upsert the embeddings into a distinct namespace
namespace = "pp1"
pineconeVector = PineconeStore.from_documents(
documents=pdf_doc,
index_name=index_name,
embedding=embeddings,
namespace=namespace
)

time.sleep(1)

# Initialize the language model with Anyscale
llm = ChatAnyscale(anyscale_api_key=os.getenv('ANYSCALE_API_KEY'), model='meta-llama/Meta-Llama-3-8B-Instruct')

# Initialize the Pinecone retriever
retriever = pineconeVector.as_retriever()
docs = retriever.invoke("what information is contained in the document")

# Define the system prompt
system_prompt = (
"Use the given context to answer the question. "
"If you don't know the answer, say you don't know. "
"Use three sentences maximum and keep the answer concise. "
"Context: {context}"
)

# Create the prompt template
prompt = ChatPromptTemplate.from_messages(
[
("system", system_prompt),
("human", "{input}"),
]
)
# Create the question-answer chain
question_answer_chain = create_stuff_documents_chain(llm, prompt)
# Create the retrieval chain
chain = create_retrieval_chain(retriever, question_answer_chain)

#Query input
query = "What kind of report is this? Blood, urine, or other type?"

# Invoke the chain with the query
response = chain.invoke({"input": query})

# Extract the answer from the response
answer = response['answer']

print(answer)

# Extract the report type from the answer
if "blood" in answer.lower():
report_type = "Blood report"
elif "urine" in answer.lower():
report_type = "Urinalysis"
else:
report_type = "Other report"

# Perform the query and update the database
query_vector = embeddings.embed_query(query)
results = index.query(vector=query_vector, top_k=10, namespace=namespace)

for match in results['matches']:
matched_id = match['id']

# Check if the record already exists
existing_record = session.query(ReportType).filter_by(report_type_name=report_type).first()
if existing_record:
print(f"Record with report_type_name '{report_type}' already exists.")
else:
# Insert a new record
new_report_type = ReportType(
report_type_name=report_type
)
session.add(new_report_type)

# Commit changes to the database
session.commit()
session.close()
Empty file added UI/desktop_app/__init__.py
Empty file.
Binary file added UI/desktop_app/db.sqlite3
Binary file not shown.
Empty file.
16 changes: 16 additions & 0 deletions UI/desktop_app/desktop_app/asgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
ASGI config for desktop_app project.

It exposes the ASGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/asgi/
"""

import os

from django.core.asgi import get_asgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'desktop_app.settings')

application = get_asgi_application()
130 changes: 130 additions & 0 deletions UI/desktop_app/desktop_app/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
"""
Django settings for desktop_app project.

Generated by 'django-admin startproject' using Django 5.0.6.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.0/ref/settings/
"""
import os
from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/5.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-jwnz+2p7%6-ywxxl4m&&biho7^m+20c7wmqccb2hs_@)ak-s6t'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ui',
'ui.DB_query'
]

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'desktop_app.urls'

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

WSGI_APPLICATION = 'desktop_app.wsgi.application'


# Database
# https://docs.djangoproject.com/en/5.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}


# Password validation
# https://docs.djangoproject.com/en/5.0/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
{
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
},
{
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
},
]


# Internationalization
# https://docs.djangoproject.com/en/5.0/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.0/howto/static-files/

STATIC_URL = '/static/'

STATICFILES_DIRS = [
BASE_DIR / "ui/static",
]

# Default primary key field type
# https://docs.djangoproject.com/en/5.0/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

7 changes: 7 additions & 0 deletions UI/desktop_app/desktop_app/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('ui.urls')),
]
16 changes: 16 additions & 0 deletions UI/desktop_app/desktop_app/wsgi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""
WSGI config for desktop_app project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'desktop_app.settings')

application = get_wsgi_application()
1 change: 1 addition & 0 deletions UI/desktop_app/manage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

24 changes: 24 additions & 0 deletions UI/desktop_app/manage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python
"""Django's command-line utility for administrative tasks."""
import os
import sys

sys.path.append(os.path.dirname(os.path.abspath(__file__)))


def main():
"""Run administrative tasks."""
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'desktop_app.settings')
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)


if __name__ == '__main__':
main()
39 changes: 39 additions & 0 deletions UI/desktop_app/ui/DB_query/AnyScaleLLM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
from langchain_core.runnables.base import Runnable
import openai
import logging

# Set up logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

class AnyScaleLLM():
def __init__(self, model_name, api_key, base_url="https://api.endpoints.anyscale.com/v1"):
self.base_url = base_url
self.api_key = api_key
self.model_name = model_name

def chat_completion(self, prompt, question):
client = openai.OpenAI(
base_url=self.base_url,
api_key=self.api_key
)

messages = [
{"role": "system", "content": prompt},
{"role": "user", "content": question}
]

try:
chat_completion = client.chat.completions.create(
model=self.model_name,
messages=messages,
temperature=0.1
)

response = chat_completion.choices[0].message.content
logger.debug(f"API Response: {response}")

return response
except Exception as e:
logger.error(f"Error during chat completion: {e}")
return "An error occurred while processing your request."
Empty file.
Loading