import streamlit as st import requests import logging import os logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) st.set_page_config( page_title="DeepSeek Chatbot", page_icon="🤖", layout="centered" ) if "messages" not in st.session_state: st.session_state.messages = [] with st.sidebar: st.header("Model Configuration") # st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)") model_options = [ "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B", # "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B", # "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B", # "deepseek-ai/DeepSeek-R1-Distill-Llama-70B", # "deepseek-ai/DeepSeek-R1-Distill-Llama-8B" ] selected_model = st.selectbox("Select Model", model_options, index=0) system_message = st.text_area( "System Message", value=( "You are an intelligent and helpful AI assistant. " "Your goal is to provide clear, concise, and informative responses to user queries. " "Be polite, professional, and friendly while ensuring accuracy. " "If a user asks an unclear question, kindly ask for clarification. " "When providing information, make sure it is easy to understand. " "Keep your tone engaging and supportive, adapting to the user's needs." ), height=150 ) 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 ) def query(payload, api_url): HF_TOKEN = os.getenv("HF_TOKEN") if not HF_TOKEN: st.error("Missing Hugging Face API token. Please set 'HF_TOKEN' in the Hugging Face Spaces secrets.") headers = {"Authorization": f"Bearer {HF_TOKEN}"} logger.info(f"Sending request to {api_url} with payload: {payload}") response = requests.post(api_url, headers=headers, json=payload) logger.info(f"Received response: {response.status_code}, {response.text}") return response.json() st.title("🤖 DeepSeek Chatbot") st.caption("Powered by Hugging Face Inference API - Configure in sidebar") for message in st.session_state.messages: with st.chat_message(message["role"]): st.markdown(message["content"]) if prompt := st.chat_input("Type your message..."): st.session_state.messages.append({"role": "user", "content": prompt}) with st.chat_message("user"): st.markdown(prompt) try: with st.spinner("Generating response..."): # 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 } } # Dynamically construct the API URL based on the selected model api_url = f"https://api-inference.huggingface.co/models/{selected_model}" logger.info(f"Selected model: {selected_model}, API URL: {api_url}") # Query the Hugging Face API using the selected model output = query(payload, api_url) # Handle API response if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]: assistant_response = output[0]['generated_text'] logger.info(f"Generated response: {assistant_response}") with st.chat_message("assistant"): st.markdown(assistant_response) st.session_state.messages.append({"role": "assistant", "content": assistant_response}) else: logger.error(f"Unexpected API response: {output}") st.error("Error: Unable to generate a response. Please try again.") except Exception as e: logger.error(f"Application Error: {str(e)}", exc_info=True) st.error(f"Application Error: {str(e)}")