File size: 2,732 Bytes
b845591
d1a5ff0
b845591
 
 
 
d1a5ff0
 
5a6f959
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d1a5ff0
 
 
 
 
5a6f959
 
 
 
 
 
 
 
d1a5ff0
5a6f959
d1a5ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a6f959
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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):]})