Spaces:
Runtime error
Runtime error
import os | |
import streamlit as st | |
# Install dependencies if not already installed | |
os.system('pip install transformers torch') | |
from transformers import AutoTokenizer, AutoModelForCausalLM | |
# Show title and description. | |
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." | |
) | |
# Create a session state variable to store the chat messages. This ensures that the | |
# messages persist across reruns. | |
if "messages" not in st.session_state: | |
st.session_state.messages = [] | |
# Display the existing chat messages via `st.chat_message`. | |
for message in st.session_state.messages: | |
with st.chat_message(message["role"]): | |
st.markdown(message["content"]) | |
# Načtení modelu a tokenizeru | |
model_name = "m42-health/Llama3-Med42-8B" | |
tokenizer = AutoTokenizer.from_pretrained(model_name) | |
model = AutoModelForCausalLM.from_pretrained(model_name) | |
# Create a chat input field to allow the user to enter a message. This will display | |
# automatically at the bottom of the page. | |
if user_input := st.chat_input("What is up?"): | |
# Store and display the current prompt. | |
st.session_state.messages.append({"role": "user", "content": user_input}) | |
with st.chat_message("user"): | |
st.markdown(user_input) | |
# Prepare input for the model | |
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") | |
# Vygenerování odpovědi | |
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) | |
# Display the model's response | |
with st.chat_message("assistant"): | |
st.markdown(response[len(input_text):]) | |
st.session_state.messages.append({"role": "assistant", "content": response[len(input_text):]}) | |