Spaces:
Running
Running
Create app-working-v2.py
Browse files- app-working-v2.py +116 -0
app-working-v2.py
ADDED
@@ -0,0 +1,116 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import streamlit as st
|
3 |
+
import requests
|
4 |
+
import logging
|
5 |
+
|
6 |
+
# Configure logging
|
7 |
+
logging.basicConfig(level=logging.INFO)
|
8 |
+
logger = logging.getLogger(__name__)
|
9 |
+
|
10 |
+
# Page configuration
|
11 |
+
st.set_page_config(
|
12 |
+
page_title="DeepSeek Chatbot - ruslanmv.com",
|
13 |
+
page_icon="🤖",
|
14 |
+
layout="centered"
|
15 |
+
)
|
16 |
+
|
17 |
+
# Initialize session state for chat history
|
18 |
+
if "messages" not in st.session_state:
|
19 |
+
st.session_state.messages = []
|
20 |
+
|
21 |
+
# Sidebar configuration
|
22 |
+
with st.sidebar:
|
23 |
+
st.header("Model Configuration")
|
24 |
+
# st.markdown("[Get HuggingFace Token](https://huggingface.co/settings/tokens)")
|
25 |
+
|
26 |
+
# Dropdown to select model
|
27 |
+
model_options = [
|
28 |
+
"deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
|
29 |
+
# "deepseek-ai/DeepSeek-R1-Distill-Qwen-14B",
|
30 |
+
# "deepseek-ai/DeepSeek-R1-Distill-Qwen-7B",
|
31 |
+
# "deepseek-ai/DeepSeek-R1-Distill-Llama-70B",
|
32 |
+
# "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"
|
33 |
+
]
|
34 |
+
selected_model = st.selectbox("Select Model", model_options, index=0)
|
35 |
+
|
36 |
+
system_message = st.text_area(
|
37 |
+
"System Message",
|
38 |
+
value="You are a friendly Chatbot created by ruslanmv.com",
|
39 |
+
height=100
|
40 |
+
)
|
41 |
+
|
42 |
+
max_tokens = st.slider(
|
43 |
+
"Max Tokens",
|
44 |
+
1, 4000, 512
|
45 |
+
)
|
46 |
+
|
47 |
+
temperature = st.slider(
|
48 |
+
"Temperature",
|
49 |
+
0.1, 4.0, 0.7
|
50 |
+
)
|
51 |
+
|
52 |
+
top_p = st.slider(
|
53 |
+
"Top-p",
|
54 |
+
0.1, 1.0, 0.9
|
55 |
+
)
|
56 |
+
|
57 |
+
# Function to query the Hugging Face API
|
58 |
+
def query(payload, api_url):
|
59 |
+
headers = {"Authorization": f"Bearer {st.secrets['HF_TOKEN']}"}
|
60 |
+
logger.info(f"Sending request to {api_url} with payload: {payload}")
|
61 |
+
response = requests.post(api_url, headers=headers, json=payload)
|
62 |
+
logger.info(f"Received response: {response.status_code}, {response.text}")
|
63 |
+
return response.json()
|
64 |
+
|
65 |
+
# Chat interface
|
66 |
+
st.title("🤖 DeepSeek Chatbot")
|
67 |
+
st.caption("Powered by Hugging Face Inference API - Configure in sidebar")
|
68 |
+
|
69 |
+
# Display chat history
|
70 |
+
for message in st.session_state.messages:
|
71 |
+
with st.chat_message(message["role"]):
|
72 |
+
st.markdown(message["content"])
|
73 |
+
|
74 |
+
# Handle input
|
75 |
+
if prompt := st.chat_input("Type your message..."):
|
76 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
77 |
+
|
78 |
+
with st.chat_message("user"):
|
79 |
+
st.markdown(prompt)
|
80 |
+
|
81 |
+
try:
|
82 |
+
with st.spinner("Generating response..."):
|
83 |
+
# Prepare the payload for the API
|
84 |
+
payload = {
|
85 |
+
"inputs": prompt,
|
86 |
+
"parameters": {
|
87 |
+
"max_new_tokens": max_tokens,
|
88 |
+
"temperature": temperature,
|
89 |
+
"top_p": top_p,
|
90 |
+
"return_full_text": False
|
91 |
+
}
|
92 |
+
}
|
93 |
+
|
94 |
+
# Dynamically construct the API URL based on the selected model
|
95 |
+
api_url = f"https://api-inference.huggingface.co/models/{selected_model}"
|
96 |
+
logger.info(f"Selected model: {selected_model}, API URL: {api_url}")
|
97 |
+
|
98 |
+
# Query the Hugging Face API using the selected model
|
99 |
+
output = query(payload, api_url)
|
100 |
+
|
101 |
+
# Handle API response
|
102 |
+
if isinstance(output, list) and len(output) > 0 and 'generated_text' in output[0]:
|
103 |
+
assistant_response = output[0]['generated_text']
|
104 |
+
logger.info(f"Generated response: {assistant_response}")
|
105 |
+
|
106 |
+
with st.chat_message("assistant"):
|
107 |
+
st.markdown(assistant_response)
|
108 |
+
|
109 |
+
st.session_state.messages.append({"role": "assistant", "content": assistant_response})
|
110 |
+
else:
|
111 |
+
logger.error(f"Unexpected API response: {output}")
|
112 |
+
st.error("Error: Unable to generate a response. Please try again.")
|
113 |
+
|
114 |
+
except Exception as e:
|
115 |
+
logger.error(f"Application Error: {str(e)}", exc_info=True)
|
116 |
+
st.error(f"Application Error: {str(e)}")
|