Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,107 +1,95 @@
|
|
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
|
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
|
47 |
value="You are a friendly Chatbot created by ruslanmv.com",
|
48 |
height=100
|
49 |
)
|
50 |
-
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
53 |
|
54 |
-
#
|
55 |
-
# 5. Main Chat Interface
|
56 |
-
# -----------------------------------
|
57 |
st.title("🤖 DeepSeek Chatbot")
|
58 |
-
st.caption("Powered by Hugging Face Inference API -
|
59 |
|
60 |
-
# Display
|
61 |
for message in st.session_state.messages:
|
62 |
with st.chat_message(message["role"]):
|
63 |
st.markdown(message["content"])
|
64 |
|
65 |
-
#
|
66 |
-
|
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(
|
75 |
|
76 |
-
# -----------------------------------
|
77 |
-
# 7. Query the Model
|
78 |
-
# -----------------------------------
|
79 |
try:
|
80 |
with st.spinner("Generating response..."):
|
81 |
-
# Prepare payload
|
82 |
-
payload = {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
output = query(payload)
|
84 |
-
|
85 |
-
#
|
86 |
-
if (
|
87 |
-
|
88 |
-
and len(output) > 0
|
89 |
-
and "generated_text" in output[0]
|
90 |
-
):
|
91 |
-
assistant_response = output[0]["generated_text"]
|
92 |
else:
|
93 |
-
|
94 |
-
|
95 |
-
)
|
96 |
|
97 |
-
# Display the assistant's response
|
98 |
with st.chat_message("assistant"):
|
99 |
st.markdown(assistant_response)
|
100 |
-
|
101 |
-
|
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)}")
|
|
|
1 |
import streamlit as st
|
2 |
import requests
|
3 |
|
4 |
+
# Hugging Face API URL
|
|
|
|
|
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 |
+
# Page configuration
|
|
|
|
|
|
|
14 |
st.set_page_config(
|
15 |
page_title="DeepSeek Chatbot - ruslanmv.com",
|
16 |
page_icon="🤖",
|
17 |
layout="centered"
|
18 |
)
|
19 |
|
20 |
+
# Initialize session state for chat history
|
|
|
|
|
|
|
21 |
if "messages" not in st.session_state:
|
22 |
st.session_state.messages = []
|
23 |
|
24 |
+
# Sidebar configuration
|
|
|
|
|
25 |
with st.sidebar:
|
26 |
+
st.header("Model Configuration")
|
27 |
+
st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")
|
28 |
+
|
|
|
|
|
|
|
29 |
system_message = st.text_area(
|
30 |
+
"System Message",
|
31 |
value="You are a friendly Chatbot created by ruslanmv.com",
|
32 |
height=100
|
33 |
)
|
34 |
+
|
35 |
+
max_tokens = st.slider(
|
36 |
+
"Max Tokens",
|
37 |
+
1, 4000, 512
|
38 |
+
)
|
39 |
+
|
40 |
+
temperature = st.slider(
|
41 |
+
"Temperature",
|
42 |
+
0.1, 4.0, 0.7
|
43 |
+
)
|
44 |
+
|
45 |
+
top_p = st.slider(
|
46 |
+
"Top-p",
|
47 |
+
0.1, 1.0, 0.9
|
48 |
+
)
|
49 |
|
50 |
+
# Chat interface
|
|
|
|
|
51 |
st.title("🤖 DeepSeek Chatbot")
|
52 |
+
st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
|
53 |
|
54 |
+
# Display chat history
|
55 |
for message in st.session_state.messages:
|
56 |
with st.chat_message(message["role"]):
|
57 |
st.markdown(message["content"])
|
58 |
|
59 |
+
# Handle input
|
60 |
+
if prompt := st.chat_input("Type your message..."):
|
61 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
62 |
|
|
|
63 |
with st.chat_message("user"):
|
64 |
+
st.markdown(prompt)
|
65 |
|
|
|
|
|
|
|
66 |
try:
|
67 |
with st.spinner("Generating response..."):
|
68 |
+
# Prepare the payload for the API
|
69 |
+
payload = {
|
70 |
+
"inputs": prompt,
|
71 |
+
"parameters": {
|
72 |
+
"max_new_tokens": max_tokens,
|
73 |
+
"temperature": temperature,
|
74 |
+
"top_p": top_p,
|
75 |
+
"return_full_text": False
|
76 |
+
}
|
77 |
+
}
|
78 |
+
|
79 |
+
# Query the Hugging Face API
|
80 |
output = query(payload)
|
81 |
+
|
82 |
+
# Handle API response
|
83 |
+
if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]:
|
84 |
+
assistant_response = output[0]['generated_text']
|
|
|
|
|
|
|
|
|
85 |
else:
|
86 |
+
st.error("Error: Unable to generate a response. Please try again.")
|
87 |
+
return
|
|
|
88 |
|
|
|
89 |
with st.chat_message("assistant"):
|
90 |
st.markdown(assistant_response)
|
91 |
+
|
92 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
|
|
|
|
|
|
93 |
|
94 |
except Exception as e:
|
95 |
+
st.error(f"Application Error: {str(e)}")
|