Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,51 @@
|
|
1 |
-
import
|
2 |
-
from
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
|
4 |
# --- Chatbot function ---
|
5 |
def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
|
6 |
history = history or []
|
7 |
|
|
|
|
|
|
|
8 |
# Create payload for the model
|
9 |
payload = {
|
10 |
-
"
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
15 |
}
|
16 |
|
17 |
# Run inference using the selected model
|
18 |
try:
|
19 |
-
|
20 |
-
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
else:
|
23 |
assistant_response = "Unexpected model response format."
|
24 |
except Exception as e:
|
@@ -27,63 +54,78 @@ def chatbot(input_text, history, model_choice, system_message, max_new_tokens, t
|
|
27 |
# Append user and assistant messages to history
|
28 |
history.append((input_text, assistant_response))
|
29 |
|
30 |
-
return history
|
31 |
|
32 |
-
# ---
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
)
|
42 |
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
msg = gr.Textbox(label="Your Message", placeholder="Type your message here...")
|
47 |
-
with gr.Row():
|
48 |
-
submit_btn = gr.Button("Submit", variant="primary")
|
49 |
-
clear_btn = gr.ClearButton([msg, chatbot_output])
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
)
|
58 |
-
with gr.Accordion("Optional Parameters", open=False):
|
59 |
-
system_message = gr.Textbox(
|
60 |
-
label="System Message",
|
61 |
-
value="You are a friendly Chatbot created by ruslanmv.com",
|
62 |
-
lines=2,
|
63 |
-
)
|
64 |
-
max_new_tokens = gr.Slider(
|
65 |
-
minimum=1, maximum=4000, value=200, label="Max New Tokens"
|
66 |
-
)
|
67 |
-
temperature = gr.Slider(
|
68 |
-
minimum=0.10, maximum=4.00, value=0.70, label="Temperature"
|
69 |
-
)
|
70 |
-
top_p = gr.Slider(
|
71 |
-
minimum=0.10, maximum=1.00, value=0.90, label="Top-p (nucleus sampling)"
|
72 |
-
)
|
73 |
|
74 |
-
|
|
|
75 |
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
)
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
[chatbot_output, chat_history, msg]
|
86 |
-
)
|
87 |
-
|
88 |
-
if __name__ == "__main__":
|
89 |
-
app.launch()
|
|
|
1 |
+
import streamlit as st
|
2 |
+
from functools import lru_cache
|
3 |
+
import requests
|
4 |
+
|
5 |
+
# Cache model loading to optimize performance
|
6 |
+
@lru_cache(maxsize=3)
|
7 |
+
def load_hf_model(model_name):
|
8 |
+
# Use the Hugging Face Inference API directly
|
9 |
+
api_url = f"https://api-inference.huggingface.co/models/deepseek-ai/{model_name}"
|
10 |
+
return api_url
|
11 |
+
|
12 |
+
# Load all models at startup
|
13 |
+
MODELS = {
|
14 |
+
"DeepSeek-R1-Distill-Qwen-32B": load_hf_model("DeepSeek-R1-Distill-Qwen-32B"),
|
15 |
+
"DeepSeek-R1": load_hf_model("DeepSeek-R1"),
|
16 |
+
"DeepSeek-R1-Zero": load_hf_model("DeepSeek-R1-Zero")
|
17 |
+
}
|
18 |
|
19 |
# --- Chatbot function ---
|
20 |
def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
|
21 |
history = history or []
|
22 |
|
23 |
+
# Get the selected model API URL
|
24 |
+
api_url = MODELS[model_choice]
|
25 |
+
|
26 |
# Create payload for the model
|
27 |
payload = {
|
28 |
+
"inputs": {
|
29 |
+
"messages": [{"role": "user", "content": input_text}],
|
30 |
+
"system": system_message,
|
31 |
+
"max_tokens": max_new_tokens,
|
32 |
+
"temperature": temperature,
|
33 |
+
"top_p": top_p
|
34 |
+
}
|
35 |
}
|
36 |
|
37 |
# Run inference using the selected model
|
38 |
try:
|
39 |
+
headers = {"Authorization": f"Bearer {st.secrets['HUGGINGFACE_TOKEN']}"}
|
40 |
+
response = requests.post(api_url, headers=headers, json=payload).json()
|
41 |
+
|
42 |
+
# Handle the response format
|
43 |
+
if isinstance(response, list) and len(response) > 0:
|
44 |
+
# Assuming the response is a list of generated text
|
45 |
+
assistant_response = response[0].get("generated_text", "No response generated.")
|
46 |
+
elif isinstance(response, dict) and "generated_text" in response:
|
47 |
+
# If the response is a dictionary with generated_text
|
48 |
+
assistant_response = response["generated_text"]
|
49 |
else:
|
50 |
assistant_response = "Unexpected model response format."
|
51 |
except Exception as e:
|
|
|
54 |
# Append user and assistant messages to history
|
55 |
history.append((input_text, assistant_response))
|
56 |
|
57 |
+
return history
|
58 |
|
59 |
+
# --- Streamlit App ---
|
60 |
+
st.set_page_config(page_title="DeepSeek Chatbot", page_icon="🤖", layout="wide")
|
61 |
+
|
62 |
+
# Title and description
|
63 |
+
st.title("DeepSeek Chatbot")
|
64 |
+
st.markdown("""
|
65 |
+
Created by [ruslanmv.com](https://ruslanmv.com/)
|
66 |
+
This is a demo of different DeepSeek models. Select a model, type your message, and click "Submit".
|
67 |
+
You can also adjust optional parameters like system message, max new tokens, temperature, and top-p.
|
68 |
+
""")
|
69 |
+
|
70 |
+
# Sidebar for model selection and parameters
|
71 |
+
with st.sidebar:
|
72 |
+
st.header("Options")
|
73 |
+
model_choice = st.radio(
|
74 |
+
"Choose a Model",
|
75 |
+
options=list(MODELS.keys()),
|
76 |
+
index=0
|
77 |
+
)
|
78 |
+
st.header("Optional Parameters")
|
79 |
+
system_message = st.text_area(
|
80 |
+
"System Message",
|
81 |
+
value="You are a friendly Chatbot created by ruslanmv.com",
|
82 |
+
height=100
|
83 |
+
)
|
84 |
+
max_new_tokens = st.slider(
|
85 |
+
"Max New Tokens",
|
86 |
+
min_value=1,
|
87 |
+
max_value=4000,
|
88 |
+
value=200
|
89 |
+
)
|
90 |
+
temperature = st.slider(
|
91 |
+
"Temperature",
|
92 |
+
min_value=0.10,
|
93 |
+
max_value=4.00,
|
94 |
+
value=0.70
|
95 |
+
)
|
96 |
+
top_p = st.slider(
|
97 |
+
"Top-p (nucleus sampling)",
|
98 |
+
min_value=0.10,
|
99 |
+
max_value=1.00,
|
100 |
+
value=0.90
|
101 |
)
|
102 |
|
103 |
+
# Initialize chat history
|
104 |
+
if "chat_history" not in st.session_state:
|
105 |
+
st.session_state.chat_history = []
|
|
|
|
|
|
|
|
|
106 |
|
107 |
+
# Display chat history
|
108 |
+
for user_msg, assistant_msg in st.session_state.chat_history:
|
109 |
+
with st.chat_message("user"):
|
110 |
+
st.write(user_msg)
|
111 |
+
with st.chat_message("assistant"):
|
112 |
+
st.write(assistant_msg)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
+
# Input box for user message
|
115 |
+
user_input = st.chat_input("Type your message here...")
|
116 |
|
117 |
+
# Handle user input
|
118 |
+
if user_input:
|
119 |
+
# Add user message to chat history
|
120 |
+
st.session_state.chat_history = chatbot(
|
121 |
+
user_input,
|
122 |
+
st.session_state.chat_history,
|
123 |
+
model_choice,
|
124 |
+
system_message,
|
125 |
+
max_new_tokens,
|
126 |
+
temperature,
|
127 |
+
top_p
|
128 |
)
|
129 |
+
|
130 |
+
# Rerun to update the chat display
|
131 |
+
st.rerun()
|
|
|
|
|
|
|
|
|
|