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 1 |
+
import streamlit as st
|
| 2 |
+
import PyPDF2
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
|
| 5 |
+
# Initialize the Inference Client
|
| 6 |
+
client = InferenceClient("meta-llama/Llama-3.2-3B-Instruct")
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def respond(
|
| 10 |
+
message,
|
| 11 |
+
history: list[tuple[str, str]],
|
| 12 |
+
system_message,
|
| 13 |
+
max_tokens,
|
| 14 |
+
temperature,
|
| 15 |
+
top_p,
|
| 16 |
+
uploaded_pdf=None
|
| 17 |
+
):
|
| 18 |
+
messages = [{"role": "system", "content": system_message}]
|
| 19 |
+
|
| 20 |
+
# Add previous conversation history to the messages
|
| 21 |
+
for val in history:
|
| 22 |
+
if val[0]:
|
| 23 |
+
messages.append({"role": "user", "content": val[0]})
|
| 24 |
+
if val[1]:
|
| 25 |
+
messages.append({"role": "assistant", "content": val[1]})
|
| 26 |
+
|
| 27 |
+
# If a new message is entered, add it to the conversation history
|
| 28 |
+
messages.append({"role": "user", "content": message})
|
| 29 |
+
|
| 30 |
+
# If a PDF is uploaded, process its content
|
| 31 |
+
if uploaded_pdf is not None:
|
| 32 |
+
file_content = extract_pdf_text(uploaded_pdf)
|
| 33 |
+
if file_content:
|
| 34 |
+
messages.append({"role": "user", "content": f"Document Content: {file_content}"})
|
| 35 |
+
|
| 36 |
+
# Get response from the model
|
| 37 |
+
response = ""
|
| 38 |
+
for message in client.chat_completion(
|
| 39 |
+
messages,
|
| 40 |
+
max_tokens=max_tokens,
|
| 41 |
+
stream=True,
|
| 42 |
+
temperature=temperature,
|
| 43 |
+
top_p=top_p,
|
| 44 |
+
):
|
| 45 |
+
token = message.choices[0].delta.content
|
| 46 |
+
response += token
|
| 47 |
+
yield response
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def extract_pdf_text(file):
|
| 51 |
+
"""Extract text from a PDF file."""
|
| 52 |
+
try:
|
| 53 |
+
reader = PyPDF2.PdfReader(file)
|
| 54 |
+
text = ""
|
| 55 |
+
for page in reader.pages:
|
| 56 |
+
text += page.extract_text()
|
| 57 |
+
return text
|
| 58 |
+
except Exception as e:
|
| 59 |
+
return f"Error extracting text from PDF: {str(e)}"
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
# Streamlit UI
|
| 63 |
+
st.set_page_config(page_title="Health Assistant", layout="wide")
|
| 64 |
+
|
| 65 |
+
# Custom CSS for Streamlit app
|
| 66 |
+
st.markdown(
|
| 67 |
+
"""
|
| 68 |
+
<style>
|
| 69 |
+
body {
|
| 70 |
+
background-color: #1e2a38; /* Dark blue background */
|
| 71 |
+
color: #ffffff; /* White text for readability */
|
| 72 |
+
font-family: 'Arial', sans-serif; /* Clean and modern font */
|
| 73 |
+
}
|
| 74 |
+
.stButton button {
|
| 75 |
+
background-color: #42B3CE !important; /* Light blue button */
|
| 76 |
+
color: #2e3b4e !important; /* Dark text for contrast */
|
| 77 |
+
border: none !important;
|
| 78 |
+
padding: 10px 20px !important;
|
| 79 |
+
border-radius: 8px !important;
|
| 80 |
+
font-size: 16px;
|
| 81 |
+
font-weight: bold;
|
| 82 |
+
transition: background-color 0.3s ease, transform 0.2s ease;
|
| 83 |
+
}
|
| 84 |
+
.stButton button:hover {
|
| 85 |
+
background-color: #3189A2 !important; /* Darker blue on hover */
|
| 86 |
+
transform: scale(1.05);
|
| 87 |
+
}
|
| 88 |
+
.stTextInput input {
|
| 89 |
+
background-color: #2f3b4d;
|
| 90 |
+
color: white;
|
| 91 |
+
border: 2px solid #42B3CE;
|
| 92 |
+
padding: 12px;
|
| 93 |
+
border-radius: 8px;
|
| 94 |
+
font-size: 16px;
|
| 95 |
+
transition: border 0.3s ease;
|
| 96 |
+
}
|
| 97 |
+
.stTextInput input:focus {
|
| 98 |
+
border-color: #3189A2;
|
| 99 |
+
}
|
| 100 |
+
</style>
|
| 101 |
+
""",
|
| 102 |
+
unsafe_allow_html=True,
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Title and description
|
| 106 |
+
st.title("Health Assistant Chat")
|
| 107 |
+
st.subheader("Chat with your health assistant and upload a document for analysis")
|
| 108 |
+
|
| 109 |
+
# System message for health-related responses
|
| 110 |
+
system_message = (
|
| 111 |
+
"You are a virtual health assistant designed to provide accurate and reliable information "
|
| 112 |
+
"related to health, wellness, and medical topics. Your primary goal is to assist users with "
|
| 113 |
+
"their health-related queries, offer general guidance, and suggest when to consult a licensed "
|
| 114 |
+
"medical professional. If a user asks a question that is unrelated to health, wellness, or medical "
|
| 115 |
+
"topics, respond politely but firmly with: 'I'm sorry, I can't help with that because I am a virtual "
|
| 116 |
+
"health assistant designed to assist with health-related needs. Please let me know if you have any health-related questions.'"
|
| 117 |
+
)
|
| 118 |
+
|
| 119 |
+
# Upload a PDF file
|
| 120 |
+
uploaded_pdf = st.file_uploader("Upload a PDF file (Optional)", type="pdf")
|
| 121 |
+
|
| 122 |
+
# User input message
|
| 123 |
+
message = st.text_input("Type your health-related question:")
|
| 124 |
+
|
| 125 |
+
# History for conversation tracking
|
| 126 |
+
if 'history' not in st.session_state:
|
| 127 |
+
st.session_state['history'] = []
|
| 128 |
+
|
| 129 |
+
# Collect and display previous conversation history
|
| 130 |
+
history = st.session_state['history']
|
| 131 |
+
for user_message, assistant_message in history:
|
| 132 |
+
st.markdown(f"**You:** {user_message}")
|
| 133 |
+
st.markdown(f"**Assistant:** {assistant_message}")
|
| 134 |
+
|
| 135 |
+
# Max tokens, temperature, and top-p sliders
|
| 136 |
+
max_tokens = st.slider("Max new tokens", min_value=1, max_value=2048, value=512)
|
| 137 |
+
temperature = st.slider("Temperature", min_value=0.1, max_value=4.0, value=0.7, step=0.1)
|
| 138 |
+
top_p = st.slider("Top-p (nucleus sampling)", min_value=0.1, max_value=1.0, value=0.95, step=0.05)
|
| 139 |
+
|
| 140 |
+
# Button to generate response
|
| 141 |
+
if st.button("Generate Response"):
|
| 142 |
+
if message:
|
| 143 |
+
# Append the user's question to the conversation history
|
| 144 |
+
st.session_state.history.append((message, ""))
|
| 145 |
+
# Generate the response based on the user's input and any uploaded document
|
| 146 |
+
response = respond(
|
| 147 |
+
message,
|
| 148 |
+
st.session_state.history,
|
| 149 |
+
system_message,
|
| 150 |
+
max_tokens,
|
| 151 |
+
temperature,
|
| 152 |
+
top_p,
|
| 153 |
+
uploaded_pdf
|
| 154 |
+
)
|
| 155 |
+
# Display the response
|
| 156 |
+
for resp in response:
|
| 157 |
+
st.markdown(f"**Assistant:** {resp}")
|
| 158 |
+
# Update the conversation history with the assistant's response
|
| 159 |
+
st.session_state.history[-1] = (message, resp)
|
| 160 |
+
else:
|
| 161 |
+
st.error("Please enter a question to proceed.")
|