|
import os |
|
import streamlit as st |
|
|
|
|
|
os.system('pip install transformers torch') |
|
|
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
|
|
|
|
st.title("💬 Healthcare Chatbot") |
|
st.write( |
|
"This is a simple chatbot that uses the Llama3-Med42-8B model to generate responses. " |
|
"To use this app, simply type your question in the input field below." |
|
) |
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
model_name = "m42-health/Llama3-Med42-8B" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
model = AutoModelForCausalLM.from_pretrained(model_name) |
|
|
|
|
|
|
|
if user_input := st.chat_input("What is up?"): |
|
|
|
|
|
st.session_state.messages.append({"role": "user", "content": user_input}) |
|
with st.chat_message("user"): |
|
st.markdown(user_input) |
|
|
|
|
|
messages = [ |
|
{"role": "system", "content": ( |
|
"You are a helpful, respectful and honest medical assistant. " |
|
"Always answer as helpfully as possible, while being safe. " |
|
"Your answers should not include any harmful, unethical, racist, sexist, toxic, dangerous, or illegal content. " |
|
"Please ensure that your responses are socially unbiased and positive in nature. " |
|
"If a question does not make any sense, or is not factually coherent, explain why instead of answering something not correct. " |
|
"If you don’t know the answer to a question, please don’t share false information." |
|
)}, |
|
{"role": "user", "content": user_input} |
|
] |
|
input_text = " ".join([f"{message['role']}: {message['content']}" for message in messages]) |
|
input_ids = tokenizer.encode(input_text, return_tensors="pt") |
|
|
|
|
|
output_ids = model.generate(input_ids, max_length=512, do_sample=True, temperature=0.4, top_k=150, top_p=0.75) |
|
response = tokenizer.decode(output_ids[0], skip_special_tokens=True) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
st.markdown(response[len(input_text):]) |
|
st.session_state.messages.append({"role": "assistant", "content": response[len(input_text):]}) |
|
|