|
import streamlit as st |
|
from functools import lru_cache |
|
import requests |
|
|
|
|
|
@lru_cache(maxsize=3) |
|
def load_hf_model(model_name): |
|
|
|
api_url = f"https://api-inference.huggingface.co/models/deepseek-ai/{model_name}" |
|
return api_url |
|
|
|
|
|
MODELS = { |
|
"DeepSeek-R1-Distill-Qwen-32B": load_hf_model("DeepSeek-R1-Distill-Qwen-32B"), |
|
"DeepSeek-R1": load_hf_model("DeepSeek-R1"), |
|
"DeepSeek-R1-Zero": load_hf_model("DeepSeek-R1-Zero") |
|
} |
|
|
|
|
|
def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p): |
|
history = history or [] |
|
|
|
|
|
api_url = MODELS[model_choice] |
|
|
|
|
|
payload = { |
|
"inputs": { |
|
"messages": [{"role": "user", "content": input_text}], |
|
"system": system_message, |
|
"max_tokens": max_new_tokens, |
|
"temperature": temperature, |
|
"top_p": top_p |
|
} |
|
} |
|
|
|
|
|
try: |
|
headers = {"Authorization": f"Bearer {st.secrets['HUGGINGFACE_TOKEN']}"} |
|
response = requests.post(api_url, headers=headers, json=payload).json() |
|
|
|
|
|
if isinstance(response, list) and len(response) > 0: |
|
|
|
assistant_response = response[0].get("generated_text", "No response generated.") |
|
elif isinstance(response, dict) and "generated_text" in response: |
|
|
|
assistant_response = response["generated_text"] |
|
else: |
|
assistant_response = "Unexpected model response format." |
|
except Exception as e: |
|
assistant_response = f"Error: {str(e)}" |
|
|
|
|
|
history.append((input_text, assistant_response)) |
|
|
|
return history |
|
|
|
|
|
st.set_page_config(page_title="DeepSeek Chatbot", page_icon="🤖", layout="wide") |
|
|
|
|
|
st.title("DeepSeek Chatbot") |
|
st.markdown(""" |
|
Created by [ruslanmv.com](https://ruslanmv.com/) |
|
This is a demo of different DeepSeek models. Select a model, type your message, and click "Submit". |
|
You can also adjust optional parameters like system message, max new tokens, temperature, and top-p. |
|
""") |
|
|
|
|
|
with st.sidebar: |
|
st.header("Options") |
|
model_choice = st.radio( |
|
"Choose a Model", |
|
options=list(MODELS.keys()), |
|
index=0 |
|
) |
|
st.header("Optional Parameters") |
|
system_message = st.text_area( |
|
"System Message", |
|
value="You are a friendly Chatbot created by ruslanmv.com", |
|
height=100 |
|
) |
|
max_new_tokens = st.slider( |
|
"Max New Tokens", |
|
min_value=1, |
|
max_value=4000, |
|
value=200 |
|
) |
|
temperature = st.slider( |
|
"Temperature", |
|
min_value=0.10, |
|
max_value=4.00, |
|
value=0.70 |
|
) |
|
top_p = st.slider( |
|
"Top-p (nucleus sampling)", |
|
min_value=0.10, |
|
max_value=1.00, |
|
value=0.90 |
|
) |
|
|
|
|
|
if "chat_history" not in st.session_state: |
|
st.session_state.chat_history = [] |
|
|
|
|
|
for user_msg, assistant_msg in st.session_state.chat_history: |
|
with st.chat_message("user"): |
|
st.write(user_msg) |
|
with st.chat_message("assistant"): |
|
st.write(assistant_msg) |
|
|
|
|
|
user_input = st.chat_input("Type your message here...") |
|
|
|
|
|
if user_input: |
|
|
|
st.session_state.chat_history = chatbot( |
|
user_input, |
|
st.session_state.chat_history, |
|
model_choice, |
|
system_message, |
|
max_new_tokens, |
|
temperature, |
|
top_p |
|
) |
|
|
|
|
|
st.rerun() |