ruslanmv/DeepSeek-R1-Chatbot
36
1 2import streamlit as st3import requests4 5# Function to query the Hugging Face API6def query(payload, api_url):7 headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}8 response = requests.post(api_url, headers=headers, json=payload)9 return response.json()10 11# Page configuration12st.set_page_config(13 page_title="DeepSeek Chatbot - ruslanmv.com",14 page_icon="๐ค",15 layout="centered"16)17 18# Initialize session state for chat history19if "messages" not in st.session_state:20 st.session_state.messages = []21if "selected_model" not in st.session_state:22 st.session_state.selected_model = "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"23 24# Sidebar configuration25with st.sidebar:26 st.header("Model Configuration")27 st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")28 29 # Dropdown to select model30 model_options = [31 "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",32 "deepseek-ai/DeepSeek-R1",33 "deepseek-ai/DeepSeek-R1-Zero"34 ]35 selected_model = st.selectbox("Select Model", model_options, index=model_options.index(st.session_state.selected_model))36 st.session_state.selected_model = selected_model37 38 system_message = st.text_area(39 "System Message",40 value="You are a friendly Chatbot created by ruslanmv.com",41 height=10042 )43 44 max_tokens = st.slider(45 "Max Tokens",46 1, 4000, 51247 )48 49 temperature = st.slider(50 "Temperature",51 0.1, 4.0, 0.752 )53 54 top_p = st.slider(55 "Top-p",56 0.1, 1.0, 0.957 )58 59# Chat interface60st.title("๐ค DeepSeek Chatbot")61st.caption("Powered by Hugging Face Inference API - Configure in sidebar")62 63# Display chat history64for message in st.session_state.messages:65 with st.chat_message(message["role"]):66 st.markdown(message["content"])67 68# Handle input69if prompt := st.chat_input("Type your message..."):70 st.session_state.messages.append({"role": "user", "content": prompt})71 72 with st.chat_message("user"):73 st.markdown(prompt)74 75 try:76 with st.spinner("Generating response..."):77 # Prepare the payload for the API78 payload = {79 "inputs": prompt,80 "parameters": {81 "max_new_tokens": max_tokens,82 "temperature": temperature,83 "top_p": top_p,84 "return_full_text": False85 }86 }87 88 # Query the Hugging Face API using the selected model89 api_url = f"https://api-inference.huggingface.co/models/{st.session_state.selected_model}"90 output = query(payload, api_url)91 92 # Handle API response93 if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]:94 assistant_response = output[0]['generated_text']95 96 with st.chat_message("assistant"):97 st.markdown(assistant_response)98 99 st.session_state.messages.append({"role": "assistant", "content": assistant_response})100 else:101 st.error("Error: Unable to generate a response. Please try again.")102 103 except Exception as e:104 st.error(f"Application Error: {str(e)}")105 106'''107 108import streamlit as st109import requests110 111# Hugging Face API URL (default model)112API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"113 114# Function to query the Hugging Face API115def query(payload, api_url):116 headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}117 response = requests.post(api_url, headers=headers, json=payload)118 return response.json()119 120# Page configuration121st.set_page_config(122 page_title="DeepSeek Chatbot - ruslanmv.com",123 page_icon="๐ค",124 layout="centered"125)126 127# Initialize session state for chat history128if "messages" not in st.session_state:129 st.session_state.messages = []130 131# Sidebar configuration132with st.sidebar:133 st.header("Model Configuration")134 st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")135 136 # Dropdown to select model137 model_options = [138 "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",139 "deepseek-ai/DeepSeek-R1",140 "deepseek-ai/DeepSeek-R1-Zero"141 ]142 selected_model = st.selectbox("Select Model", model_options, index=0)143 144 system_message = st.text_area(145 "System Message",146 value="You are a friendly Chatbot created by ruslanmv.com",147 height=100148 )149 150 max_tokens = st.slider(151 "Max Tokens",152 1, 4000, 512153 )154 155 temperature = st.slider(156 "Temperature",157 0.1, 4.0, 0.7158 )159 160 top_p = st.slider(161 "Top-p",162 0.1, 1.0, 0.9163 )164 165# Chat interface166st.title("๐ค DeepSeek Chatbot")167st.caption("Powered by Hugging Face Inference API - Configure in sidebar")168 169# Display chat history170for message in st.session_state.messages:171 with st.chat_message(message["role"]):172 st.markdown(message["content"])173 174# Handle input175if prompt := st.chat_input("Type your message..."):176 st.session_state.messages.append({"role": "user", "content": prompt})177 178 with st.chat_message("user"):179 st.markdown(prompt)180 181 try:182 with st.spinner("Generating response..."):183 # Prepare the payload for the API184 payload = {185 "inputs": prompt,186 "parameters": {187 "max_new_tokens": max_tokens,188 "temperature": temperature,189 "top_p": top_p,190 "return_full_text": False191 }192 }193 194 # Query the Hugging Face API using the selected model195 output = query(payload, f"https://api-inference.huggingface.co/models/{selected_model}")196 197 # Handle API response198 if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]:199 assistant_response = output[0]['generated_text']200 201 with st.chat_message("assistant"):202 st.markdown(assistant_response)203 204 st.session_state.messages.append({"role": "assistant", "content": assistant_response})205 else:206 st.error("Error: Unable to generate a response. Please try again.")207 208 except Exception as e:209 st.error(f"Application Error: {str(e)}")210 211 212 213 214'''215 