File size: 3,749 Bytes
13d9684
1de3299
aa0b46e
0cc0935
 
aa0b46e
5725c5e
 
 
b1f285e
5725c5e
 
 
 
 
 
 
 
e470c22
a5c1b90
 
 
13d9684
 
 
28c44fd
 
13d9684
 
 
e470c22
2984f3a
13d9684
 
 
 
e470c22
0cc0935
e470c22
 
 
 
 
13d9684
 
 
 
 
 
 
 
e470c22
704150d
 
5c90fce
704150d
75c96e9
415a393
b1f285e
 
1993293
13d9684
4d72a21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e4a8b4b
 
 
 
 
 
 
 
 
3d89143
4d72a21
 
9bd9c69
cd92192
13d9684
 
 
 
 
704150d
aa0b46e
13d9684
4d72a21
13d9684
 
704150d
70197f7
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import os
import json
import streamlit as st
from huggingface_hub import InferenceApi, login, InferenceClient


# Streamlit app configuration
st.set_page_config(page_title="Medical Chatbot")
st.title("Medical Chatbot 🤖")
st.subheader("", divider='rainbow')
# Get the Hugging Face token from environment variables
hf_token = os.getenv("HF_TOKEN")
if hf_token is None:
    raise ValueError("Hugging Face token not found. Please set the HF_TOKEN environment variable.")

# Authenticate with Hugging Face
login(hf_token)

# Model information and links
model_links = {
    "Zephyr-7B": "HuggingFaceH4/zephyr-7b-beta"
}
model_info = {
    "Zephyr-7B": {
        'description': """Zephyr 7B is a Huggingface model, fine-tuned for helpful and instructive interactions.""",
        # 'logo': 'https://huggingface.co/HuggingFaceH4/zephyr-7b-gemma-v0.1/resolve/main/thumbnail.png'
        'logo': 'https://huggingface.co/HuggingFaceH4/zephyr-7b-alpha/resolve/main/thumbnail.png'
    }
}

# Sidebar for model selection
st.sidebar.image(model_info['Zephyr-7B']['logo'])
selected_model = st.sidebar.selectbox("Select Model", model_links.keys())
st.sidebar.write(f"You're now chatting with **{selected_model}**")
st.sidebar.markdown(model_info[selected_model]['description'])

# Inference API Initialization
client = InferenceClient('HuggingFaceH4/zephyr-7b-beta')

# Sidebar settings
max_tokens = st.sidebar.slider("Max new tokens", 1, 2048, 512)
temperature = st.sidebar.slider("Temperature", 0.1, 4.0, 0.7)
top_p = st.sidebar.slider("Top-p (nucleus sampling)", 0.1, 1.0, 0.95)

# Reset conversation button
def reset_conversation():
    st.session_state.messages = []
    st.session_state.model = selected_model

st.sidebar.button('Reset Chat', on_click=reset_conversation)

# Initialize conversation and chat history
if 'messages' not in st.session_state:
    st.session_state.messages = [
        {"role": "system", "content": "You are a knowledgeable and empathetic medical assistant providing accurate and compassionate health advice based on user input."}
    ]

# Display chat history
for message in st.session_state.messages:
    with st.chat_message(message["role"]):
        st.markdown(message["content"])

def respond(message, history, max_tokens, temperature, top_p):
    # Prepare the list of messages for the chat completion
    messages = [{"role": "system", "content": st.session_state.messages[0]["content"]}]

    for val in history:
        if val["role"] == "user":
            messages.append({"role": "user", "content": val["content"]})
        elif val["role"] == "assistant":
            messages.append({"role": "assistant", "content": val["content"]})

    messages.append({"role": "user", "content": message})

    # Generate response
    response = ""
    response_container = st.empty()  # Placeholder to update the response text dynamically

    for message in client.chat_completion(
        messages,
        max_tokens=max_tokens,
        stream=True,
        temperature=temperature,
        top_p=top_p,
    ):
        token = message.choices[0].delta.content
        response += token
        response_container.text(response)  # Stream the response

    return response


# User input
if user_input := st.chat_input("Ask a health question..."):
    # Display user message
    with st.chat_message("user"):
        st.markdown(user_input)
    st.session_state.messages.append({"role": "user", "content": user_input})

    # Generate and display assistant response
    response = respond(user_input, st.session_state.messages, max_tokens, temperature, top_p)
    with st.chat_message("assistant"):
        st.markdown(response)
    st.session_state.messages.append({"role": "assistant", "content": response})
    st.rerun()