ruslanmv commited on
Commit
1ed9ba1
·
verified ·
1 Parent(s): ecabf8c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -24
app.py CHANGED
@@ -16,34 +16,40 @@ if "messages" not in st.session_state:
16
  # Sidebar for model selection and parameters
17
  with st.sidebar:
18
  st.header("Model Configuration")
19
-
20
  # Model selection
21
- selected_model = st.selectbox(
 
 
 
 
 
 
 
 
 
22
  "Choose Model",
23
- [
24
- "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
25
- "deepseek-ai/DeepSeek-R1",
26
- "deepseek-ai/DeepSeek-R1-Zero"
27
- ],
28
  index=0
29
  )
30
-
 
31
  # System message
32
  system_message = st.text_area(
33
  "System Message",
34
  value="You are a friendly Chatbot created by ruslanmv.com",
35
  height=100
36
  )
37
-
38
  # Generation parameters
39
- max_tokens = st.slider(
40
- "Max Tokens",
41
  min_value=1,
42
  max_value=4000,
43
  value=512,
44
  step=10
45
  )
46
-
47
  temperature = st.slider(
48
  "Temperature",
49
  min_value=0.1,
@@ -51,7 +57,7 @@ with st.sidebar:
51
  value=0.7,
52
  step=0.1
53
  )
54
-
55
  top_p = st.slider(
56
  "Top-p (nucleus sampling)",
57
  min_value=0.1,
@@ -73,35 +79,33 @@ for message in st.session_state.messages:
73
  if prompt := st.chat_input("Type your message..."):
74
  # Add user message to chat history
75
  st.session_state.messages.append({"role": "user", "content": prompt})
76
-
77
  # Display user message
78
  with st.chat_message("user"):
79
  st.markdown(prompt)
80
-
81
  # Prepare conversation history in the required format
82
  full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
83
-
84
  try:
85
  # Generate response using selected model
86
  with st.spinner("Generating response..."):
87
  # The model expects a single text input with the conversation history
88
- response = demo.fn(
89
  full_prompt, # Pass the full conversation history
90
- # Parameters need to be passed as a dictionary in the second argument
91
  {
92
  "temperature": temperature,
93
  "top_p": top_p,
94
- "max_new_tokens": max_tokens,
95
  "repetition_penalty": 1.0
96
  }
97
  )
98
-
99
  # Display assistant response
100
  with st.chat_message("assistant"):
101
  st.markdown(response)
102
-
103
  # Add assistant response to chat history
104
  st.session_state.messages.append({"role": "assistant", "content": response})
105
-
106
- except Exception as e:
107
- st.error(f"Error generating response: {str(e)}")
 
16
  # Sidebar for model selection and parameters
17
  with st.sidebar:
18
  st.header("Model Configuration")
19
+
20
  # Model selection
21
+ model_mapping = {
22
+ "deepseek-ai/DeepSeek-R1-Distill-Qwen-32B": gr.load(
23
+ name="deepseek-ai/DeepSeek-R1-Distill-Qwen-32B",
24
+ src="huggingface"
25
+ ),
26
+ "deepseek-ai/DeepSeek-R1": gr.load(name="deepseek-ai/DeepSeek-R1", src="huggingface"),
27
+ "deepseek-ai/DeepSeek-R1-Zero": gr.load(name="deepseek-ai/DeepSeek-R1-Zero", src="huggingface")
28
+ }
29
+
30
+ selected_model_name = st.selectbox(
31
  "Choose Model",
32
+ list(model_mapping.keys()),
 
 
 
 
33
  index=0
34
  )
35
+ selected_model = model_mapping[selected_model_name]
36
+
37
  # System message
38
  system_message = st.text_area(
39
  "System Message",
40
  value="You are a friendly Chatbot created by ruslanmv.com",
41
  height=100
42
  )
43
+
44
  # Generation parameters
45
+ max_new_tokens = st.slider(
46
+ "Max New Tokens",
47
  min_value=1,
48
  max_value=4000,
49
  value=512,
50
  step=10
51
  )
52
+
53
  temperature = st.slider(
54
  "Temperature",
55
  min_value=0.1,
 
57
  value=0.7,
58
  step=0.1
59
  )
60
+
61
  top_p = st.slider(
62
  "Top-p (nucleus sampling)",
63
  min_value=0.1,
 
79
  if prompt := st.chat_input("Type your message..."):
80
  # Add user message to chat history
81
  st.session_state.messages.append({"role": "user", "content": prompt})
82
+
83
  # Display user message
84
  with st.chat_message("user"):
85
  st.markdown(prompt)
86
+
87
  # Prepare conversation history in the required format
88
  full_prompt = f"{system_message}\n\nUser: {prompt}\nAssistant:"
89
+
90
  try:
91
  # Generate response using selected model
92
  with st.spinner("Generating response..."):
93
  # The model expects a single text input with the conversation history
94
+ response = selected_model.fn(
95
  full_prompt, # Pass the full conversation history
 
96
  {
97
  "temperature": temperature,
98
  "top_p": top_p,
99
+ "max_new_tokens": max_new_tokens,
100
  "repetition_penalty": 1.0
101
  }
102
  )
103
+
104
  # Display assistant response
105
  with st.chat_message("assistant"):
106
  st.markdown(response)
107
+
108
  # Add assistant response to chat history
109
  st.session_state.messages.append({"role": "assistant", "content": response})
110
+
111
+ except Exception as e: