CoolFace
Apppublic

MoiMoi-01/DeepSeek-R1-Chatbot

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
app-working-v2.py117 linesDownload Raw Back to root
1 2import streamlit as st3import requests4import logging5 6# Configure logging7logging.basicConfig(level=logging.INFO)8logger = logging.getLogger(__name__)9 10# Page configuration11st.set_page_config(12    page_title="DeepSeek Chatbot - ruslanmv.com",13    page_icon="๐Ÿค–",14    layout="centered"15)16 17# Initialize session state for chat history18if "messages" not in st.session_state:19    st.session_state.messages = []20 21# Sidebar configuration22with st.sidebar:23    st.header("Model Configuration")24   # st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")25 26    # Dropdown to select model27    model_options = [28        "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",29     #   "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B",30     #   "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",31     #   "deepseek-ai/DeepSeek-R1-Distill-Llama-70B", 32     #   "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"33    ]34    selected_model = st.selectbox("Select Model", model_options, index=0)35 36    system_message = st.text_area(37        "System Message",38        value="You are a friendly Chatbot created by ruslanmv.com",39        height=10040    )41 42    max_tokens = st.slider(43        "Max Tokens",44        1, 4000, 51245    )46 47    temperature = st.slider(48        "Temperature",49        0.1, 4.0, 0.750    )51 52    top_p = st.slider(53        "Top-p",54        0.1, 1.0, 0.955    )56 57# Function to query the Hugging Face API58def query(payload, api_url):59    headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}60    logger.info(f"Sending request to {api_url} with payload: {payload}")61    response = requests.post(api_url, headers=headers, json=payload)62    logger.info(f"Received response: {response.status_code}, {response.text}")63    return response.json()64 65# Chat interface66st.title("๐Ÿค– DeepSeek Chatbot")67st.caption("Powered by Hugging Face Inference API - Configure in sidebar")68 69# Display chat history70for message in st.session_state.messages:71    with st.chat_message(message["role"]):72        st.markdown(message["content"])73 74# Handle input75if prompt := st.chat_input("Type your message..."):76    st.session_state.messages.append({"role": "user", "content": prompt})77 78    with st.chat_message("user"):79        st.markdown(prompt)80 81    try:82        with st.spinner("Generating response..."):83            # Prepare the payload for the API84            payload = {85                "inputs": prompt,86                "parameters": {87                    "max_new_tokens": max_tokens,88                    "temperature": temperature,89                    "top_p": top_p,90                    "return_full_text": False91                }92            }93 94            # Dynamically construct the API URL based on the selected model95            api_url = f"https://api-inference.huggingface.co/models/{selected_model}"96            logger.info(f"Selected model: {selected_model}, API URL: {api_url}")97 98            # Query the Hugging Face API using the selected model99            output = query(payload, api_url)100 101            # Handle API response102            if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]:103                assistant_response = output[0]['generated_text']104                logger.info(f"Generated response: {assistant_response}")105 106                with st.chat_message("assistant"):107                    st.markdown(assistant_response)108 109                st.session_state.messages.append({"role": "assistant", "content": assistant_response})110            else:111                logger.error(f"Unexpected API response: {output}")112                st.error("Error: Unable to generate a response. Please try again.")113 114    except Exception as e:115        logger.error(f"Application Error: {str(e)}", exc_info=True)116        st.error(f"Application Error: {str(e)}")117