File size: 1,546 Bytes
d1a5ff0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
from transformers import AutoTokenizer, AutoModelForCausalLM

# Načtení modelu a tokenizeru
model_name = "m42-health/Llama3-Med42-8B"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)

st.title('Healthcare Chatbot')

# Uživatelský vstup
user_input = st.text_input("You:", "")
if 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")

    # 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)

    st.text_area("Bot:", response[len(input_text):])