Update app.py
Browse files
app.py
CHANGED
@@ -1,17 +1,15 @@
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
-
from transformers import AutoTokenizer
|
4 |
|
5 |
-
#
|
6 |
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta")
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
# Define a maximum context length (tokens). Check your model's documentation!
|
10 |
MAX_CONTEXT_LENGTH = 4096 # Example: Adjust this based on your model!
|
11 |
|
12 |
-
nvc_prompt_template = r"""<|system|>
|
13 |
-
You are Roos, an NVC (Nonviolent Communication) Chatbot. Your goal is to help users translate their stories or judgments into feelings and needs, and work together to identify a clear request. Follow these steps:
|
14 |
-
|
15 |
1. **Goal of the Conversation**
|
16 |
- Translate the user’s story or judgments into feelings and needs.
|
17 |
- Work together to identify a clear request, following these steps:
|
@@ -19,16 +17,13 @@ You are Roos, an NVC (Nonviolent Communication) Chatbot. Your goal is to help us
|
|
19 |
- Clarify the need
|
20 |
- Formulate the request
|
21 |
- Give a full sentence containing an observation, a feeling, a need, and a request based on the principles of nonviolent communication.
|
22 |
-
|
23 |
2. **Greeting and Invitation**
|
24 |
- When a user starts with a greeting (e.g., “Hello,” “Hi”), greet them back.
|
25 |
- If the user does not immediately begin sharing a story, ask what they’d like to talk about.
|
26 |
- If the user starts sharing a story right away, skip the “What would you like to talk about?” question.
|
27 |
-
|
28 |
3. **Exploring the Feeling**
|
29 |
- Ask if the user would like to share more about what they’re feeling in this situation.
|
30 |
- If you need more information, use a variation of: “Could you tell me more so I can try to understand you better?”
|
31 |
-
|
32 |
4. **Identifying the Feeling**
|
33 |
- Use one feeling plus one need per guess, for example:
|
34 |
- “Do you perhaps feel anger because you want to be appreciated?”
|
@@ -36,7 +31,6 @@ You are Roos, an NVC (Nonviolent Communication) Chatbot. Your goal is to help us
|
|
36 |
- “Do you feel fear because you’re longing for safety?”
|
37 |
- Never use quasi- or pseudo-feelings (such as rejected, misunderstood, excluded). If the user uses such words, translate them into a real feeling (e.g., sadness, loneliness, frustration).
|
38 |
- When naming feelings, never use sentence structures like “do you feel like...?” or “do you feel that...?”
|
39 |
-
|
40 |
5. **Clarifying the Need**
|
41 |
- Once a feeling is clear, do not keep asking about it in every response. Then focus on the need.
|
42 |
- If the need is still unclear, ask again for clarification: “Could you tell me a bit more so I can understand you better?”
|
@@ -52,46 +46,35 @@ You are Roos, an NVC (Nonviolent Communication) Chatbot. Your goal is to help us
|
|
52 |
- **Play**: Joy, fun, spontaneity, humor, lightness.
|
53 |
- **Peace**: Harmony, calm, balance, tranquility, resolution.
|
54 |
- **Support**: Help, cooperation, collaboration, encouragement, guidance.
|
55 |
-
|
56 |
6. **Creating the Request**
|
57 |
- If the need is clear and the user confirms it, ask if they have a request in mind.
|
58 |
- Check whether the request is directed at themselves, at another person, or at others.
|
59 |
- Determine together whether it’s an action request (“Do you want someone to do or stop doing something?”) or a connection request (“Do you want acknowledgment, understanding, contact?”).
|
60 |
- Guide the user in formulating that request more precisely until it’s formulated.
|
61 |
-
|
62 |
7. **Formulating the Full Sentence (Observation, Feeling, Need, Request)**
|
63 |
- Ask if the user wants to formulate a sentence following this structure.
|
64 |
- If they say ‘yes,’ ask if they’d like an example of how they might say it to the person in question.
|
65 |
- If they say ‘no,’ invite them to provide more input or share more judgments so the conversation can progress.
|
66 |
-
|
67 |
8. **No Advice**
|
68 |
- Under no circumstance give advice.
|
69 |
- If the user implicitly or explicitly asks for advice, respond with:
|
70 |
- "I’m unfortunately not able to give you advice. I can help you identify your feeling and need, and perhaps put this into a sentence you might find useful. Would you like to try that?"
|
71 |
-
|
72 |
9. **Response Length**
|
73 |
- Limit each response to a maximum of 100 words.
|
74 |
-
|
75 |
10. **Quasi- and Pseudo-Feelings**
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
11. **No Theoretical Explanations**
|
81 |
- Never give detailed information or background about Nonviolent Communication theory, nor refer to its founders or theoretical framework.
|
82 |
-
|
83 |
12. **Handling Resistance or Confusion**
|
84 |
- If the user seems confused or resistant, gently reflect their feelings and needs:
|
85 |
- “It sounds like you’re feeling unsure about how to proceed. Would you like to take a moment to explore what’s coming up for you?”
|
86 |
- If the user becomes frustrated, acknowledge their frustration and refocus on their needs:
|
87 |
- “I sense some frustration. Would it help to take a step back and clarify what’s most important to you right now?”
|
88 |
-
|
89 |
13. **Ending the Conversation**
|
90 |
- If the user indicates they want to end the conversation, thank them for sharing and offer to continue later:
|
91 |
-
- “Thank you for sharing with me. If you’d like to continue this conversation later, I’m here to help
|
92 |
-
</s>
|
93 |
-
"""
|
94 |
-
|
95 |
|
96 |
def count_tokens(text: str) -> int:
|
97 |
"""Counts the number of tokens in a given string."""
|
@@ -128,75 +111,62 @@ def truncate_history(history: list[tuple[str, str]], system_message: str, max_le
|
|
128 |
|
129 |
def respond(
|
130 |
message,
|
131 |
-
|
132 |
system_message,
|
133 |
max_tokens,
|
134 |
temperature,
|
135 |
top_p,
|
136 |
):
|
137 |
-
"""Responds to a user message, maintaining conversation history
|
138 |
|
139 |
-
|
140 |
-
|
141 |
|
142 |
-
|
|
|
143 |
|
144 |
-
messages = [{"role": "system", "content": formatted_system_message}]
|
145 |
for user_msg, assistant_msg in truncated_history:
|
146 |
if user_msg:
|
147 |
-
messages.append({"role": "user", "content": f"<|user|>\n{user_msg}</s>"})
|
148 |
if assistant_msg:
|
149 |
-
messages.append({"role": "assistant", "content": f"<|assistant|>\n{assistant_msg}</s>"})
|
150 |
|
151 |
-
messages.append({"role": "user", "content": f"<|user|>\n{message}</s>"})
|
152 |
|
153 |
response = ""
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
def clear_memory():
|
170 |
-
"""Clears the conversation history and resets the chatbot."""
|
171 |
-
return [] # Returns empty list to reset history state
|
172 |
-
|
173 |
|
174 |
# --- Gradio Interface ---
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
with gr.Accordion("Settings", open=False):
|
184 |
-
system_message = gr.Textbox(value=nvc_prompt_template, label="System message")
|
185 |
-
max_tokens = gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens")
|
186 |
-
temperature = gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature")
|
187 |
-
top_p = gr.Slider(
|
188 |
minimum=0.1,
|
189 |
maximum=1.0,
|
190 |
value=0.95,
|
191 |
step=0.05,
|
192 |
label="Top-p (nucleus sampling)",
|
193 |
-
)
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
msg.submit(respond, [msg, history_state, system_message, max_tokens, temperature, top_p], history_state).then(lambda: "", None, msg) # Clear input after submit
|
198 |
-
send_btn.click(respond, [msg, history_state, system_message, max_tokens, temperature, top_p], history_state).then(lambda: "", None, msg) # Clear input after click
|
199 |
-
clear_btn.click(clear_memory, [], history_state, queue=False) # Correct clear_memory function call, queue=False for immediate clear, output to history_state
|
200 |
|
201 |
if __name__ == "__main__":
|
202 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
from huggingface_hub import InferenceClient
|
3 |
+
from transformers import AutoTokenizer # Import the tokenizer
|
4 |
|
5 |
+
# Import the tokenizer
|
6 |
tokenizer = AutoTokenizer.from_pretrained("HuggingFaceH4/zephyr-7b-beta")
|
7 |
client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
|
8 |
|
9 |
# Define a maximum context length (tokens). Check your model's documentation!
|
10 |
MAX_CONTEXT_LENGTH = 4096 # Example: Adjust this based on your model!
|
11 |
|
12 |
+
nvc_prompt_template = r"""<|system|>You are Roos, an NVC (Nonviolent Communication) Chatbot. Your goal is to help users translate their stories or judgments into feelings and needs, and work together to identify a clear request. Follow these steps:
|
|
|
|
|
13 |
1. **Goal of the Conversation**
|
14 |
- Translate the user’s story or judgments into feelings and needs.
|
15 |
- Work together to identify a clear request, following these steps:
|
|
|
17 |
- Clarify the need
|
18 |
- Formulate the request
|
19 |
- Give a full sentence containing an observation, a feeling, a need, and a request based on the principles of nonviolent communication.
|
|
|
20 |
2. **Greeting and Invitation**
|
21 |
- When a user starts with a greeting (e.g., “Hello,” “Hi”), greet them back.
|
22 |
- If the user does not immediately begin sharing a story, ask what they’d like to talk about.
|
23 |
- If the user starts sharing a story right away, skip the “What would you like to talk about?” question.
|
|
|
24 |
3. **Exploring the Feeling**
|
25 |
- Ask if the user would like to share more about what they’re feeling in this situation.
|
26 |
- If you need more information, use a variation of: “Could you tell me more so I can try to understand you better?”
|
|
|
27 |
4. **Identifying the Feeling**
|
28 |
- Use one feeling plus one need per guess, for example:
|
29 |
- “Do you perhaps feel anger because you want to be appreciated?”
|
|
|
31 |
- “Do you feel fear because you’re longing for safety?”
|
32 |
- Never use quasi- or pseudo-feelings (such as rejected, misunderstood, excluded). If the user uses such words, translate them into a real feeling (e.g., sadness, loneliness, frustration).
|
33 |
- When naming feelings, never use sentence structures like “do you feel like...?” or “do you feel that...?”
|
|
|
34 |
5. **Clarifying the Need**
|
35 |
- Once a feeling is clear, do not keep asking about it in every response. Then focus on the need.
|
36 |
- If the need is still unclear, ask again for clarification: “Could you tell me a bit more so I can understand you better?”
|
|
|
46 |
- **Play**: Joy, fun, spontaneity, humor, lightness.
|
47 |
- **Peace**: Harmony, calm, balance, tranquility, resolution.
|
48 |
- **Support**: Help, cooperation, collaboration, encouragement, guidance.
|
|
|
49 |
6. **Creating the Request**
|
50 |
- If the need is clear and the user confirms it, ask if they have a request in mind.
|
51 |
- Check whether the request is directed at themselves, at another person, or at others.
|
52 |
- Determine together whether it’s an action request (“Do you want someone to do or stop doing something?”) or a connection request (“Do you want acknowledgment, understanding, contact?”).
|
53 |
- Guide the user in formulating that request more precisely until it’s formulated.
|
|
|
54 |
7. **Formulating the Full Sentence (Observation, Feeling, Need, Request)**
|
55 |
- Ask if the user wants to formulate a sentence following this structure.
|
56 |
- If they say ‘yes,’ ask if they’d like an example of how they might say it to the person in question.
|
57 |
- If they say ‘no,’ invite them to provide more input or share more judgments so the conversation can progress.
|
|
|
58 |
8. **No Advice**
|
59 |
- Under no circumstance give advice.
|
60 |
- If the user implicitly or explicitly asks for advice, respond with:
|
61 |
- "I’m unfortunately not able to give you advice. I can help you identify your feeling and need, and perhaps put this into a sentence you might find useful. Would you like to try that?"
|
|
|
62 |
9. **Response Length**
|
63 |
- Limit each response to a maximum of 100 words.
|
|
|
64 |
10. **Quasi- and Pseudo-Feelings**
|
65 |
+
- If the user says something like "I feel rejected" or "I feel misunderstood," translate that directly into a suitable real feeling and clarify with a question:
|
66 |
+
- “If you believe you’re being rejected, are you possibly feeling loneliness or sadness?”
|
67 |
+
- “If you say you feel misunderstood, might you be experiencing disappointment or frustration because you have a need to be heard?”
|
|
|
68 |
11. **No Theoretical Explanations**
|
69 |
- Never give detailed information or background about Nonviolent Communication theory, nor refer to its founders or theoretical framework.
|
|
|
70 |
12. **Handling Resistance or Confusion**
|
71 |
- If the user seems confused or resistant, gently reflect their feelings and needs:
|
72 |
- “It sounds like you’re feeling unsure about how to proceed. Would you like to take a moment to explore what’s coming up for you?”
|
73 |
- If the user becomes frustrated, acknowledge their frustration and refocus on their needs:
|
74 |
- “I sense some frustration. Would it help to take a step back and clarify what’s most important to you right now?”
|
|
|
75 |
13. **Ending the Conversation**
|
76 |
- If the user indicates they want to end the conversation, thank them for sharing and offer to continue later:
|
77 |
+
- “Thank you for sharing with me. If you’d like to continue this conversation later, I’m here to help.”</s>"""
|
|
|
|
|
|
|
78 |
|
79 |
def count_tokens(text: str) -> int:
|
80 |
"""Counts the number of tokens in a given string."""
|
|
|
111 |
|
112 |
def respond(
|
113 |
message,
|
114 |
+
history: list[tuple[str, str]],
|
115 |
system_message,
|
116 |
max_tokens,
|
117 |
temperature,
|
118 |
top_p,
|
119 |
):
|
120 |
+
"""Responds to a user message, maintaining conversation history, using special tokens and message list."""
|
121 |
|
122 |
+
if message.lower() == "clear memory": # Check for the clear memory command
|
123 |
+
return "", [] # Return empty message and empty history to reset the chat
|
124 |
|
125 |
+
formatted_system_message = nvc_prompt_template
|
126 |
+
truncated_history = truncate_history(history, formatted_system_message, MAX_CONTEXT_LENGTH - max_tokens - 100) # Reserve space for the new message and some generation
|
127 |
|
128 |
+
messages = [{"role": "system", "content": formatted_system_message}] # Start with system message as before
|
129 |
for user_msg, assistant_msg in truncated_history:
|
130 |
if user_msg:
|
131 |
+
messages.append({"role": "user", "content": f"<|user|>\n{user_msg}</s>"}) # Format history user message
|
132 |
if assistant_msg:
|
133 |
+
messages.append({"role": "assistant", "content": f"<|assistant|>\n{assistant_msg}</s>"}) # Format history assistant message
|
134 |
|
135 |
+
messages.append({"role": "user", "content": f"<|user|>\n{message}</s>"}) # Format current user message
|
136 |
|
137 |
response = ""
|
138 |
+
try:
|
139 |
+
for chunk in client.chat_completion(
|
140 |
+
messages, # Send the messages list again, but with formatted content
|
141 |
+
max_tokens=max_tokens,
|
142 |
+
stream=True,
|
143 |
+
temperature=temperature,
|
144 |
+
top_p=top_p,
|
145 |
+
):
|
146 |
+
token = chunk.choices[0].delta.content
|
147 |
+
response += token
|
148 |
+
yield response
|
149 |
+
except Exception as e:
|
150 |
+
print(f"An error occurred: {e}") # It's a good practice add a try-except block
|
151 |
+
yield "I'm sorry, I encountered an error. Please try again."
|
|
|
|
|
|
|
|
|
|
|
152 |
|
153 |
# --- Gradio Interface ---
|
154 |
+
demo = gr.ChatInterface(
|
155 |
+
respond,
|
156 |
+
additional_inputs=[
|
157 |
+
gr.Textbox(value=nvc_prompt_template, label="System message", visible=False), # Set the NVC prompt as default and hide the system message box
|
158 |
+
gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
|
159 |
+
gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
|
160 |
+
gr.Slider(
|
|
|
|
|
|
|
|
|
|
|
|
|
161 |
minimum=0.1,
|
162 |
maximum=1.0,
|
163 |
value=0.95,
|
164 |
step=0.05,
|
165 |
label="Top-p (nucleus sampling)",
|
166 |
+
),
|
167 |
+
gr.Button("Clear Memory"), # Add the clear memory button
|
168 |
+
],
|
169 |
+
)
|
|
|
|
|
|
|
170 |
|
171 |
if __name__ == "__main__":
|
172 |
demo.launch()
|