|
import streamlit as st |
|
import requests |
|
|
|
|
|
|
|
|
|
API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B" |
|
|
|
def 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() |
|
|
|
|
|
|
|
|
|
|
|
st.set_page_config( |
|
page_title="DeepSeek Chatbot - ruslanmv.com", |
|
page_icon="🤖", |
|
layout="centered" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
if "messages" not in st.session_state: |
|
st.session_state.messages = [] |
|
|
|
|
|
|
|
|
|
with st.sidebar: |
|
st.header("Configuration") |
|
st.markdown("[Get your HuggingFace Token](https://huggingface.co/settings/tokens)") |
|
|
|
|
|
|
|
st.write("**NOTE:** These sliders do not affect the inference in this demo.") |
|
system_message = st.text_area( |
|
"System Message (display only)", |
|
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) |
|
|
|
|
|
|
|
|
|
st.title("🤖 DeepSeek Chatbot") |
|
st.caption("Powered by Hugging Face Inference API - Original Inference Approach") |
|
|
|
|
|
for message in st.session_state.messages: |
|
with st.chat_message(message["role"]): |
|
st.markdown(message["content"]) |
|
|
|
|
|
|
|
|
|
if user_input := st.chat_input("Type your message..."): |
|
|
|
st.session_state.messages.append({"role": "user", "content": user_input}) |
|
|
|
|
|
with st.chat_message("user"): |
|
st.markdown(user_input) |
|
|
|
|
|
|
|
|
|
try: |
|
with st.spinner("Generating response..."): |
|
|
|
payload = {"inputs": user_input} |
|
output = query(payload) |
|
|
|
|
|
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." |
|
) |
|
|
|
|
|
with st.chat_message("assistant"): |
|
st.markdown(assistant_response) |
|
|
|
|
|
st.session_state.messages.append( |
|
{"role": "assistant", "content": assistant_response} |
|
) |
|
|
|
except Exception as e: |
|
st.error(f"Application Error: {str(e)}") |
|
|