ruslanmv commited on
Commit
5a584aa
·
verified ·
1 Parent(s): fd1b0cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -111
app.py CHANGED
@@ -1,118 +1,103 @@
1
  # app.py
2
-
3
  import streamlit as st
4
- from models import demo_qwen, demo_r1, demo_zero
5
-
6
- st.set_page_config(page_title="DeepSeek Chatbot", layout="centered")
7
-
8
- # A helper function to pick the correct Gradio interface
9
- def select_demo(model_name):
10
- if model_name == "DeepSeek-R1-Distill-Qwen-32B":
11
- return demo_qwen
12
- elif model_name == "DeepSeek-R1":
13
- return demo_r1
14
- elif model_name == "DeepSeek-R1-Zero":
15
- return demo_zero
16
- else:
17
- return demo_qwen # default fallback
18
-
19
- # Title of the Streamlit app
20
- st.title("DeepSeek Chatbot")
21
-
22
- # Sidebar or main area for parameter selection
23
- st.subheader("Model and Parameters")
24
-
25
- model_name = st.selectbox(
26
- "Select Model",
27
- ["DeepSeek-R1-Distill-Qwen-32B", "DeepSeek-R1", "DeepSeek-R1-Zero"]
28
- )
29
-
30
- # Optional parameter: System message
31
- system_message = st.text_area(
32
- "System Message",
33
- value="You are a friendly Chatbot created by ruslanmv.com",
34
- height=80
35
- )
36
-
37
- # Optional parameter: max new tokens
38
- max_new_tokens = st.slider(
39
- "Max new tokens",
40
- min_value=1,
41
- max_value=4000,
42
- value=512,
43
- step=1
44
- )
45
 
46
- # Optional parameter: temperature
47
- temperature = st.slider(
48
- "Temperature",
49
- min_value=0.10,
50
- max_value=4.00,
51
- value=0.80,
52
- step=0.05
53
  )
54
 
55
- # Optional parameter: top-p
56
- top_p = st.slider(
57
- "Top-p (nucleus sampling)",
58
- min_value=0.10,
59
- max_value=1.00,
60
- value=0.90,
61
- step=0.05
62
- )
63
-
64
- # A text area for user input
65
- st.subheader("Chat")
66
- user_prompt = st.text_area("Your message:", value="", height=100)
67
-
68
- if "chat_history" not in st.session_state:
69
- st.session_state["chat_history"] = []
70
-
71
- # Button to send user prompt
72
- if st.button("Send"):
73
- if user_prompt.strip():
74
- # Retrieve the correct Gradio demo
75
- demo = select_demo(model_name)
76
-
77
- # Here we assume the Gradio interface has a function signature for `.predict()`
78
- # that accepts text plus generation parameters in some order.
79
- # Many huggingface-style Gradio demos simply take a single text prompt,
80
- # but it depends entirely on how `demo` is defined in your Gradio code.
81
- #
82
- # If the interface has multiple inputs in a specific order, you might do something like:
83
- # response = demo.predict(system_message, user_prompt, max_new_tokens, temperature, top_p)
84
- #
85
- # Or if it only expects a single string, you might combine them:
86
- # combined_prompt = f"System: {system_message}\nUser: {user_prompt}"
87
- # response = demo.predict(combined_prompt, max_new_tokens, temperature, top_p)
88
- #
89
- # The exact call depends on your Gradio block's input signature.
90
-
91
- # For illustrative purposes, let's assume a simple signature:
92
- # demo.predict(prompt: str, max_new_tokens: int, temperature: float, top_p: float)
93
- # and we inject the system message on top:
94
-
95
- combined_prompt = f"{system_message}\n\nUser: {user_prompt}"
96
-
97
- try:
98
- response = demo.predict(
99
- combined_prompt,
100
- max_new_tokens,
101
- temperature,
102
- top_p
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
103
  )
104
- except Exception as e:
105
- response = f"Error: {e}"
106
 
107
- st.session_state["chat_history"].append(("User", user_prompt))
108
- st.session_state["chat_history"].append(("Assistant", response))
109
- st.experimental_rerun()
110
-
111
- # Display conversation
112
- if st.session_state["chat_history"]:
113
- for role, text in st.session_state["chat_history"]:
114
- if role == "User":
115
- st.markdown(f"**{role}:** {text}")
116
- else:
117
- st.markdown(f"**{role}:** {text}")
118
-
 
1
  # app.py
 
2
  import streamlit as st
3
+ from models import demo
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
+ # Page configuration
6
+ st.set_page_config(
7
+ page_title="DeepSeek Chatbot - ruslanmv.com",
8
+ page_icon="🤖",
9
+ layout="centered"
 
 
10
  )
11
 
12
+ # Initialize session state for chat history
13
+ if "messages" not in st.session_state:
14
+ st.session_state.messages = []
15
+
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,
50
+ max_value=4.0,
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,
58
+ max_value=1.0,
59
+ value=0.9,
60
+ step=0.1
61
+ )
62
+
63
+ # Main chat interface
64
+ st.title("🤖 DeepSeek Chatbot")
65
+ st.caption("Powered by ruslanmv.com - Choose your model and parameters in the sidebar")
66
+
67
+ # Display chat messages
68
+ for message in st.session_state.messages:
69
+ with st.chat_message(message["role"]):
70
+ st.markdown(message["content"])
71
+
72
+ # Chat input
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 full prompt with system message
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
+ # Updated parameter names to match model expectations
88
+ response = demo.fn(
89
+ full_prompt,
90
+ max_length=max_tokens, # Changed from max_new_tokens
91
+ temperature=temperature,
92
+ top_p=top_p
93
  )
 
 
94
 
95
+ # Display assistant response
96
+ with st.chat_message("assistant"):
97
+ st.markdown(response)
98
+
99
+ # Add assistant response to chat history
100
+ st.session_state.messages.append({"role": "assistant", "content": response})
101
+
102
+ except Exception as e:
103
+ st.error(f"Error generating response: {str(e)}")