Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,64 +1,112 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
-
For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
|
| 6 |
-
"""
|
| 7 |
-
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
temperature,
|
| 16 |
-
top_p,
|
| 17 |
-
):
|
| 18 |
-
messages = [{"role": "system", "content": system_message}]
|
| 19 |
|
| 20 |
-
|
| 21 |
-
if val[0]:
|
| 22 |
-
messages.append({"role": "user", "content": val[0]})
|
| 23 |
-
if val[1]:
|
| 24 |
-
messages.append({"role": "assistant", "content": val[1]})
|
| 25 |
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
top_p=top_p,
|
| 36 |
-
):
|
| 37 |
-
token = message.choices[0].delta.content
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
""
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
minimum=0.1,
|
| 54 |
-
maximum=1.0,
|
| 55 |
-
value=0.95,
|
| 56 |
-
step=0.05,
|
| 57 |
-
label="Top-p (nucleus sampling)",
|
| 58 |
-
),
|
| 59 |
-
],
|
| 60 |
-
)
|
| 61 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from textblob import TextBlob
|
| 4 |
+
from langchain.prompts import PromptTemplate
|
| 5 |
+
from dotenv import load_dotenv
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from langchain_groq import ChatGroq
|
| 8 |
+
from langchain_core.prompts import PromptTemplate
|
| 9 |
+
from langchain_core.output_parsers import StrOutputParser
|
| 10 |
+
from langchain_chroma import Chroma
|
| 11 |
+
from langchain_huggingface import HuggingFaceEmbeddings
|
| 12 |
|
| 13 |
+
# Load environment variables
|
| 14 |
+
load_dotenv()
|
| 15 |
+
|
| 16 |
+
# Load the dataset
|
| 17 |
+
df = pd.read_csv('./drugs_side_effects_drugs_com.csv')
|
| 18 |
+
df = df[['drug_name', 'medical_condition', 'side_effects']]
|
| 19 |
+
df.dropna(inplace=True)
|
| 20 |
+
|
| 21 |
+
# Prepare context data for vector store
|
| 22 |
+
context_data = [" | ".join([f"{col}: {df.iloc[i][col]}" for col in df.columns]) for i in range(2)]
|
| 23 |
+
|
| 24 |
+
# Set up Groq LLM and vector store
|
| 25 |
+
groq_key = os.environ.get('gloq_key')
|
| 26 |
+
llm = ChatGroq(model="llama-3.1-70b-versatile", api_key=groq_key)
|
| 27 |
+
embed_model = HuggingFaceEmbeddings(model_name="mixedbread-ai/mxbai-embed-large-v1")
|
| 28 |
+
vectorstore = Chroma(
|
| 29 |
+
collection_name="medical_dataset_store",
|
| 30 |
+
embedding_function=embed_model,
|
| 31 |
+
persist_directory="./"
|
| 32 |
+
)
|
| 33 |
+
vectorstore.add_texts(context_data)
|
| 34 |
+
retriever = vectorstore.as_retriever()
|
| 35 |
+
|
| 36 |
+
# Define prompt template
|
| 37 |
+
SYSTEM_PROMPT_GENERAL = """
|
| 38 |
+
You are CareBot, a pharmacist and medical expert known as Treasure. Your goal is to provide empathetic, supportive, and detailed responses tailored to the user's needs.
|
| 39 |
+
Behavior Guidelines:
|
| 40 |
+
1. Introduction: Greet the user as Treasure during the first interaction.
|
| 41 |
+
2. Personalization: Adapt responses to the user's tone and emotional state.
|
| 42 |
+
3. Empathy: Respond warmly to the user's concerns and questions.
|
| 43 |
+
4. Evidence-Based: Use reliable sources to answer queries. For missing data, advise seeking professional consultation.
|
| 44 |
+
5. Focus: Avoid providing off-topic information; address the user's query specifically.
|
| 45 |
+
6. Encouragement: Balance acknowledging concerns with actionable and constructive suggestions.
|
| 46 |
+
7. Context Integration: Use the given context to deliver accurate and relevant answers without repeating the context explicitly.
|
| 47 |
+
|
| 48 |
+
Objective:
|
| 49 |
+
Deliver thoughtful, empathetic, and medically sound advice based on the user’s query.
|
| 50 |
+
|
| 51 |
+
Response Style:
|
| 52 |
+
- Detailed but concise
|
| 53 |
+
- Professional, empathetic tone
|
| 54 |
+
- Clear and actionable guidance
|
| 55 |
"""
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
+
rag_prompt_template = PromptTemplate(
|
| 58 |
+
input_variables=["context", "user_input"],
|
| 59 |
+
template="""
|
| 60 |
+
{system_prompt}
|
| 61 |
|
| 62 |
+
Context: {context}
|
| 63 |
+
|
| 64 |
+
User: {user_input}
|
| 65 |
+
Assistant:"""
|
| 66 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
+
st.title("CareBot: Your AI Medical Assistant")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
# Initialize session state for chat history
|
| 71 |
+
if "messages" not in st.session_state:
|
| 72 |
+
st.session_state["messages"] = [
|
| 73 |
+
{"role": "assistant", "content": "Hi there! I'm Treasure, your friendly pharmacist. How can I help you today?"}
|
| 74 |
+
]
|
| 75 |
|
| 76 |
+
# Display chat history
|
| 77 |
+
for msg in st.session_state.messages:
|
| 78 |
+
st.chat_message(msg["role"]).write(msg["content"])
|
| 79 |
|
| 80 |
+
# User input
|
| 81 |
+
if user_query := st.chat_input("Ask me a medical question, or share your concerns."):
|
| 82 |
+
# Add user message to the session state
|
| 83 |
+
st.session_state.messages.append({"role": "user", "content": user_query})
|
| 84 |
+
st.chat_message("user").write(user_query)
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
# Perform sentiment analysis
|
| 87 |
+
sentiment = TextBlob(user_query).sentiment.polarity
|
| 88 |
|
| 89 |
+
# Modify prompt based on sentiment
|
| 90 |
+
system_prompt = SYSTEM_PROMPT_GENERAL
|
| 91 |
+
if sentiment < 0:
|
| 92 |
+
system_prompt += "\nThe user seems upset or worried. Prioritize empathy and reassurance."
|
| 93 |
|
| 94 |
+
# Retrieve context from vector store
|
| 95 |
+
context_results = retriever.get_relevant_documents(user_query)
|
| 96 |
+
context = "\n".join([result.page_content for result in context_results])
|
| 97 |
+
|
| 98 |
+
# Format the prompt
|
| 99 |
+
formatted_prompt = rag_prompt_template.format(
|
| 100 |
+
system_prompt=system_prompt,
|
| 101 |
+
context=context,
|
| 102 |
+
user_input=user_query
|
| 103 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
# Generate response using Groq LLM
|
| 106 |
+
response = ""
|
| 107 |
+
for text in llm.stream(formatted_prompt):
|
| 108 |
+
response += text
|
| 109 |
|
| 110 |
+
# Add assistant response to the session state
|
| 111 |
+
st.session_state.messages.append({"role": "assistant", "content": response.strip()})
|
| 112 |
+
st.chat_message("assistant").write(response.strip())
|