Update app.py
Browse files123456456781011121391011121819202113141516171819282930312021222335362437253940414243442627282946303132335051523435363738394041424344454647484954555650515852536054555657586566676869705960616272637464657677786667818268697071727374757677787980848586878889909181828384859394958687889789901001011021031049192939410795import streamlit as stimport requests
# -----------------------------------# 1. Hugging Face API Configuration# -----------------------------------# Hugging Face API URLAPI_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
# Function to query the Hugging Face APIdef query(payload): """ Query the Hugging Face Inference API with the given payload. Keeps the original approach: payload = {"inputs": user_input}. """ headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"} response = requests.post(API_URL, headers=headers, json=payload) return response.json()
# -----------------------------------# 2. Streamlit Page Settings# -----------------------------------# Page configurationst.set_page_config( page_title="DeepSeek Chatbot - ruslanmv.com", page_icon="🤖", layout="centered")
# -----------------------------------# 3. Session State Initialization# -----------------------------------# We'll keep a chat history in st.session_state# Initialize session state for chat historyif "messages" not in st.session_state: st.session_state.messages = []
# -----------------------------------# 4. Sidebar Configuration# Sidebar configuration# -----------------------------------with st.sidebar: st.header("Configuration") st.markdown("[Get your HuggingFace Token](https://huggingface.co/settings/tokens)")
# Although these parameters are shown on the sidebar, we won't actually # pass them to the payload in `query()`, to strictly preserve the "original" approach. st.write("**NOTE:** These sliders do not affect the inference in this demo.") st.header("Model Configuration") st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)") system_message = st.text_area( "System Message (display only)", "System Message", value="You are a friendly Chatbot created by ruslanmv.com", height=100 ) max_tokens = st.slider("Max Tokens (not used here)", 1, 4000, 512) temperature = st.slider("Temperature (not used here)", 0.1, 4.0, 0.7) top_p = st.slider("Top-p (not used here)", 0.1, 1.0, 0.9) max_tokens = st.slider( "Max Tokens", 1, 4000, 512 ) temperature = st.slider( "Temperature", 0.1, 4.0, 0.7 ) top_p = st.slider( "Top-p", 0.1, 1.0, 0.9 )
# -----------------------------------# 5. Main Chat Interface# -----------------------------------# Chat interfacest.title("🤖 DeepSeek Chatbot")st.caption("Powered by Hugging Face Inference API - Original Inference Approach")st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
# Display the chat history, message by message# Display chat historyfor message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"])
# -----------------------------------# 6. Capture User Input# -----------------------------------if user_input := st.chat_input("Type your message..."): # 6.1 Append user message to chat history st.session_state.messages.append({"role": "user", "content": user_input})# Handle inputif prompt := st.chat_input("Type your message..."): st.session_state.messages.append({"role": "user", "content": prompt}) # Display user's message with st.chat_message("user"): st.markdown(user_input) st.markdown(prompt)
# ----------------------------------- # 7. Query the Model # ----------------------------------- try: with st.spinner("Generating response..."): # Prepare payload with the original approach payload = {"inputs": user_input} # Prepare the payload for the API payload = { "inputs": prompt, "parameters": { "max_new_tokens": max_tokens, "temperature": temperature, "top_p": top_p, "return_full_text": False } } # Query the Hugging Face API output = query(payload)
# Check if the output is valid if ( isinstance(output, list) and len(output) > 0 and "generated_text" in output[0] ): assistant_response = output[0]["generated_text"] # Handle API response if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]: assistant_response = output[0]['generated_text'] else: assistant_response = ( "Error: Unable to generate a response. Please try again." ) st.error("Error: Unable to generate a response. Please try again.") return
# Display the assistant's response with st.chat_message("assistant"): st.markdown(assistant_response)
# Store assistant's response in chat history st.session_state.messages.append( {"role": "assistant", "content": assistant_response} ) st.session_state.messages.append({"role": "assistant", "content": assistant_response})
except Exception as e: st.error(f"Application Error: {str(e)}") st.error(f"Application Error: {str(e)}")app.py+52-64