Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,102 +1,118 @@
|
|
| 1 |
# app.py
|
|
|
|
| 2 |
import streamlit as st
|
| 3 |
-
from models import
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
st.
|
| 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 |
-
max_value=1.0,
|
| 59 |
-
value=0.9,
|
| 60 |
-
step=0.1
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
# Main chat interface
|
| 64 |
-
st.title("🤖 DeepSeek Chatbot")
|
| 65 |
-
st.caption("Powered by ruslanmv.com - Choose your model and parameters in the sidebar")
|
| 66 |
-
|
| 67 |
-
# Display chat messages
|
| 68 |
-
for message in st.session_state.messages:
|
| 69 |
-
with st.chat_message(message["role"]):
|
| 70 |
-
st.markdown(message["content"])
|
| 71 |
-
|
| 72 |
-
# Chat input
|
| 73 |
-
if prompt := st.chat_input("Type your message..."):
|
| 74 |
-
# Add user message to chat history
|
| 75 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 76 |
-
|
| 77 |
-
# Display user message
|
| 78 |
-
with st.chat_message("user"):
|
| 79 |
-
st.markdown(prompt)
|
| 80 |
-
|
| 81 |
-
# Prepare full prompt with system message
|
| 82 |
-
full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
|
| 83 |
-
|
| 84 |
-
try:
|
| 85 |
-
# Generate response using selected model
|
| 86 |
-
with st.spinner("Generating response..."):
|
| 87 |
-
response = demo.fn(
|
| 88 |
-
full_prompt,
|
| 89 |
-
max_new_tokens=max_tokens,
|
| 90 |
-
temperature=temperature,
|
| 91 |
-
top_p=top_p
|
| 92 |
-
)
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
st.session_state.
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
|
| 3 |
import streamlit as st
|
| 4 |
+
from models import demo_qwen, demo_r1, demo_zero
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="DeepSeek Chatbot", layout="centered")
|
| 7 |
+
|
| 8 |
+
# A helper function to pick the correct Gradio interface
|
| 9 |
+
def select_demo(model_name):
|
| 10 |
+
if model_name == "DeepSeek-R1-Distill-Qwen-32B":
|
| 11 |
+
return demo_qwen
|
| 12 |
+
elif model_name == "DeepSeek-R1":
|
| 13 |
+
return demo_r1
|
| 14 |
+
elif model_name == "DeepSeek-R1-Zero":
|
| 15 |
+
return demo_zero
|
| 16 |
+
else:
|
| 17 |
+
return demo_qwen # default fallback
|
| 18 |
|
| 19 |
+
# Title of the Streamlit app
|
| 20 |
+
st.title("DeepSeek Chatbot")
|
| 21 |
+
|
| 22 |
+
# Sidebar or main area for parameter selection
|
| 23 |
+
st.subheader("Model and Parameters")
|
| 24 |
+
|
| 25 |
+
model_name = st.selectbox(
|
| 26 |
+
"Select Model",
|
| 27 |
+
["DeepSeek-R1-Distill-Qwen-32B", "DeepSeek-R1", "DeepSeek-R1-Zero"]
|
| 28 |
)
|
| 29 |
|
| 30 |
+
# Optional parameter: System message
|
| 31 |
+
system_message = st.text_area(
|
| 32 |
+
"System Message",
|
| 33 |
+
value="You are a friendly Chatbot created by ruslanmv.com",
|
| 34 |
+
height=80
|
| 35 |
+
)
|
| 36 |
+
|
| 37 |
+
# Optional parameter: max new tokens
|
| 38 |
+
max_new_tokens = st.slider(
|
| 39 |
+
"Max new tokens",
|
| 40 |
+
min_value=1,
|
| 41 |
+
max_value=4000,
|
| 42 |
+
value=512,
|
| 43 |
+
step=1
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
# Optional parameter: temperature
|
| 47 |
+
temperature = st.slider(
|
| 48 |
+
"Temperature",
|
| 49 |
+
min_value=0.10,
|
| 50 |
+
max_value=4.00,
|
| 51 |
+
value=0.80,
|
| 52 |
+
step=0.05
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
# Optional parameter: top-p
|
| 56 |
+
top_p = st.slider(
|
| 57 |
+
"Top-p (nucleus sampling)",
|
| 58 |
+
min_value=0.10,
|
| 59 |
+
max_value=1.00,
|
| 60 |
+
value=0.90,
|
| 61 |
+
step=0.05
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# A text area for user input
|
| 65 |
+
st.subheader("Chat")
|
| 66 |
+
user_prompt = st.text_area("Your message:", value="", height=100)
|
| 67 |
+
|
| 68 |
+
if "chat_history" not in st.session_state:
|
| 69 |
+
st.session_state["chat_history"] = []
|
| 70 |
+
|
| 71 |
+
# Button to send user prompt
|
| 72 |
+
if st.button("Send"):
|
| 73 |
+
if user_prompt.strip():
|
| 74 |
+
# Retrieve the correct Gradio demo
|
| 75 |
+
demo = select_demo(model_name)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# Here we assume the Gradio interface has a function signature for `.predict()`
|
| 78 |
+
# that accepts text plus generation parameters in some order.
|
| 79 |
+
# Many huggingface-style Gradio demos simply take a single text prompt,
|
| 80 |
+
# but it depends entirely on how `demo` is defined in your Gradio code.
|
| 81 |
+
#
|
| 82 |
+
# If the interface has multiple inputs in a specific order, you might do something like:
|
| 83 |
+
# response = demo.predict(system_message, user_prompt, max_new_tokens, temperature, top_p)
|
| 84 |
+
#
|
| 85 |
+
# Or if it only expects a single string, you might combine them:
|
| 86 |
+
# combined_prompt = f"System: {system_message}\nUser: {user_prompt}"
|
| 87 |
+
# response = demo.predict(combined_prompt, max_new_tokens, temperature, top_p)
|
| 88 |
+
#
|
| 89 |
+
# The exact call depends on your Gradio block's input signature.
|
| 90 |
+
|
| 91 |
+
# For illustrative purposes, let's assume a simple signature:
|
| 92 |
+
# demo.predict(prompt: str, max_new_tokens: int, temperature: float, top_p: float)
|
| 93 |
+
# and we inject the system message on top:
|
| 94 |
+
|
| 95 |
+
combined_prompt = f"{system_message}\n\nUser: {user_prompt}"
|
| 96 |
+
|
| 97 |
+
try:
|
| 98 |
+
response = demo.predict(
|
| 99 |
+
combined_prompt,
|
| 100 |
+
max_new_tokens,
|
| 101 |
+
temperature,
|
| 102 |
+
top_p
|
| 103 |
+
)
|
| 104 |
+
except Exception as e:
|
| 105 |
+
response = f"Error: {e}"
|
| 106 |
|
| 107 |
+
st.session_state["chat_history"].append(("User", user_prompt))
|
| 108 |
+
st.session_state["chat_history"].append(("Assistant", response))
|
| 109 |
+
st.experimental_rerun()
|
| 110 |
+
|
| 111 |
+
# Display conversation
|
| 112 |
+
if st.session_state["chat_history"]:
|
| 113 |
+
for role, text in st.session_state["chat_history"]:
|
| 114 |
+
if role == "User":
|
| 115 |
+
st.markdown(f"**{role}:** {text}")
|
| 116 |
+
else:
|
| 117 |
+
st.markdown(f"**{role}:** {text}")
|
| 118 |
+
|