ruslanmv/DeepSeek-R1-Chatbot
36
1import streamlit as st2import requests3import logging4 5# Configure logging6logging.basicConfig(level=logging.INFO)7logger = logging.getLogger(__name__)8 9# Page configuration10st.set_page_config(11 page_title="DeepSeek Chatbot - ruslanmv.com",12 page_icon="๐ค",13 layout="centered"14)15 16# Initialize session state for chat history17if "messages" not in st.session_state:18 st.session_state.messages = []19 20# Sidebar configuration21with st.sidebar:22 st.header("Model Configuration")23 st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")24 25 # Dropdown to select model26 model_options = [27 "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",28 ]29 selected_model = st.selectbox("Select Model", model_options, index=0)30 31 system_message = st.text_area(32 "System Message",33 value="You are a friendly chatbot created by ruslanmv.com. Provide clear, accurate, and brief answers. Keep responses polite, engaging, and to the point. If unsure, politely suggest alternatives.",34 height=10035 )36 37 max_tokens = st.slider(38 "Max Tokens",39 10, 4000, 10040 )41 42 temperature = st.slider(43 "Temperature",44 0.1, 4.0, 0.345 )46 47 top_p = st.slider(48 "Top-p",49 0.1, 1.0, 0.650 )51 52# Function to query the Hugging Face API53def query(payload, api_url):54 headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}55 logger.info(f"Sending request to {api_url} with payload: {payload}")56 response = requests.post(api_url, headers=headers, json=payload)57 logger.info(f"Received response: {response.status_code}, {response.text}")58 try:59 return response.json()60 except requests.exceptions.JSONDecodeError:61 logger.error(f"Failed to decode JSON response: {response.text}")62 return None63 64# Chat interface65st.title("๐ค DeepSeek Chatbot")66st.caption("Powered by Hugging Face Inference API - Configure in sidebar")67 68# Display chat history69for message in st.session_state.messages:70 with st.chat_message(message["role"]):71 st.markdown(message["content"])72 73# Handle input74if prompt := st.chat_input("Type your message..."):75 st.session_state.messages.append({"role": "user", "content": prompt})76 77 with st.chat_message("user"):78 st.markdown(prompt)79 80 try:81 with st.spinner("Generating response..."):82 # Prepare the payload for the API83 # Combine system message and user input into a single prompt84 full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"85 payload = {86 "inputs": full_prompt,87 "parameters": {88 "max_new_tokens": max_tokens,89 "temperature": temperature,90 "top_p": top_p,91 "return_full_text": False92 }93 }94 95 # Dynamically construct the API URL based on the selected model96 api_url = f"https://api-inference.huggingface.co/models/{selected_model}"97 logger.info(f"Selected model: {selected_model}, API URL: {api_url}")98 99 # Query the Hugging Face API using the selected model100 output = query(payload, api_url)101 102 # Handle API response103 if output is not None and isinstance(output, list) and len(output) > 0:104 if 'generated_text' in output[0]:105 # Extract the assistant's response106 assistant_response = output[0]['generated_text'].strip()107 108 # Check for and remove duplicate responses109 responses = assistant_response.split("\n</think>\n")110 unique_response = responses[0].strip()111 112 logger.info(f"Generated response: {unique_response}")113 114 # Append response to chat only once115 with st.chat_message("assistant"):116 st.markdown(unique_response)117 118 st.session_state.messages.append({"role": "assistant", "content": unique_response})119 else:120 logger.error(f"Unexpected API response structure: {output}")121 st.error("Error: Unexpected response from the model. Please try again.")122 else:123 logger.error(f"Empty or invalid API response: {output}")124 st.error("Error: Unable to generate a response. Please check the model and try again.")125 126 except Exception as e:127 logger.error(f"Application Error: {str(e)}", exc_info=True)128 st.error(f"Application Error: {str(e)}")129 