Update app.py
Browse files12345678910111213141516171819202122232425262728299 unmodified lines394041424344454647484950515253545556574 unmodified lines6263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127# app.pyimport streamlit as stfrom huggingface_hub import InferenceClientfrom datetime import datetime# Configure pagest.set_page_config(page_title="DeepSeek Chatbot - ruslanmv.com",page_icon="🤖",layout="centered",initial_sidebar_state="expanded")# Initialize session stateif "messages" not in st.session_state:st.session_state.messages = []# Sidebar controlswith st.sidebar:st.title("🤖 Chatbot Settings")st.markdown("Created by [ruslanmv.com](https://ruslanmv.com/)")# Model selectionselected_model = st.selectbox("Choose Model",options=["deepseek-ai/DeepSeek-R1-Distill-Qwen-32B","deepseek-ai/DeepSeek-R1","deepseek-ai/DeepSeek-R1-Zero"9 unmodified lines)# Generation parametersmax_new_tokens = st.slider("Max new tokens",min_value=1,max_value=4000,value=512,step=50)temperature = st.slider("Temperature",min_value=0.1,max_value=4.0,value=1.0,step=0.1)4 unmodified linesvalue=0.9,step=0.1)# Optional HF Tokenhf_token = st.text_input("HuggingFace Token (optional)",type="password",help="Enter your HuggingFace token if required for model access")# Main chat interfacest.title("💬 DeepSeek Chatbot")st.caption("🚀 A conversational AI powered by DeepSeek models")# Display chat messagesfor message in st.session_state.messages:with st.chat_message(message["role"]):st.markdown(message["content"])if "timestamp" in message:st.caption(f"_{message['timestamp']}_")# Chat input and processingif prompt := st.chat_input("Type your message..."):# Add user message to historyst.session_state.messages.append({"role": "user","content": prompt,"timestamp": datetime.now().strftime("%H:%M:%S")})# Display user messagewith st.chat_message("user"):st.markdown(prompt)st.caption(f"_{st.session_state.messages[-1]['timestamp']}_")# Create full prompt with system messagefull_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"# Create client and generate responseclient = InferenceClient(model=selected_model, token=hf_token)# Display assistant responsewith st.chat_message("assistant"):response = st.write_stream(client.text_generation(full_prompt,max_new_tokens=max_new_tokens,temperature=temperature,top_p=top_p,stream=True))timestamp = datetime.now().strftime("%H:%M:%S")st.caption(f"_{timestamp}_")# Add assistant response to historyst.session_state.messages.append({"role": "assistant","content": response,"timestamp": timestamp})# Optional debug information# st.sidebar.markdown("---")# st.sidebar.json(st.session_state.messages)12345678910111213141516171819202122232425269 unmodified lines363738394041424344454647484950515253544 unmodified lines5960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102# app.pyimport streamlit as stfrom models import demo# Page configurationst.set_page_config(page_title="DeepSeek Chatbot - ruslanmv.com",page_icon="🤖",layout="centered")# Initialize session state for chat historyif "messages" not in st.session_state:st.session_state.messages = []# Sidebar for model selection and parameterswith st.sidebar:st.header("Model Configuration")# Model selectionselected_model = st.selectbox("Choose Model",["deepseek-ai/DeepSeek-R1-Distill-Qwen-32B","deepseek-ai/DeepSeek-R1","deepseek-ai/DeepSeek-R1-Zero"9 unmodified lines)# Generation parametersmax_tokens = st.slider("Max New Tokens",min_value=1,max_value=4000,value=512,step=10)temperature = st.slider("Temperature",min_value=0.1,max_value=4.0,value=0.7,step=0.1)4 unmodified linesvalue=0.9,step=0.1)# Main chat interfacest.title("🤖 DeepSeek Chatbot")st.caption("Powered by ruslanmv.com - Choose your model and parameters in the sidebar")# Display chat messagesfor message in st.session_state.messages:with st.chat_message(message["role"]):st.markdown(message["content"])# Chat inputif prompt := st.chat_input("Type your message..."):# Add user message to chat historyst.session_state.messages.append({"role": "user", "content": prompt})# Display user messagewith st.chat_message("user"):st.markdown(prompt)# Prepare full prompt with system messagefull_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"try:# Generate response using selected modelwith st.spinner("Generating response..."):response = demo.fn(full_prompt,max_new_tokens=max_tokens,temperature=temperature,top_p=top_p)# Display assistant responsewith st.chat_message("assistant"):st.markdown(response)# Add assistant response to chat historyst.session_state.messages.append({"role": "assistant", "content": response})except Exception as e:st.error(f"Error generating response: {str(e)}")
app.py+34-59