yash001010 commited on
Commit
e06d532
·
verified ·
1 Parent(s): 4347aac

Upload 3 files

Browse files
Files changed (3) hide show
  1. Embedded_Med_books/chroma.sqlite3 +0 -0
  2. app.py +144 -0
  3. requirements.txt +15 -0
Embedded_Med_books/chroma.sqlite3 ADDED
Binary file (168 kB). View file
 
app.py ADDED
@@ -0,0 +1,144 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import streamlit as st
3
+ from langchain_community.vectorstores import Chroma
4
+ from langchain_community.embeddings import HuggingFaceBgeEmbeddings
5
+ from groq import Groq
6
+ from dotenv import load_dotenv
7
+
8
+ # Initialize Streamlit page configuration
9
+ st.set_page_config(page_title="Medical Knowledge Assistant", layout="wide")
10
+
11
+ # Add API key input in sidebar
12
+ with st.sidebar:
13
+ st.header("API Key Configuration")
14
+ api_key = st.text_input("Enter your Groq API Key:", type="password")
15
+ if api_key:
16
+ os.environ['GROQ_API_KEY'] = api_key
17
+ else:
18
+ # Try loading from .env file
19
+ load_dotenv()
20
+ api_key = os.getenv("GROQ_API_KEY")
21
+ if api_key:
22
+ st.success("API Key loaded from .env file")
23
+
24
+ # Check for API key before proceeding
25
+ if not api_key:
26
+ st.warning("Please enter your Groq API key in the sidebar to continue.")
27
+ st.stop()
28
+
29
+ # Initialize the app
30
+ st.title("Medical Knowledge Assistant")
31
+
32
+ try:
33
+ # Set up the embeddings
34
+ model_name = "BAAI/bge-large-en"
35
+ model_kwargs = {'device': 'cpu'}
36
+ encode_kwargs = {'normalize_embeddings': False}
37
+ embeddings = HuggingFaceBgeEmbeddings(
38
+ model_name=model_name,
39
+ model_kwargs=model_kwargs,
40
+ encode_kwargs=encode_kwargs
41
+ )
42
+
43
+ # Load the vector store from the local drive
44
+ script_dir = os.path.dirname(os.path.abspath(__file__))
45
+ persist_directory = os.path.join(script_dir, 'Embedded_Med_books')
46
+
47
+ # Debug information
48
+ st.sidebar.header("Debug Information")
49
+ st.sidebar.write("Vector store path:", persist_directory)
50
+
51
+ with st.sidebar:
52
+ st.write("API Key Loaded:", "Yes" if api_key else "No")
53
+
54
+ # Check vector store directory
55
+ if not os.path.exists(persist_directory):
56
+ st.error(f"Vector store directory not found at: {persist_directory}")
57
+ if st.button("Create Directory"):
58
+ os.makedirs(persist_directory)
59
+ st.success("Directory created!")
60
+
61
+ # Load the vector store
62
+ vector_store = Chroma(
63
+ persist_directory=persist_directory,
64
+ embedding_function=embeddings
65
+ )
66
+ retriever = vector_store.as_retriever(search_kwargs={'k': 1})
67
+
68
+ # Initialize Groq client
69
+ client = Groq(api_key=api_key)
70
+
71
+ # Streamlit input
72
+ query = st.text_input("Enter your medical question here:")
73
+
74
+ def query_with_groq(query, retriever):
75
+ try:
76
+ # Retrieve relevant documents
77
+ docs = retriever.get_relevant_documents(query)
78
+ context = "\n".join([doc.page_content for doc in docs])
79
+
80
+ # Call the Groq API with the query and context
81
+ completion = client.chat.completions.create(
82
+ model="llama3-70b-8192",
83
+ messages=[
84
+ {
85
+ "role": "system",
86
+ "content": (
87
+ "You are a knowledgeable medical assistant. For any medical term or disease, include comprehensive information covering: "
88
+ "definitions, types, historical background, major theories, known causes, and contributing risk factors. "
89
+ "Explain the genesis or theories on its origin, if applicable. Use a structured, thorough approach and keep language accessible. "
90
+ "provide symptoms, diagnosis, and treatment and post operative care , address all with indepth explanation , with specific details and step-by-step processes where relevant. "
91
+ "If the context does not adequately cover the user's question, respond with: 'I cannot provide an answer based on the available medical dataset.'"
92
+ )
93
+ },
94
+ {
95
+ "role": "system",
96
+ "content": (
97
+ "If the user asks for a medical explanation, ensure accuracy, don't include layman's terms if complex terms are used, "
98
+ "and organize responses in a structured way."
99
+ )
100
+ },
101
+ {
102
+ "role": "system",
103
+ "content": (
104
+ "When comparing two terms or conditions, provide a clear, concise, and structured comparison. Highlight key differences in their "
105
+ "definitions, symptoms, causes, diagnoses, and treatments with indepth explanation of each. If relevant, include any overlapping characteristics."
106
+ )
107
+ },
108
+ {
109
+ "role": "user",
110
+ "content": f"{context}\n\nQ: {query}\nA:"
111
+ }
112
+ ],
113
+ temperature=0.7,
114
+ max_tokens=3000,
115
+ stream=True
116
+ )
117
+
118
+ # Create a placeholder for streaming response
119
+ response_container = st.empty()
120
+ response = ""
121
+
122
+ # Stream the response
123
+ for chunk in completion:
124
+ if chunk.choices[0].delta.content:
125
+ response += chunk.choices[0].delta.content
126
+ response_container.markdown(response)
127
+
128
+ return response
129
+
130
+ except Exception as e:
131
+ st.error(f"Error during query processing: {str(e)}")
132
+ return None
133
+
134
+ if st.button("Get Answer"):
135
+ if query:
136
+ with st.spinner("Processing your query..."):
137
+ answer = query_with_groq(query, retriever)
138
+ if answer:
139
+ st.success("Query processed successfully!")
140
+ else:
141
+ st.warning("Please enter a query.")
142
+
143
+ except Exception as e:
144
+ st.error(f"Initialization error: {str(e)}")
requirements.txt ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ streamlit==1.31.0
2
+ langchain-community==0.0.16
3
+ groq==0.4.5
4
+ python-dotenv==1.0.1
5
+ chromadb==0.4.22
6
+ sentence-transformers==2.5.1
7
+ transformers==4.37.2
8
+ torch==2.2.0
9
+ pydantic==2.6.1
10
+ typing-inspect==0.9.0
11
+ typing_extensions==4.9.0
12
+ numpy==1.26.3
13
+ pandas==2.2.0
14
+ tqdm==4.66.1
15
+ requests==2.31.0