Spaces:
Running
Running
Update app-work-only-1.py
Browse files- app-work-only-1.py +1 -111
app-work-only-1.py
CHANGED
|
@@ -101,114 +101,4 @@ if prompt := st.chat_input("Type your message..."):
|
|
| 101 |
st.error("Error: Unable to generate a response. Please try again.")
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
-
st.error(f"Application Error: {str(e)}")
|
| 105 |
-
|
| 106 |
-
'''
|
| 107 |
-
|
| 108 |
-
import streamlit as st
|
| 109 |
-
import requests
|
| 110 |
-
|
| 111 |
-
# Hugging Face API URL (default model)
|
| 112 |
-
API_URL = "https://api-inference.huggingface.co/models/deepseek-ai/DeepSeek-R1-Distill-Qwen-32B"
|
| 113 |
-
|
| 114 |
-
# Function to query the Hugging Face API
|
| 115 |
-
def query(payload, api_url):
|
| 116 |
-
headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
|
| 117 |
-
response = requests.post(api_url, headers=headers, json=payload)
|
| 118 |
-
return response.json()
|
| 119 |
-
|
| 120 |
-
# Page configuration
|
| 121 |
-
st.set_page_config(
|
| 122 |
-
page_title="DeepSeek Chatbot - ruslanmv.com",
|
| 123 |
-
page_icon="🤖",
|
| 124 |
-
layout="centered"
|
| 125 |
-
)
|
| 126 |
-
|
| 127 |
-
# Initialize session state for chat history
|
| 128 |
-
if "messages" not in st.session_state:
|
| 129 |
-
st.session_state.messages = []
|
| 130 |
-
|
| 131 |
-
# Sidebar configuration
|
| 132 |
-
with st.sidebar:
|
| 133 |
-
st.header("Model Configuration")
|
| 134 |
-
st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")
|
| 135 |
-
|
| 136 |
-
# Dropdown to select model
|
| 137 |
-
model_options = [
|
| 138 |
-
"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
| 139 |
-
"deepseek-ai/DeepSeek-R1",
|
| 140 |
-
"deepseek-ai/DeepSeek-R1-Zero"
|
| 141 |
-
]
|
| 142 |
-
selected_model = st.selectbox("Select Model", model_options, index=0)
|
| 143 |
-
|
| 144 |
-
system_message = st.text_area(
|
| 145 |
-
"System Message",
|
| 146 |
-
value="You are a friendly Chatbot created by ruslanmv.com",
|
| 147 |
-
height=100
|
| 148 |
-
)
|
| 149 |
-
|
| 150 |
-
max_tokens = st.slider(
|
| 151 |
-
"Max Tokens",
|
| 152 |
-
1, 4000, 512
|
| 153 |
-
)
|
| 154 |
-
|
| 155 |
-
temperature = st.slider(
|
| 156 |
-
"Temperature",
|
| 157 |
-
0.1, 4.0, 0.7
|
| 158 |
-
)
|
| 159 |
-
|
| 160 |
-
top_p = st.slider(
|
| 161 |
-
"Top-p",
|
| 162 |
-
0.1, 1.0, 0.9
|
| 163 |
-
)
|
| 164 |
-
|
| 165 |
-
# Chat interface
|
| 166 |
-
st.title("🤖 DeepSeek Chatbot")
|
| 167 |
-
st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
|
| 168 |
-
|
| 169 |
-
# Display chat history
|
| 170 |
-
for message in st.session_state.messages:
|
| 171 |
-
with st.chat_message(message["role"]):
|
| 172 |
-
st.markdown(message["content"])
|
| 173 |
-
|
| 174 |
-
# Handle input
|
| 175 |
-
if prompt := st.chat_input("Type your message..."):
|
| 176 |
-
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 177 |
-
|
| 178 |
-
with st.chat_message("user"):
|
| 179 |
-
st.markdown(prompt)
|
| 180 |
-
|
| 181 |
-
try:
|
| 182 |
-
with st.spinner("Generating response..."):
|
| 183 |
-
# Prepare the payload for the API
|
| 184 |
-
payload = {
|
| 185 |
-
"inputs": prompt,
|
| 186 |
-
"parameters": {
|
| 187 |
-
"max_new_tokens": max_tokens,
|
| 188 |
-
"temperature": temperature,
|
| 189 |
-
"top_p": top_p,
|
| 190 |
-
"return_full_text": False
|
| 191 |
-
}
|
| 192 |
-
}
|
| 193 |
-
|
| 194 |
-
# Query the Hugging Face API using the selected model
|
| 195 |
-
output = query(payload, f"https://api-inference.huggingface.co/models/{selected_model}")
|
| 196 |
-
|
| 197 |
-
# Handle API response
|
| 198 |
-
if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]:
|
| 199 |
-
assistant_response = output[0]['generated_text']
|
| 200 |
-
|
| 201 |
-
with st.chat_message("assistant"):
|
| 202 |
-
st.markdown(assistant_response)
|
| 203 |
-
|
| 204 |
-
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
| 205 |
-
else:
|
| 206 |
-
st.error("Error: Unable to generate a response. Please try again.")
|
| 207 |
-
|
| 208 |
-
except Exception as e:
|
| 209 |
-
st.error(f"Application Error: {str(e)}")
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
'''
|
|
|
|
| 101 |
st.error("Error: Unable to generate a response. Please try again.")
|
| 102 |
|
| 103 |
except Exception as e:
|
| 104 |
+
st.error(f"Application Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|