Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,163 +1,127 @@
|
|
| 1 |
-
########################################
|
| 2 |
# app.py
|
| 3 |
-
########################################
|
| 4 |
import streamlit as st
|
| 5 |
-
from
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
""
|
| 11 |
-
|
| 12 |
-
""
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 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 |
-
#
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
"Max new tokens",
|
| 89 |
min_value=1,
|
| 90 |
max_value=4000,
|
| 91 |
-
value=
|
| 92 |
-
step=
|
| 93 |
)
|
| 94 |
-
|
| 95 |
-
temperature = st.
|
| 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.
|
| 104 |
"Top-p (nucleus sampling)",
|
| 105 |
min_value=0.1,
|
| 106 |
max_value=1.0,
|
| 107 |
value=0.9,
|
| 108 |
-
step=0.
|
| 109 |
)
|
| 110 |
-
|
| 111 |
-
#
|
| 112 |
-
|
| 113 |
-
"
|
| 114 |
-
"
|
|
|
|
| 115 |
)
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 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 |
-
|
| 163 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
+
from huggingface_hub import InferenceClient
|
| 4 |
+
from datetime import datetime
|
| 5 |
+
|
| 6 |
+
# Configure page
|
| 7 |
+
st.set_page_config(
|
| 8 |
+
page_title="DeepSeek Chatbot - ruslanmv.com",
|
| 9 |
+
page_icon="🤖",
|
| 10 |
+
layout="centered",
|
| 11 |
+
initial_sidebar_state="expanded"
|
| 12 |
+
)
|
| 13 |
+
|
| 14 |
+
# Initialize session state
|
| 15 |
+
if "messages" not in st.session_state:
|
| 16 |
+
st.session_state.messages = []
|
| 17 |
+
|
| 18 |
+
# Sidebar controls
|
| 19 |
+
with st.sidebar:
|
| 20 |
+
st.title("🤖 Chatbot Settings")
|
| 21 |
+
st.markdown("Created by [ruslanmv.com](https://ruslanmv.com/)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Model selection
|
| 24 |
+
selected_model = st.selectbox(
|
| 25 |
+
"Choose Model",
|
| 26 |
+
options=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
| 28 |
"deepseek-ai/DeepSeek-R1",
|
| 29 |
"deepseek-ai/DeepSeek-R1-Zero"
|
| 30 |
+
],
|
| 31 |
+
index=0
|
| 32 |
)
|
| 33 |
+
|
| 34 |
+
# System message
|
| 35 |
+
system_message = st.text_area(
|
| 36 |
+
"System Message",
|
| 37 |
+
value="You are a friendly Chatbot created by ruslanmv.com",
|
| 38 |
+
height=100
|
| 39 |
+
)
|
| 40 |
+
|
| 41 |
+
# Generation parameters
|
| 42 |
+
max_new_tokens = st.slider(
|
| 43 |
"Max new tokens",
|
| 44 |
min_value=1,
|
| 45 |
max_value=4000,
|
| 46 |
+
value=512,
|
| 47 |
+
step=50
|
| 48 |
)
|
| 49 |
+
|
| 50 |
+
temperature = st.slider(
|
| 51 |
"Temperature",
|
| 52 |
min_value=0.1,
|
| 53 |
max_value=4.0,
|
| 54 |
value=1.0,
|
| 55 |
step=0.1
|
| 56 |
)
|
| 57 |
+
|
| 58 |
+
top_p = st.slider(
|
| 59 |
"Top-p (nucleus sampling)",
|
| 60 |
min_value=0.1,
|
| 61 |
max_value=1.0,
|
| 62 |
value=0.9,
|
| 63 |
+
step=0.1
|
| 64 |
)
|
| 65 |
+
|
| 66 |
+
# Optional HF Token
|
| 67 |
+
hf_token = st.text_input(
|
| 68 |
+
"HuggingFace Token (optional)",
|
| 69 |
+
type="password",
|
| 70 |
+
help="Enter your HuggingFace token if required for model access"
|
| 71 |
)
|
| 72 |
|
| 73 |
+
# Main chat interface
|
| 74 |
+
st.title("💬 DeepSeek Chatbot")
|
| 75 |
+
st.caption("🚀 A conversational AI powered by DeepSeek models")
|
| 76 |
+
|
| 77 |
+
# Display chat messages
|
| 78 |
+
for message in st.session_state.messages:
|
| 79 |
+
with st.chat_message(message["role"]):
|
| 80 |
+
st.markdown(message["content"])
|
| 81 |
+
if "timestamp" in message:
|
| 82 |
+
st.caption(f"_{message['timestamp']}_")
|
| 83 |
+
|
| 84 |
+
# Chat input and processing
|
| 85 |
+
if prompt := st.chat_input("Type your message..."):
|
| 86 |
+
# Add user message to history
|
| 87 |
+
st.session_state.messages.append({
|
| 88 |
+
"role": "user",
|
| 89 |
+
"content": prompt,
|
| 90 |
+
"timestamp": datetime.now().strftime("%H:%M:%S")
|
| 91 |
+
})
|
| 92 |
+
|
| 93 |
+
# Display user message
|
| 94 |
+
with st.chat_message("user"):
|
| 95 |
+
st.markdown(prompt)
|
| 96 |
+
st.caption(f"_{st.session_state.messages[-1]['timestamp']}_")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
+
# Create full prompt with system message
|
| 99 |
+
full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
|
| 100 |
+
|
| 101 |
+
# Create client and generate response
|
| 102 |
+
client = InferenceClient(model=selected_model, token=hf_token)
|
| 103 |
+
|
| 104 |
+
# Display assistant response
|
| 105 |
+
with st.chat_message("assistant"):
|
| 106 |
+
response = st.write_stream(
|
| 107 |
+
client.text_generation(
|
| 108 |
+
full_prompt,
|
| 109 |
+
max_new_tokens=max_new_tokens,
|
| 110 |
+
temperature=temperature,
|
| 111 |
+
top_p=top_p,
|
| 112 |
+
stream=True
|
| 113 |
+
)
|
| 114 |
+
)
|
| 115 |
+
timestamp = datetime.now().strftime("%H:%M:%S")
|
| 116 |
+
st.caption(f"_{timestamp}_")
|
| 117 |
+
|
| 118 |
+
# Add assistant response to history
|
| 119 |
+
st.session_state.messages.append({
|
| 120 |
+
"role": "assistant",
|
| 121 |
+
"content": response,
|
| 122 |
+
"timestamp": timestamp
|
| 123 |
+
})
|
| 124 |
+
|
| 125 |
+
# Optional debug information
|
| 126 |
+
# st.sidebar.markdown("---")
|
| 127 |
+
# st.sidebar.json(st.session_state.messages)
|