parthib07 commited on
Commit
641fab9
Β·
verified Β·
1 Parent(s): 789d4d9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -65
app.py CHANGED
@@ -1,65 +1,65 @@
1
- import streamlit as st
2
- from langchain.prompts import PromptTemplate
3
- from langchain_groq import ChatGroq
4
- from langchain.vectorstores import FAISS
5
- from langchain.embeddings import HuggingFaceEmbeddings
6
- import os
7
- from dotenv import load_dotenv
8
- load_dotenv()
9
-
10
- def initialize_groq_llm():
11
- return ChatGroq(
12
- groq_api_key=os.getenv("GROQ_API_KEY"),
13
- model_name="llama-3.3-70b-versatile",
14
- max_tokens=512
15
- )
16
-
17
- embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
18
- faiss_index = FAISS.load_local(
19
- "medical_faiss_index",
20
- embedding_model,
21
- allow_dangerous_deserialization=True
22
- )
23
-
24
- prompt_template = """
25
- You are a healthcare professional built by Parthib, and you can assist users with health-related issues.
26
- Use the following pieces of information along with the LLM's knowledge to answer the user's question about diseases or healthcare.
27
- If the following pieces provide some information, combine it with your existing knowledge to craft the most accurate and helpful response.
28
- Include relevant details such as home remedies, medications, and other necessary actions in a clear, point-wise manner for quick readability.
29
- If any other related questions arise, just say, "I am a healthcare professional."
30
- If you don't know the answer, just say that you don't know. Don't try to make up an answer.
31
-
32
- Context: {context}
33
- Question: {question}
34
-
35
- Only return the helpful answer below and nothing else.
36
- Helpful answer:
37
- """
38
-
39
- def generate_response(question):
40
-
41
- retriever = faiss_index.as_retriever(search_kwargs={'k': 1})
42
- docs = retriever.get_relevant_documents(question)
43
- context = "\n".join([doc.page_content for doc in docs])
44
-
45
- llm = initialize_groq_llm()
46
- prompt = PromptTemplate(
47
- input_variables=["context", "question"],
48
- template=prompt_template
49
- )
50
- formatted_prompt = prompt.format(context=context, question=question)
51
-
52
- response = llm.invoke(formatted_prompt)
53
- return response.content
54
-
55
- st.set_page_config(page_title="HealthCare ChatBot", page_icon="πŸ€–", layout="centered")
56
- st.header("HealthCare ChatBot πŸ€–")
57
-
58
- user_input = st.text_input("Ask a Healthcare related question:", "")
59
- st.button("Generate Response")
60
- st.spinner('Processing')
61
-
62
- if user_input:
63
- user_input = user_input.lower().strip()
64
- response = generate_response(user_input)
65
- st.write(f"Response: {response}")
 
1
+ import streamlit as st
2
+ from langchain.prompts import PromptTemplate
3
+ from langchain_groq import ChatGroq
4
+ from langchain.vectorstores import FAISS
5
+ from langchain.embeddings import HuggingFaceEmbeddings
6
+ import os
7
+ from dotenv import load_dotenv
8
+ load_dotenv()
9
+
10
+ def initialize_groq_llm():
11
+ return ChatGroq(
12
+ groq_api_key=os.getenv("GROQ_API_KEY"),
13
+ model_name="llama-3.3-70b-versatile",
14
+ max_tokens=512
15
+ )
16
+
17
+ embedding_model = HuggingFaceEmbeddings(model_name="sentence-transformers/all-MiniLM-L6-v2")
18
+ faiss_index = FAISS.load_local(
19
+ "medical_faiss_index",
20
+ embedding_model,
21
+ allow_dangerous_deserialization=True
22
+ )
23
+
24
+ prompt_template = """
25
+ You are a healthcare professional built by Parthib Karak an AI engineering from Institute of Engineering and management,kolkata, and you can assist users with health-related issues.
26
+ Use the following pieces of information along with the LLM's knowledge to answer the user's question about diseases or healthcare.
27
+ If the following pieces provide some information, combine it with your existing knowledge to craft the most accurate and helpful response.
28
+ Include relevant details such as home remedies, medications, and other necessary actions in a clear, point-wise manner for quick readability.
29
+ If any other related questions arise, just say, "I am a healthcare professional.How may i assist you today?"
30
+ If you don't know the answer, just say that you don't know. Don't try to make up an answer.
31
+
32
+ Context: {context}
33
+ Question: {question}
34
+
35
+ Only return the helpful answer below and nothing else.
36
+ Helpful answer:
37
+ """
38
+
39
+ def generate_response(question):
40
+
41
+ retriever = faiss_index.as_retriever(search_kwargs={'k': 1})
42
+ docs = retriever.get_relevant_documents(question)
43
+ context = "\n".join([doc.page_content for doc in docs])
44
+
45
+ llm = initialize_groq_llm()
46
+ prompt = PromptTemplate(
47
+ input_variables=["context", "question"],
48
+ template=prompt_template
49
+ )
50
+ formatted_prompt = prompt.format(context=context, question=question)
51
+
52
+ response = llm.invoke(formatted_prompt)
53
+ return response.content
54
+
55
+ st.set_page_config(page_title="HealthCare ChatBot", page_icon="πŸ€–", layout="centered")
56
+ st.header("HealthCare ChatBot πŸ€–")
57
+
58
+ user_input = st.text_input("Ask a Healthcare related question:", "")
59
+ st.button("Generate Response")
60
+ st.spinner('Processing')
61
+
62
+ if user_input:
63
+ user_input = user_input.lower().strip()
64
+ response = generate_response(user_input)
65
+ st.write(f"Response: {response}")