Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,43 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
@st.cache_resource
|
| 13 |
def load_model():
|
| 14 |
return Llama.from_pretrained(
|
| 15 |
-
# repo_id="Mykes/med_gemma7b_gguf",
|
| 16 |
-
# filename="*Q4_K_M.gguf",
|
| 17 |
repo_id="Mykes/med_phi3-mini-4k-GGUF",
|
| 18 |
filename="*Q4_K_M.gguf",
|
| 19 |
verbose=False,
|
|
@@ -24,32 +48,44 @@ def load_model():
|
|
| 24 |
|
| 25 |
llm = load_model()
|
| 26 |
|
| 27 |
-
# basic_prompt = "Below is the context which is your conversation history and the last user question. Write a response according the context and question. ### Context: user: Ответь мне на вопрос о моем здоровье. assistant: Конечно! Какой у Вас вопрос? ### Question: {question} ### Response:"
|
| 28 |
basic_prompt = "Q: {question}\nA:"
|
| 29 |
-
input_text = st.text_input('text')
|
| 30 |
-
model_input = basic_prompt.format(question=input_text)
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
max_tokens=None,
|
| 44 |
-
stop=["<end_of_turn>"],
|
| 45 |
-
echo=True,
|
| 46 |
-
stream=True # Enable streaming
|
| 47 |
-
):
|
| 48 |
-
# Append the new token to the generated text
|
| 49 |
-
generated_text += token['choices'][0]['text']
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from llama_cpp import Llama
|
| 3 |
|
| 4 |
+
st.set_page_config(page_title="Chat with AI", page_icon="🤖")
|
| 5 |
+
|
| 6 |
+
# Custom CSS for better styling
|
| 7 |
+
st.markdown("""
|
| 8 |
+
<style>
|
| 9 |
+
.stTextInput > div > div > input {
|
| 10 |
+
background-color: #f0f2f6;
|
| 11 |
+
}
|
| 12 |
+
.chat-message {
|
| 13 |
+
padding: 1.5rem; border-radius: 0.5rem; margin-bottom: 1rem; display: flex
|
| 14 |
+
}
|
| 15 |
+
.chat-message.user {
|
| 16 |
+
background-color: #2b313e
|
| 17 |
+
}
|
| 18 |
+
.chat-message.bot {
|
| 19 |
+
background-color: #475063
|
| 20 |
+
}
|
| 21 |
+
.chat-message .avatar {
|
| 22 |
+
width: 20%;
|
| 23 |
+
}
|
| 24 |
+
.chat-message .avatar img {
|
| 25 |
+
max-width: 78px;
|
| 26 |
+
max-height: 78px;
|
| 27 |
+
border-radius: 50%;
|
| 28 |
+
object-fit: cover;
|
| 29 |
+
}
|
| 30 |
+
.chat-message .message {
|
| 31 |
+
width: 80%;
|
| 32 |
+
padding: 0 1.5rem;
|
| 33 |
+
color: #fff;
|
| 34 |
+
}
|
| 35 |
+
</style>
|
| 36 |
+
""", unsafe_allow_html=True)
|
| 37 |
+
|
| 38 |
@st.cache_resource
|
| 39 |
def load_model():
|
| 40 |
return Llama.from_pretrained(
|
|
|
|
|
|
|
| 41 |
repo_id="Mykes/med_phi3-mini-4k-GGUF",
|
| 42 |
filename="*Q4_K_M.gguf",
|
| 43 |
verbose=False,
|
|
|
|
| 48 |
|
| 49 |
llm = load_model()
|
| 50 |
|
|
|
|
| 51 |
basic_prompt = "Q: {question}\nA:"
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# Initialize chat history
|
| 54 |
+
if "messages" not in st.session_state:
|
| 55 |
+
st.session_state.messages = []
|
| 56 |
+
|
| 57 |
+
# Display chat messages from history on app rerun
|
| 58 |
+
for message in st.session_state.messages:
|
| 59 |
+
with st.chat_message(message["role"]):
|
| 60 |
+
st.markdown(message["content"])
|
| 61 |
+
|
| 62 |
+
# React to user input
|
| 63 |
+
if prompt := st.chat_input("What is your question?"):
|
| 64 |
+
# Display user message in chat message container
|
| 65 |
+
st.chat_message("user").markdown(prompt)
|
| 66 |
+
# Add user message to chat history
|
| 67 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 68 |
+
|
| 69 |
+
model_input = basic_prompt.format(question=prompt)
|
| 70 |
|
| 71 |
+
# Display assistant response in chat message container
|
| 72 |
+
with st.chat_message("assistant"):
|
| 73 |
+
message_placeholder = st.empty()
|
| 74 |
+
full_response = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
+
for token in llm(
|
| 77 |
+
model_input,
|
| 78 |
+
max_tokens=None,
|
| 79 |
+
stop=["<end_of_turn>"],
|
| 80 |
+
echo=True,
|
| 81 |
+
stream=True
|
| 82 |
+
):
|
| 83 |
+
full_response += token['choices'][0]['text']
|
| 84 |
+
message_placeholder.markdown(full_response + "▌")
|
| 85 |
+
message_placeholder.markdown(full_response)
|
| 86 |
+
|
| 87 |
+
# Add assistant response to chat history
|
| 88 |
+
st.session_state.messages.append({"role": "assistant", "content": full_response})
|
| 89 |
|
| 90 |
+
st.sidebar.title("Chat with AI")
|
| 91 |
+
st.sidebar.markdown("This is a simple chat interface using Streamlit and an AI model.")
|