ruslanmv commited on
Commit
07a96e5
·
verified ·
1 Parent(s): 9ab0176

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +68 -90
app.py CHANGED
@@ -1,51 +1,71 @@
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:
@@ -53,57 +73,15 @@ def chatbot(input_text, history, model_choice, system_message, max_new_tokens, t
53
 
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"):
@@ -111,14 +89,14 @@ for user_msg, assistant_msg in st.session_state.chat_history:
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,
@@ -127,5 +105,5 @@ if user_input:
127
  top_p
128
  )
129
 
130
- # Rerun to update the chat display
131
  st.rerun()
 
1
  import streamlit as st
2
+ from models import demo # Import the demo object from models.py
 
3
 
4
+ # --- Streamlit App Configuration ---
5
+ st.set_page_config(
6
+ page_title="DeepSeek Chatbot",
7
+ page_icon="🤖",
8
+ layout="wide"
9
+ )
10
 
11
+ # --- App Title and Description ---
12
+ st.title("DeepSeek Chatbot")
13
+ st.markdown("""
14
+ Created by [ruslanmv.com](https://ruslanmv.com/)
15
+ This is a demo of different DeepSeek models. Select a model, type your message, and click "Submit".
16
+ You can also adjust optional parameters like system message, max new tokens, temperature, and top-p.
17
+ """)
18
 
19
+ # --- Sidebar for Model Selection and Parameters ---
20
+ with st.sidebar:
21
+ st.header("Options")
22
+ model_choice = st.radio(
23
+ "Choose a Model",
24
+ options=["DeepSeek-R1-Distill-Qwen-32B", "DeepSeek-R1", "DeepSeek-R1-Zero"],
25
+ index=1 # Default to "DeepSeek-R1"
26
+ )
27
 
28
+ with st.expander("Optional Parameters", expanded=False):
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
+ max_new_tokens = st.slider(
35
+ "Max New Tokens",
36
+ min_value=1,
37
+ max_value=4000,
38
+ value=200
39
+ )
40
+ temperature = st.slider(
41
+ "Temperature",
42
+ min_value=0.10,
43
+ max_value=4.00,
44
+ value=0.70
45
+ )
46
+ top_p = st.slider(
47
+ "Top-p (nucleus sampling)",
48
+ min_value=0.10,
49
+ max_value=1.00,
50
+ value=0.90
51
+ )
52
+
53
+ # --- Chatbot Function ---
54
+ def chatbot(input_text, history, model_choice, system_message, max_new_tokens, temperature, top_p):
55
  # Create payload for the model
56
  payload = {
57
+ "messages": [{"role": "user", "content": input_text}],
58
+ "system": system_message,
59
+ "max_tokens": max_new_tokens,
60
+ "temperature": temperature,
61
+ "top_p": top_p
 
 
62
  }
63
 
64
  # Run inference using the selected model
65
  try:
66
+ response = demo(payload) # Use the demo object directly
67
+ if isinstance(response, dict) and "choices" in response:
68
+ assistant_response = response["choices"][0]["message"]["content"]
 
 
 
 
 
 
 
69
  else:
70
  assistant_response = "Unexpected model response format."
71
  except Exception as e:
 
73
 
74
  # Append user and assistant messages to history
75
  history.append((input_text, assistant_response))
 
76
  return history
77
 
78
+ # --- Chat History Management ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
79
  if "chat_history" not in st.session_state:
80
  st.session_state.chat_history = []
81
 
82
+ # --- Chat Interface ---
83
+ st.header("Chat with DeepSeek")
84
+
85
  # Display chat history
86
  for user_msg, assistant_msg in st.session_state.chat_history:
87
  with st.chat_message("user"):
 
89
  with st.chat_message("assistant"):
90
  st.write(assistant_msg)
91
 
92
+ # Input for new message
93
+ input_text = st.chat_input("Type your message here...")
94
 
95
+ # Handle new message submission
96
+ if input_text:
97
+ # Update chat history
98
  st.session_state.chat_history = chatbot(
99
+ input_text,
100
  st.session_state.chat_history,
101
  model_choice,
102
  system_message,
 
105
  top_p
106
  )
107
 
108
+ # Rerun the app to display the updated chat history
109
  st.rerun()