Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,109 +1,163 @@
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
#
|
| 5 |
-
st.
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
This is a demo of different DeepSeek models. Select a model, type your message, and click "Submit".
|
| 16 |
-
You can also adjust optional parameters like system message, max new tokens, temperature, and top-p.
|
| 17 |
-
""")
|
| 18 |
-
|
| 19 |
-
# --- Sidebar for Model Selection and Parameters ---
|
| 20 |
-
with st.sidebar:
|
| 21 |
-
st.header("Options")
|
| 22 |
-
model_choice = st.radio(
|
| 23 |
-
"Choose a Model",
|
| 24 |
-
options=["DeepSeek-R1-Distill-Qwen-32B", "DeepSeek-R1", "DeepSeek-R1-Zero"],
|
| 25 |
-
index=1 # Default to "DeepSeek-R1"
|
| 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 |
-
def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
|
| 55 |
-
# Create payload for the model
|
| 56 |
-
payload = {
|
| 57 |
-
"messages": [{"role": "user", "content": input_text}],
|
| 58 |
-
"system": system_message,
|
| 59 |
-
"max_tokens": max_new_tokens,
|
| 60 |
-
"temperature": temperature,
|
| 61 |
-
"top_p": top_p
|
| 62 |
-
}
|
| 63 |
-
|
| 64 |
-
# Run inference using the selected model
|
| 65 |
-
try:
|
| 66 |
-
response = demo(payload) # Use the demo object directly
|
| 67 |
-
if isinstance(response, dict) and "choices" in response:
|
| 68 |
-
assistant_response = response["choices"][0]["message"]["content"]
|
| 69 |
else:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
#
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
# --- Chat Interface ---
|
| 83 |
-
st.header("Chat with DeepSeek")
|
| 84 |
-
|
| 85 |
-
# Display chat history
|
| 86 |
-
for user_msg, assistant_msg in st.session_state.chat_history:
|
| 87 |
-
with st.chat_message("user"):
|
| 88 |
-
st.write(user_msg)
|
| 89 |
-
with st.chat_message("assistant"):
|
| 90 |
-
st.write(assistant_msg)
|
| 91 |
-
|
| 92 |
-
# Input for new message
|
| 93 |
-
input_text = st.chat_input("Type your message here...")
|
| 94 |
-
|
| 95 |
-
# Handle new message submission
|
| 96 |
-
if input_text:
|
| 97 |
-
# Update chat history
|
| 98 |
-
st.session_state.chat_history = chatbot(
|
| 99 |
-
input_text,
|
| 100 |
-
st.session_state.chat_history,
|
| 101 |
-
model_choice,
|
| 102 |
-
system_message,
|
| 103 |
-
max_new_tokens,
|
| 104 |
-
temperature,
|
| 105 |
-
top_p
|
| 106 |
)
|
|
|
|
|
|
|
| 107 |
|
| 108 |
-
#
|
| 109 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
########################################
|
| 2 |
+
# app.py
|
| 3 |
+
########################################
|
| 4 |
import streamlit as st
|
| 5 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
|
| 6 |
+
|
| 7 |
+
# We define a cache to load pipelines for each model only once.
|
| 8 |
+
@st.cache_resource
|
| 9 |
+
def load_text_generation_pipeline(model_name: str):
|
| 10 |
+
"""
|
| 11 |
+
Loads a text-generation pipeline from the Hugging Face Hub.
|
| 12 |
+
"""
|
| 13 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name, use_fast=True)
|
| 14 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 15 |
+
model_name,
|
| 16 |
+
torch_dtype="auto", # or torch.float16 if GPU is available
|
| 17 |
+
device_map="auto" # automatically map layers to available GPU(s)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
)
|
| 19 |
+
text_generation = pipeline(
|
| 20 |
+
"text-generation",
|
| 21 |
+
model=model,
|
| 22 |
+
tokenizer=tokenizer
|
| 23 |
+
)
|
| 24 |
+
return text_generation
|
| 25 |
+
|
| 26 |
+
def generate_response(
|
| 27 |
+
text_generation,
|
| 28 |
+
system_prompt: str,
|
| 29 |
+
conversation_history: list,
|
| 30 |
+
user_query: str,
|
| 31 |
+
max_new_tokens: int,
|
| 32 |
+
temperature: float,
|
| 33 |
+
top_p: float
|
| 34 |
+
):
|
| 35 |
+
"""
|
| 36 |
+
Generates a response from the language model given the system prompt,
|
| 37 |
+
conversation history, and user query with specified parameters.
|
| 38 |
+
"""
|
| 39 |
+
# Construct a prompt that includes the system role, conversation history, and the new user input.
|
| 40 |
+
# Adjust format depending on your model's instructions format.
|
| 41 |
+
# Here we do a simple approach: system prompt + turn-by-turn conversation.
|
| 42 |
+
full_prompt = system_prompt.strip()
|
| 43 |
+
for (speaker, text) in conversation_history:
|
| 44 |
+
if speaker == "user":
|
| 45 |
+
full_prompt += f"\nUser: {text}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
else:
|
| 47 |
+
full_prompt += f"\nAssistant: {text}"
|
| 48 |
+
# Add the new user query
|
| 49 |
+
full_prompt += f"\nUser: {user_query}\nAssistant:"
|
| 50 |
+
|
| 51 |
+
# Use the pipeline to generate text
|
| 52 |
+
outputs = text_generation(
|
| 53 |
+
full_prompt,
|
| 54 |
+
max_new_tokens=max_new_tokens,
|
| 55 |
+
temperature=temperature,
|
| 56 |
+
top_p=top_p,
|
| 57 |
+
do_sample=True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
)
|
| 59 |
+
# The pipeline returns a list of generated sequences; get the text from the first one
|
| 60 |
+
generated_text = outputs[0]["generated_text"]
|
| 61 |
|
| 62 |
+
# Extract just the new answer part from the generated text
|
| 63 |
+
# Since we appended "Assistant:" at the end, the model's response is everything after that
|
| 64 |
+
answer = generated_text.split("Assistant:")[-1].strip()
|
| 65 |
+
return answer
|
| 66 |
+
|
| 67 |
+
def main():
|
| 68 |
+
st.title("Streamlit Chatbot with Model Selection")
|
| 69 |
+
st.markdown(
|
| 70 |
+
"""
|
| 71 |
+
**System message**: You are a friendly Chatbot created by [ruslanmv.com](https://ruslanmv.com)
|
| 72 |
+
Below you can select the model, adjust parameters, and begin chatting!
|
| 73 |
+
"""
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
# Sidebar for model selection and parameters
|
| 77 |
+
st.sidebar.header("Select Model & Parameters")
|
| 78 |
+
model_name = st.sidebar.selectbox(
|
| 79 |
+
"Choose a model:",
|
| 80 |
+
[
|
| 81 |
+
"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
| 82 |
+
"deepseek-ai/DeepSeek-R1",
|
| 83 |
+
"deepseek-ai/DeepSeek-R1-Zero"
|
| 84 |
+
]
|
| 85 |
+
)
|
| 86 |
+
|
| 87 |
+
max_new_tokens = st.sidebar.slider(
|
| 88 |
+
"Max new tokens",
|
| 89 |
+
min_value=1,
|
| 90 |
+
max_value=4000,
|
| 91 |
+
value=1024,
|
| 92 |
+
step=1
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
temperature = st.sidebar.slider(
|
| 96 |
+
"Temperature",
|
| 97 |
+
min_value=0.1,
|
| 98 |
+
max_value=4.0,
|
| 99 |
+
value=1.0,
|
| 100 |
+
step=0.1
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
top_p = st.sidebar.slider(
|
| 104 |
+
"Top-p (nucleus sampling)",
|
| 105 |
+
min_value=0.1,
|
| 106 |
+
max_value=1.0,
|
| 107 |
+
value=0.9,
|
| 108 |
+
step=0.05
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
# The system "role" content
|
| 112 |
+
system_message = (
|
| 113 |
+
"You are a friendly Chatbot created by ruslanmv.com. "
|
| 114 |
+
"You answer user questions in a concise and helpful way."
|
| 115 |
+
)
|
| 116 |
+
|
| 117 |
+
# Load the chosen model
|
| 118 |
+
text_generation_pipeline = load_text_generation_pipeline(model_name)
|
| 119 |
+
|
| 120 |
+
# We'll keep conversation history in session_state
|
| 121 |
+
if "conversation" not in st.session_state:
|
| 122 |
+
st.session_state["conversation"] = [] # List of tuples (speaker, text)
|
| 123 |
+
|
| 124 |
+
# Display conversation so far
|
| 125 |
+
# Each element in st.session_state["conversation"] is ("user" or "assistant", message_text)
|
| 126 |
+
for speaker, text in st.session_state["conversation"]:
|
| 127 |
+
if speaker == "user":
|
| 128 |
+
st.markdown(f"<div style='text-align:left; color:blue'><strong>User:</strong> {text}</div>", unsafe_allow_html=True)
|
| 129 |
+
else:
|
| 130 |
+
st.markdown(f"<div style='text-align:left; color:green'><strong>Assistant:</strong> {text}</div>", unsafe_allow_html=True)
|
| 131 |
+
|
| 132 |
+
# User input text box
|
| 133 |
+
user_input = st.text_input("Your message", "")
|
| 134 |
+
|
| 135 |
+
# When user hits "Send"
|
| 136 |
+
if st.button("Send"):
|
| 137 |
+
if user_input.strip():
|
| 138 |
+
# 1) Add user query to conversation
|
| 139 |
+
st.session_state["conversation"].append(("user", user_input.strip()))
|
| 140 |
+
# 2) Generate a response
|
| 141 |
+
with st.spinner("Thinking..."):
|
| 142 |
+
answer = generate_response(
|
| 143 |
+
text_generation=text_generation_pipeline,
|
| 144 |
+
system_prompt=system_message,
|
| 145 |
+
conversation_history=st.session_state["conversation"],
|
| 146 |
+
user_query=user_input.strip(),
|
| 147 |
+
max_new_tokens=max_new_tokens,
|
| 148 |
+
temperature=temperature,
|
| 149 |
+
top_p=top_p
|
| 150 |
+
)
|
| 151 |
+
# 3) Add assistant answer to conversation
|
| 152 |
+
st.session_state["conversation"].append(("assistant", answer))
|
| 153 |
+
# 4) Rerun to display
|
| 154 |
+
st.experimental_rerun()
|
| 155 |
+
|
| 156 |
+
# Optional: Provide a button to clear the conversation
|
| 157 |
+
if st.button("Clear Conversation"):
|
| 158 |
+
st.session_state["conversation"] = []
|
| 159 |
+
st.experimental_rerun()
|
| 160 |
+
|
| 161 |
+
|
| 162 |
+
if __name__ == "__main__":
|
| 163 |
+
main()
|