Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,29 +1,107 @@
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
|
4 |
-
#
|
|
|
|
|
5 |
API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
|
6 |
|
7 |
-
# Function to query the Hugging Face API
|
8 |
def query(payload):
|
|
|
|
|
|
|
|
|
9 |
headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
|
10 |
response = requests.post(API_URL, headers=headers, json=payload)
|
11 |
return response.json()
|
12 |
|
13 |
-
# Streamlit app
|
14 |
-
st.title("DeepSeek-R1-Distill-Qwen-32B Chatbot")
|
15 |
|
16 |
-
#
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
-
# Display
|
25 |
-
|
26 |
-
st.
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
|
4 |
+
# -----------------------------------
|
5 |
+
# 1. Hugging Face API Configuration
|
6 |
+
# -----------------------------------
|
7 |
API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
|
8 |
|
|
|
9 |
def query(payload):
|
10 |
+
"""
|
11 |
+
Query the Hugging Face Inference API with the given payload.
|
12 |
+
Keeps the original approach: payload = {"inputs": user_input}.
|
13 |
+
"""
|
14 |
headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
|
15 |
response = requests.post(API_URL, headers=headers, json=payload)
|
16 |
return response.json()
|
17 |
|
|
|
|
|
18 |
|
19 |
+
# -----------------------------------
|
20 |
+
# 2. Streamlit Page Settings
|
21 |
+
# -----------------------------------
|
22 |
+
st.set_page_config(
|
23 |
+
page_title="DeepSeek Chatbot - ruslanmv.com",
|
24 |
+
page_icon="🤖",
|
25 |
+
layout="centered"
|
26 |
+
)
|
27 |
|
28 |
+
# -----------------------------------
|
29 |
+
# 3. Session State Initialization
|
30 |
+
# -----------------------------------
|
31 |
+
# We'll keep a chat history in st.session_state
|
32 |
+
if "messages" not in st.session_state:
|
33 |
+
st.session_state.messages = []
|
34 |
+
|
35 |
+
# -----------------------------------
|
36 |
+
# 4. Sidebar Configuration
|
37 |
+
# -----------------------------------
|
38 |
+
with st.sidebar:
|
39 |
+
st.header("Configuration")
|
40 |
+
st.markdown("[Get your HuggingFace Token](https://huggingface.co/settings/tokens)")
|
41 |
+
|
42 |
+
# Although these parameters are shown on the sidebar, we won't actually
|
43 |
+
# pass them to the payload in `query()`, to strictly preserve the "original" approach.
|
44 |
+
st.write("**NOTE:** These sliders do not affect the inference in this demo.")
|
45 |
+
system_message = st.text_area(
|
46 |
+
"System Message (display only)",
|
47 |
+
value="You are a friendly Chatbot created by ruslanmv.com",
|
48 |
+
height=100
|
49 |
+
)
|
50 |
+
max_tokens = st.slider("Max Tokens (not used here)", 1, 4000, 512)
|
51 |
+
temperature = st.slider("Temperature (not used here)", 0.1, 4.0, 0.7)
|
52 |
+
top_p = st.slider("Top-p (not used here)", 0.1, 1.0, 0.9)
|
53 |
+
|
54 |
+
# -----------------------------------
|
55 |
+
# 5. Main Chat Interface
|
56 |
+
# -----------------------------------
|
57 |
+
st.title("🤖 DeepSeek Chatbot")
|
58 |
+
st.caption("Powered by Hugging Face Inference API - Original Inference Approach")
|
59 |
+
|
60 |
+
# Display the chat history, message by message
|
61 |
+
for message in st.session_state.messages:
|
62 |
+
with st.chat_message(message["role"]):
|
63 |
+
st.markdown(message["content"])
|
64 |
+
|
65 |
+
# -----------------------------------
|
66 |
+
# 6. Capture User Input
|
67 |
+
# -----------------------------------
|
68 |
+
if user_input := st.chat_input("Type your message..."):
|
69 |
+
# 6.1 Append user message to chat history
|
70 |
+
st.session_state.messages.append({"role": "user", "content": user_input})
|
71 |
|
72 |
+
# Display user's message
|
73 |
+
with st.chat_message("user"):
|
74 |
+
st.markdown(user_input)
|
75 |
+
|
76 |
+
# -----------------------------------
|
77 |
+
# 7. Query the Model
|
78 |
+
# -----------------------------------
|
79 |
+
try:
|
80 |
+
with st.spinner("Generating response..."):
|
81 |
+
# Prepare payload with the original approach
|
82 |
+
payload = {"inputs": user_input}
|
83 |
+
output = query(payload)
|
84 |
+
|
85 |
+
# Check if the output is valid
|
86 |
+
if (
|
87 |
+
isinstance(output, list)
|
88 |
+
and len(output) > 0
|
89 |
+
and "generated_text" in output[0]
|
90 |
+
):
|
91 |
+
assistant_response = output[0]["generated_text"]
|
92 |
+
else:
|
93 |
+
assistant_response = (
|
94 |
+
"Error: Unable to generate a response. Please try again."
|
95 |
+
)
|
96 |
+
|
97 |
+
# Display the assistant's response
|
98 |
+
with st.chat_message("assistant"):
|
99 |
+
st.markdown(assistant_response)
|
100 |
+
|
101 |
+
# Store assistant's response in chat history
|
102 |
+
st.session_state.messages.append(
|
103 |
+
{"role": "assistant", "content": assistant_response}
|
104 |
+
)
|
105 |
+
|
106 |
+
except Exception as e:
|
107 |
+
st.error(f"Application Error: {str(e)}")
|